Thursday, 21 January 2021

Game No. 2 : SPACE INVADERS Game in Python

 Game No. 2 : SPACE INVADERS Game in Python


import pygame

import random

import math,sys


 

#INITIALISING PYGAME

pygame.init()


#SCREEN

screen = pygame.display.set_mode((800,600))

 

#TITLE

pygame.display.set_caption("SPACE INVADERS")

 

#BACKGROUND

background = pygame.image.load('imgs/background.png')

#pygame.mixer.music.load('sounds/background.wav')

#pygame.mixer.music.play(-1)

 

#PLAYER

playerimg = pygame.image.load('imgs/si.png')

pX = 360

pY = 480

pXchange = 0

pYchange = 0

speed = 5

def player(x,y):

    screen.blit(playerimg, (x,y))

 

#ENEMY

#eSound = pygame.mixer.Sound('sounds/explosion.wav')

'''enemyimg = pygame.image.load('imgs/alien.png')

eX = 400

eY = 100

eXchange = 3

eYchange = 3'''

enemyimg = []

eX = []

eY = []

eXchange = []

eYchange = []

num_of_enemies = 5

 

for i in range(num_of_enemies):

    enemyimg.append(pygame.image.load('imgs/alien.png'))

    eX.append(random.randint(100,700))

    eY.append(random.randint(50,300))

    eXchange.append(3)

    eYchange.append(30)

 

def enemy(x,y,i):

    screen.blit(enemyimg[i], (x,y))

 

#BULLET

#bSound = pygame.mixer.Sound('sounds/laser.wav')

bulletimg = pygame.image.load('imgs/bullet.png')

bX = pX

bY = pY

bYchange = -10

bState = 0 #READY

def fire_bullet(x,y):

    global bState

    bState = 1 #FIRE

    screen.blit(bulletimg, (x+15,y-30))

 

#COLLISION DETECTION

def isCollision(EX,EY,BX,BY):

    distance = math.sqrt( (BX-EX)**2 + (BY-EY)**2 )

    if distance < 30:

        return True

    return False

 

#SCORE

score = 0

font = pygame.font.Font('freesansbold.ttf', 32)

sCoord = (10,10)

def print_score(sc):

    screen.blit(font.render("SCORE: " + str(sc), True, (255,255,255)), sCoord)

 

#GAME OVER

def game_over_text(sc):

    msg = pygame.font.Font('freesansbold.ttf', 64)

    mCoord = (180,200)

    screen.blit(msg.render("GAME OVER!!", True, (255,255,255)), mCoord)

 

    fs = pygame.font.Font('freesansbold.ttf', 32)

    fCoord = (280,300)

    screen.blit(fs.render("FINAL SCORE: " + str(sc), True, (255,255,255)), fCoord)

 

 

running = True

 

while running:

    #screen.fill((100,0,200))

    screen.blit(background, (0,0))

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

            pygame.quit()

            sys.exit(0)

 

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT: pXchange -= speed

            if event.key == pygame.K_RIGHT: pXchange += speed

            if event.key == pygame.K_UP: pYchange -= speed

            if event.key == pygame.K_DOWN: pYchange += speed

            if event.key == pygame.K_SPACE:

                #bSound.play()

                bX,bY = pX, pY

                fire_bullet(bX,bY)

 

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                pXchange = 0

            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:

                pYchange = 0

    

    '''

    collision = isCollision(eX,eY,bX,bY)

    if collision:

        eSound.play()

        score += 1

        eX = random.randint(100,700)

        eY = random.randint(50,250)

        bState = 0

    '''

 

    #PLAYER MOVEMENT

    pX += pXchange

    pY += pYchange

    if pX <= 0:

        pX = 800

    elif pX >= 800:

        pX = 0

    if pY < 0:

        pY = 0

    elif pY>536:

        pY = 536

    player(pX,pY)

 

    #ENEMY MOVEMENT

    for i in range(num_of_enemies):

 

        #GAME OVER

        if eY[i] > 400:

            for j in range(num_of_enemies):

                eY[j] = 800

            game_over_text(score)

            break

 

 

        eX[i] += eXchange[i]

        if eX[i] > 736:

            eY[i] += 30

            eXchange[i] = -eXchange[i]

        if eX[i] < 0:

            eY[i] += 30

            eXchange[i] = -eXchange[i]

 

        collision = isCollision(eX[i],eY[i],bX,bY)

        if collision:

            #eSound.play()

            score += 1

            eX[i] = random.randint(100,700)

            eY[i] = random.randint(50,250)

            bState = 0

 

        enemy(eX[i],eY[i],i)

 

 

    '''

    eX += eXchange

    if eX > 736:

        eY += 30

        eXchange = -eXchange

    elif eX < 0:

        eY += 30

        eXchange = -eXchange

    enemy(eX,eY)

    '''

    #BULLET MOVEMENT

    if bState == 1:

        fire_bullet(bX,bY)

        bY += bYchange

        if bY <= 0:

            bState = 0

 

    print_score(score)

    pygame.display.update()



#bit.ly/3dC6rF6

No comments:

Post a Comment