PyGame Full Tutorial


Video :- 



IMPORTANT :- 

PLEASE KEEP ALL THE FILES IN THE SAME FOLDER WHERE YOUR CODE IS WRITTEN.



Audio Files :- 

1. https://drive.google.com/file/d/1QW33W0Yr2-3unPn9AbN9lSkj-p7ufBT1/view?usp=sharing
2. https://drive.google.com/file/d/18Mwq0k7QSV7mUtbMl8vLGfs75ljoVrx7/view?usp=sharing

Just copy paste these links in a new tab and just download the files from there...

Images :-




CODE :- 

import pygame
import os
pygame.font.init()
pygame.mixer.init()
Bullet_Hit = pygame.mixer.Sound("Play.mp3")
Bullet_Fire = pygame.mixer.Sound("Sound.mp3")
FPS = 60
WIDTH = 1200
HEIGHT = 600
Shooter_Width = 95
Shooter_Height = 95
Vel = 5


#Bullets
Bullet_Vel = 8
Max_Bullets = 5
HEALTH_FONT = pygame.font.SysFont("comicsans",40)
WINNER_FONT = pygame.font.SysFont("comicsans",100)

S1_HIT = pygame.USEREVENT + 1
S2_HIT =pygame.USEREVENT + 2

BLACK = (0,0,0)
Border = pygame.Rect((WIDTH/2)-5,0 ,10 ,HEIGHT)
win = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shooter By Scientificoder")
SPACE_IMG = pygame.transform.scale(pygame.image.load("Brown2.jpg"),(WIDTH,HEIGHT))
Shooter_Image = pygame.image.load("Shooter.png")
Shooter_1 = pygame.transform.scale(Shooter_Image,(Shooter_Width,Shooter_Height))
Shooter_2 = pygame.transform.flip(pygame.transform.scale(Shooter_Image,(Shooter_Width,Shooter_Height)),True,False)

def drawWindows(S1,S2,S1_BULLET,S2_BULLET,S1_HEALTH,S2_HEALTH):
    win.blit(SPACE_IMG,(0,0))
    win.blit(Shooter_1,(S1.x,S1.y))
    win.blit(Shooter_2,(S2.x,S2.y))
    pygame.draw.rect(win,BLACK,Border)

    S1_healthtext = HEALTH_FONT.render(
        "Health: " + str(S2_HEALTH),1,BLACK)
   
    S2_healthtext = HEALTH_FONT.render(
        "Health: " + str(S1_HEALTH),1,BLACK)
    win.blit(S1_healthtext,(WIDTH - S1_healthtext.get_width()-10,10))
    win.blit(S2_healthtext,(10,10))


    for bullet in S1_BULLET:
        pygame.draw.rect(win,BLACK,bullet)
    for bullet in S2_BULLET:
        pygame.draw.rect(win,BLACK,bullet)

    pygame.display.update()

def S1_movement(S1,keys_pressed):
    if keys_pressed[pygame.K_w] and S1.y -Vel >0:
        S1.y -= Vel
    if keys_pressed[pygame.K_s] and S1.y + Vel <510:
        S1.y += Vel
    if keys_pressed[pygame.K_d] and S1.x + Vel < 530:
        S1.x += Vel
    if keys_pressed[pygame.K_a] and S1.x - Vel >0:
        S1.x -= Vel

def S2_movement(S2,keys_pressed):
    if keys_pressed[pygame.K_UP] and S2.y - Vel >0:
        S2.y -= Vel
    if keys_pressed[pygame.K_DOWN] and S2.y + Vel <510:
        S2.y += Vel
    if keys_pressed[pygame.K_RIGHT] and S2.x + Vel <1120:
        S2.x += Vel
    if keys_pressed[pygame.K_LEFT] and S2.x - Vel > 590:
        S2.x -= Vel

def handlebullets(S1_BULLET,S2_BULLET,S1,S2):
    for bullet in S1_BULLET:
        bullet.x += Bullet_Vel
        if S2.colliderect(bullet):
            pygame.event.post(pygame.event.Event(S2_HIT))
            Bullet_Hit.play()
            S1_BULLET.remove(bullet)
        elif bullet.x > WIDTH:
            S1_BULLET.remove(bullet)
       

    for bullet in S2_BULLET:
        bullet.x -= Bullet_Vel
        if S1.colliderect(bullet):
            pygame.event.post(pygame.event.Event(S1_HIT))
            Bullet_Hit.play()
            S2_BULLET.remove(bullet)
        elif bullet.x<0:
            S2_BULLET.remove(bullet)
       

def winner(text):
    draw_text = WINNER_FONT.render(text,1,BLACK)
    win.blit(draw_text,(WIDTH/2 - draw_text.get_width()/2,HEIGHT/2-draw_text.get_height()/2))
    pygame.display.update()
    pygame.time.delay(5000)

def run():
    S1 = pygame.Rect(100,300,Shooter_Width,Shooter_Height)
    S2 = pygame.Rect(1100,300,Shooter_Width,Shooter_Height)
    S1_BULLET = []
    S2_BULLET = []
    S1_HEALTH = 10
    S2_HEALTH = 10
    clock = pygame.time.Clock()
    Flag = True


    while Flag:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Flag = False
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RCTRL and len(S2_BULLET)<Max_Bullets:
                    bullet = pygame.Rect(S2.x,S2.y+S2.height//2 -2, 10,5)
                    Bullet_Fire.play()
                    S2_BULLET.append(bullet)

                if event.key == pygame.K_LCTRL and len(S1_BULLET)<Max_Bullets:
                    bullet = pygame.Rect(S1.x+30,S1.y+S1.height//2 -2, 10,5)
                    Bullet_Fire.play()
                    S1_BULLET.append(bullet)

            if event.type == S1_HIT:
                S1_HEALTH -= 1

            if event.type == S2_HIT:
                S2_HEALTH -= 1
           
        winner_text = ""
        if S1_HEALTH <= 0:
            winner_text = "Player 2 Wins !"

        if S2_HEALTH <= 0:
            winner_text = "Player 1 Wins !"

        if winner_text != "":
            winner(winner_text)
            break



        keys_pressed = pygame.key.get_pressed()
        drawWindows(S1,S2,S1_BULLET,S2_BULLET,S1_HEALTH,S2_HEALTH)
        S1_movement(S1,keys_pressed)
        S2_movement(S2,keys_pressed)
        handlebullets(S1_BULLET,S2_BULLET,S1,S2)



if __name__ == "__main__":
    run()


HOPE YOU LIKE IT !!!


 

No comments:

Post a Comment