UnicMinds

Monster Game in PyGame

Making a Monster Game in PyGame

About the Game: 

There is a monster chasing the player. Avoid sharks and jellyfish on your way as they push you to the edge of the ocean. You lose if you get eaten by the monster. Score as many points that you can till you are eaten by the sea monster.

Steps of the Game:

Step 1: Setting up the game

Step 2: Creating the Sprite classes

Step 3: Creating Objects

Step 4: Displaying the score on the screen

Step 5: Checking the collisions

Step 1: Setting up the game

import pygame

pygame.init()

clock = pygame.time.Clock()   # to change the frame speed of the game.

score = 0

scoreText = pygame.font.SysFont(“Impact”, 30)

text = scoreText.render(“Score: ” + str(score), True, “red”) #creating the score text on the game screen

window = pygame.display.set_mode(size=(1900, 1000)) #creating the game screen of with 1900 pixels and height of 1000 pixels

from pygame.locals import (   # importing all the key presses that would be used in the game

    K_UP,

    K_DOWN,

    K_RIGHT,

    K_LEFT,

    K_SPACE,

    QUIT

)

Step 2: Creating the Sprite classes

Monster class : Constructor function initialises the list of images for the monster in self.images   and an index variable to keep track of the images in the list.

Animation function iterates through index of the images as shown below

class Monster(pygame.sprite.Sprite):

    def __init__(self):

        super(Monster).__init__()

        self.images = [pygame.image.load(“monster1.png”), pygame.image.load(“monster2.png”), pygame.image.load(“monster3.png”)]

        self.index = 0

    def animation(self):

        self.surf = self.images[self.index]

        self.rect = self.surf.get_rect(center=(950, 900))

        if self.index < 1:

            self.index += 1

        else:

            self.index = 0

Player class : Constructor function initialises the list of images for the player in self.images   and an index variable to keep track of the images in the list.

Animation function iterates through index of the images as shown below

Update function is used to move the player using the up, down, left and right keys.

class Player(pygame.sprite.Sprite):

    def __init__(self):

        super(Player).__init__()

        self.images = [pygame.image.load(“player.png”), pygame.image.load(“player2.png”)]

        self.index = 0

        self.bounciness = 15

        self.Pushed = False

        self.surf = self.images[self.index]

        self.rect = self.surf.get_rect(center=(950, 150))

    def animation(self):

        self.surf = self.images[self.index]

        self.rect = self.surf.get_rect(center=(950, 150))

        if self.index < 1:

            self.index += 1

        else:

            self.index = 0

    def update(self, pressedKeys):

        #global y

        if not self.Pushed:

            if pressedKeys[pygame.K_UP]:

                self.rect.move_ip(0, -20)

            elif pressedKeys[pygame.K_RIGHT]:

                self.rect.move_ip(15, 0)

            elif pressedKeys[pygame.K_LEFT]:

                self.rect.move_ip(-15, 0)

            elif pressedKeys[pygame.K_DOWN]:

                self.rect.move_ip(0, 10)

    Jellyfish class has the usual constructor function and move function.Jellyfish moves from down to the top. Once the jellyfish moves out of the screen from the top, jelly fish appears randomly from the bottom and right part of the screen

class Jellyfish(pygame.sprite.Sprite):

    def __init__(self):

        super(Jellyfish).__init__()

        self.surf = pygame.image.load(“jellyfish.png”)

        self.speed = 10

        self.rect = self.surf.get_rect(center=(950, 300))

    def move(self):

        self.rect.move_ip(0, self.speed)

        if self.rect.top > 1200:

            self.rect.bottom = randint(0, 1200)

            self.rect.right = randint(500, 1900)

Class Fish creates the shark which moves from left to right. Once the shark is out of the screen from the right side, it appears randomly from the left part of the screen.

class Fish(pygame.sprite.Sprite):

    def __init__(self):

        super(Fish).__init__()

        self.surf = pygame.image.load(“fish.png”)

        self.speed = 20

        self.rect = self.surf.get_rect(center=(45, 300))

    def move(self):

        self.rect.move_ip(self.speed, 0)

        if self.rect.right >= 1900:

            self.rect.left = randint(-500, 100)

            self.rect.top = randint(200, 800)

Classes create two walls which act like channels across the water source. Both the walls move from top to bottom. Once both the walls leave the bottom part of the screen , they reappear from the top part of the screen.

class Wall1(pygame.sprite.Sprite):

    def __init__(self):

        super(Wall1).__init__()

        self.surf = pygame.image.load(“wall.png”)

        width = self.surf.get_width()

        height = self.surf.get_height()

        height += 300

        self.surf = pygame.transform.scale(self.surf, (386, height))

        self.rect = self.surf.get_rect(center=(100, 500))

    def move(self):

        self.rect.move_ip(0, 13)

        if self.rect.top > 1200:

            self.rect.bottom = randint(0, 1200)

class Wall2(pygame.sprite.Sprite):

    def __init__(self):

        super(Wall2).__init__()

        self.surf = pygame.image.load(“wall.png”)

        height = self.surf.get_height()

        height += 300

        self.surf = pygame.transform.scale(self.surf, (386, height))

        self.rect = self.surf.get_rect(center=(1800, 500))

    def move(self):

        self.rect.move_ip(0, 13)

        if self.rect.top > 1200:

            self.rect.bottom = randint(0, 1200)

Step 3: Creating the objects

monster1 = Monster()

fish = Fish()

wall1 = Wall1()

wall2 = Wall2()

player = Player()

jellyfish1 = Jellyfish()

jellyfish2 = Jellyfish2()

jellyfish3 = Jellyfish3()

making a Monster game in PyGame

Step 4: Creating the while loop for the game

while running:

    pressed_keys = pygame.key.get_pressed()

        #checking for trigger of events in the game

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

        window.fill(“dark blue”)

        #calling all the functions to move all the sprites in the game

        player.update(pressed_keys)

         jellyfish.move()

        fish.move()

        jellyfish2.move()

        jellyfish3.move()

        wall1.move()

        wall2.move()

        monster1.animation()

        #drawing all the sprites in the game using blit function

        window.blit(player.surf, player.rect)

        window.blit(jellyfish2.surf, jellyfish2.rect)

        window.blit(jellyfish3.surf, jellyfish3.rect)

        window.blit(jellyfish.surf, jellyfish.rect)

        window.blit(monster1.surf, monster1.rect)

        window.blit(wall2.surf, wall2.rect)

        window.blit(wall1.surf, wall1.rect)

        window.blit(fish.surf, fish.rect)

steps to make a Monster game in Python

Step 5: Checking all the Collisions

if pygame.Rect.colliderect(player.rect, monster1.rect):

            print(“You died”)

            running = False

        if pygame.Rect.colliderect(player.rect, fish.rect):

            print(“player got hit by fish”)

            player.Pushed = True

            player.rect.move_ip(fish.speed*2, 0)

        elif pygame.Rect.colliderect(player.rect, jellyfish.rect) or pygame.Rect.colliderect(player.rect, jellyfish2.rect) or pygame.Rect.colliderect(player.rect, jellyfish3.rect):

            player.Pushed = True

            player.rect.move_ip(0, jellyfish.speed)

        else:

            player.Pushed = False

        if pygame.Rect.colliderect(player.rect, wall1.rect):

            player.Pushed = True

            player.rect.move_ip(player.bounciness, 0)

        elif pygame.Rect.colliderect(player.rect, wall2.rect):

            player.Pushed = True

            player.rect.move_ip(-player.bounciness, 0)

#putting the score on the screen, score increases by 1 every frame.

         score += 1

        text = scoreText.render(“Score: ” + str(score), True, “green”)

        window.blit(text, (600, 200))

        pygame.display.flip()

        clock.tick(10)

Hope this is useful, thank you.

You may like to read: how Minecraft teaches kids to code!, Flappy Bird Game in Scratch Programming, & IQ Tests for Kids

BOOK A FREE TRIAL