How to make Ghost Buster Game in Pygame?

How to make Ghost Buster Game in Pygame?

So, in this article, I'll be writing about how to make a ghost buster game in Pygame. In this game, user has to kill all the Ghosts inorder to save yourself.!

We will be using Pygame(a Python library) to build this game. Pygame is an open-source library that is designed for making video games. It helps us to create fully functional games and multimedia programs in python.

The “Ghost Busters” game which we have built using Pygame, a python programming language library where it consist of multiple multimedia platform that utilize different form of programming. The game is played in a simple windowed interface that display a haunted background and every second ghosts appear from anywhere. The player can play the game via mouse. They need to take the mouse to where the ghosts appear and click on it. The game is simple which is all about how many ghosts you can kill in a time span of twenty seconds. The score will be displayed automatically on the left corner of the screen as you proceed the gameplay. The game will be over as soon as time counter shows twenty seconds. We have also decided put this game with other common games made by us like angry birds, minecraft, tic-tac-toe etc. in a website that we created.

First of all, You have to install the Pygame library using the command:-

pip install pygame

Stepwise Implementation

Step 1:

In this first step, we have to import libraries. After that, we have to set the height and width of the screen to which the game will be played alongwith timer.

import pygame
import random
from pygame.locals import *
pygame.init()
w=1000
h=600
red=(255,0,0)
col=(255,0,255)
col1=(255,255,0)
sec=8
pygame.time.set_timer(USEREVENT+1,1000)

Step 2:

Now we have to define some images which we shall use in our game like background image, aim_pointer image, gun and ghosts alongwith sounds.

# Images and Sounds of Background and Gun
screen = pygame.display.set_mode((w,h))
pic=pygame.image.load("assets/images/background.png")
tar=pygame.image.load("assets/images/aim_pointer.png")
gun=pygame.image.load("assets/images/gun.png")
gsound=pygame.mixer.Sound("assets/sounds/shot_sound.wav")
bsound=pygame.mixer.Sound("assets/sounds/background.wav")
bsound.play()
# Images of Ghosts 
def game():
    sec=20
    w=800
    h=500
    z1=pygame.image.load("assets/images/ghost_1.png")
    z2=pygame.image.load("assets/images/ghost_2.png")
    z3=pygame.image.load("assets/images/ghost_3.png")
    z4=pygame.image.load("assets/images/ghost_4.png")
    zlist=[z1,z2,z3,z4]
    zimg=random.choice(zlist)
    zx = random.randint(0,w - 200)
    zy = random.randint(0,h - 300)
    counter = 0

Step 3:

Now declare time and score time.

# Time
def time(sec):
    font = pygame.font.SysFont(None,40)
    text=font.render('Time Left : '+str(sec),True,col)
    screen.blit(text,(w-200,0))
# Score Count
def score(counter):
    font = pygame.font.SysFont(None,40)
    text=font.render('Score : '+str(counter),True,red)
    screen.blit(text,(8,0))

Step 4:

Initialize the position of the bird and starting the game loop

# Game Loop
    while True:
        mx, my = pygame.mouse.get_pos()
        print(mx,my)
        zrec= pygame.Rect(zx,zy,zimg.get_width(),zimg.get_height())
        point= pygame.Rect(mx,my,tar.get_width(),tar.get_height())
        for event in pygame.event.get():
            print(event)
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()        
            elif event.type==USEREVENT+1:
                sec-=1            
            if event.type == pygame.MOUSEBUTTONDOWN:
                gsound.play()
                if zrec.colliderect(point):
                    print("collission")
                    zx = random.randint(0,w - 200)
                    zy = random.randint(0,h - 300)
                    zimg=random.choice(zlist)
                    counter+=1
        if sec==0:
            over()
            break        
        screen.blit(pic,(0,0))
        screen.blit(zimg,(zx,zy))
        screen.blit(tar,(mx-48,my-48))
        score(counter)
        time(sec)
        screen.blit(gun,(mx,h - 250))
        pygame.display.update()

Step 5:

Now we create a GameOver() function

# Game Over
def over():
    while True:
        font = pygame.font.SysFont(None,40)
        text=font.render('GAME OVER !! ',True,col1)
        screen.blit(text,(w/2-40 ,h/2-40))
        pygame.display.update()
        quit()


game()

You can download the code from here by forking it .