Creating a Simple Game with Pygame Zero


Game Overview

In this lesson, we will create a simple game using Pygame Zero. The player will control a character that needs to catch falling objects. The game will keep track of the player's score and the number of missed objects. The game will end when the player misses three objects.

Set up an image folder

This game uses images of a box and an obstacle. In the upper left corner of the screen, click the folder icon with a plus sign and name the folder images.

Put the images into the folder

Download the character image HERE, object image HERE, and background image HERE Click the three dots and select Upload file. Then, select person.png, person2.png, background.png from the files.

Drag person.png and person2.png into the images folder. Your folders should look something like this example.

Setting up the Game Window

First, let's import the required modules and set up the game window dimensions.


    import pgzrun
    import random
    
    WIDTH = 800
    HEIGHT = 600
                

Creating the Player and Objects

Next, we will create the player character and position it at the bottom center of the screen. We will also create a list of objects that will fall from the top of the screen.


    player = Actor("character")
    player.pos = WIDTH // 2, HEIGHT - 90
    
    objects = []
    for _ in range(5):
        obj = Actor("object")
        obj.pos = random.randint(0, WIDTH), random.randint(-100, -10)
        objects.append(obj)
                

Creating the Background

We will create a background actor and position it at the center of the screen.


    background = Actor("background", (WIDTH // 2, HEIGHT // 2))
                

Setting up the Score and Missed Counters

We will create two variables to keep track of the player's score and the number of missed objects.


    score = 0
    missed = 0
                

Define the draw() Function

The draw() function will display the game elements on the screen.


    def draw():
    

First, we will clear the screen.

    
        screen.clear()
    

Draw the background, player, and each falling object.

    
        background.draw()
        player.draw()
        for obj in objects:
            obj.draw()
    

Display the score and the number of missed objects.

    
        screen.draw.text("Score: " + str(score), (10, 10), fontsize=30, color="white")
        screen.draw.text("Missed: " + str(missed), (10, 50), fontsize=30, color="white")
    

If the player misses 3 objects, we will display a "Game Over!" message.

    
        if missed >= 3:
            screen.draw.text("Game Over!",(WIDTH // 2 - 100, HEIGHT // 2),fontsize=60,color="red")
    

Define the update() Function

The update() function handles the game logic.


    def update():
    

Access global variables score and missed. Check if the player misses less than 3 objects.


        global score, missed    
        if missed < 3:
    

Move the player left or right based on keyboard input.

        
            if keyboard.left and player.left > 0:
                player.x -= 8
            if keyboard.right and player.right < WIDTH:
                player.x += 8
    

Move each falling object down.

        
            for obj in objects:
                obj.y += 3
    

Check if an object reaches the bottom of the screen. If so, reset its position to the top and increment the number of missed objects.

            
            if obj.top > HEIGHT:
                obj.pos = random.randint(0, WIDTH), random.randint(-100, -10)
                missed += 1
    

Check if the player collides with an object. If so, reset the object's position and increment the player's score.

            
            if player.colliderect(obj):
                obj.pos = random.randint(0, WIDTH), random.randint(-100, -10)
                score += 1
    

Starting the Game

Finally, we will call pgzrun.go() to start the game. Make sure it's always the last line of code in your program.


    pgzrun.go()
                

Copyright © KX Technology Group, LLC. All Rights Reserved.