David's Blog

An Introduction to Pygame Zero Building Your First Game in Python

By David Li on 2023-06-16T23:52:29.000Z

An Introduction to Pygame Zero: Building Your First Game in Python

Pygame Zero is a beginner-friendly game engine for Python that allows you to create simple video games without the need to write complex boilerplate code. This powerful library provides a straightforward interface that abstracts away many of the complexities of Pygame, a popular library for creating multimedia applications in Python. In this article, we will explore the basics of Pygame Zero and guide you through the process of creating a simple game.

Table of Contents

  1. Getting Started with Pygame Zero
  2. Building a Simple Game
  3. Conclusion

Getting Started with Pygame Zero

Before diving into game development, make sure to install Python 3 and Pygame Zero. You can download Python from the official website and install Pygame Zero by running the following command:

pip install pgzero

Note: If you are using Python 3.8 or later, you may need to install the Pygame Zero version that supports the latest Python version with this command:

pip install pgzero[pygame14]

Now that we have everything set up, let’s start creating our game!

Building a Simple Game

In this tutorial, we will create a simple game where the player controls a spaceship that shoots down enemy spaceships. The game will end when an enemy spaceship reaches the bottom of the screen.

Setting Up the Game Window

First, we need to set up the game window. Create a new Python file named spaceshooter.py and add the following code:

import pgzrun

WIDTH = 800
HEIGHT = 600

pgzrun.go()

The WIDTH and HEIGHT variables define the dimensions of the game window, and the pgzrun.go() function starts the game loop.

Sprites and Actors

Next, we will create the spaceship and enemy sprites using the Actor class. Add the following code to your spaceshooter.py file:

player = Actor("spaceship")
player.midbottom = (WIDTH // 2, HEIGHT)

enemies = []

for i in range(5):
    enemy = Actor("enemy")
    enemy.topleft = (i * 150 + 50, 50)
    enemies.append(enemy)

This code creates a spaceship Actor and positions it at the middle bottom of the screen. It also creates a list of enemy Actor objects and positions them horizontally across the screen.

To render these actors, we need to define the draw() function:

def draw():
    screen.clear()
    player.draw()
    for enemy in enemies:
        enemy.draw()

User Input and Movement

Now, let’s add controls to move the spaceship using the arrow keys. Add the following code to your spaceshooter.py file:

def update():
    if keyboard.left:
        player.x -= 5
    if keyboard.right:
        player.x += 5

    player.x = max(player.width // 2, min(player.x, WIDTH - player.width // 2))

The update() function is called every frame, and here we check for left or right arrow key presses to move the spaceship. The last line of code ensures that the spaceship stays within the screen boundaries.

Collisions and Scoring

Finally, let’s add a simple scoring system and detect collisions between the spaceship and enemies. Add the following code to your spaceshooter.py file:

score = 0

def on_key_down(key):
    global score
    if key == keys.SPACE:
        for enemy in enemies:
            if player.colliderect(enemy):
                enemies.remove(enemy)
                score += 1
                break

def update():
    ## ...previous code for player movement...
    for enemy in enemies:
        enemy.y += 1
        if enemy.bottom > HEIGHT:
            print("Game Over!")
            exit(1)

def draw():
    ## ...previous code for drawing actors...
    screen.draw.text(f"Score: {score}", (10, 10), color="white")

The on_key_down() function is called when a key is pressed. Here, we check if the spacebar is pressed and detect collisions between the player and enemies. If a collision occurs, we remove the enemy from the list and increase the score.

In the update() function, we added code to move the enemies downwards. If an enemy reaches the bottom of the screen, the game ends.

Finally, we updated the draw() function to display the player’s score on the screen.

Conclusion

Congratulations! You have just created a simple spaceship shooter game using Pygame Zero. You can now build upon this foundation to create more complex games and explore additional features of the Pygame Zero library.

Here are some ideas to enhance your current game:

  • Add background music and sound effects.
  • Create multiple levels with increasing difficulty.
  • Implement a high score system.
  • Add power-ups and different types of enemies.

You can find more information about Pygame Zero and its features in the official documentation.

Happy game development!

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-15.