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.
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!
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.
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.
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()
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.
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.
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:
You can find more information about Pygame Zero and its features in the official documentation.
Happy game development!