Making a game with pygame zero
Pygame zero is a very interesting project that allows to use the pygame module in an easy way, so that you can concentrate on the logic of the game, instead of a long learning process that may discourage the users from creating a game. It’s a good tool to introduce to computer logic, I think, through games, but it is also very powerful, as you will check by yourself, looking at the examples of games made with Pygame zero.
In this video I will tell you how to start making a game with pygame zero. First you have to install the modules and then with a couple of line you can see something moving on the screen. Really nice way to start learning how to make a videogame.
How to install pgzero
To install pygame zero or pgzero, in the cmd (windows button + r and then ‘cmd’) write:
pip install pgzero
This way you will install also pygame and other modules that pgzero uses.
To run the code you have to write:
pgzrun intro.py
where intro.py is the name of the file you created.
Run the code from Sublime or IDLE
If you want to start the program from an IDE (or from sublimeRepl) you have to import pgzrun as the first line of code and write at the last line of code pgzrun.go() like in the following example.
# this can run using python filename.py import pgzrun WIDTH = HEIGHT = 300 def draw(): screen.clear() screen.draw.circle((WIDTH//2,HEIGHT//2), 30, 'white') pgzrun.go()
Pygame zero: how to make a window (previous post about this topic)
Move that circle
We could make that circle move… using pygame too, why not? Here’s the code where we can move the circle moving the mouse… just a couple of lines and the use of
- pygame.mouse.get_pos()
- to get x and y position of the mouse
- and the use them to move the center of the circle making it follow the mouse
- pygame.mouse.set_visible(False)
- to hide the mouse curso (if you want)
Remember you have to import pygame.
# this can run using python filename.py import pygame import pgzrun from collections import namedtuple Size = namedtuple("Size", "x y") size = Size(600,400) def update(): screen.clear() pygame.mouse.set_visible(False) x, y = pygame.mouse.get_pos() screen.draw.circle((x,y), 30, "yellow") pgzrun.go()
Video nr. 1: make an alien move
Let’s start making a sprite move on the screen
import pgzrun alien = Actor('alien') alien.topright = 0, 10 WIDTH = 500 HEIGHT = alien.height + 20 def draw(): screen.clear() alien.draw() def update(): alien.left += 2 if alien.left > WIDTH: alien.right = 0 pgzrun.go()
Video nr. 2: recap
Let’s recap what we’ve done.
Video nr. 3: adding interaction
When we click on the alien we will get a +1, when we click on the ‘space’ we get a -1.
# this runs with python filename.py as usual or inside sublime text import pgzrun alien = Actor('alien') alien.topright = 0, 10 WIDTH = 500 HEIGHT = alien.height + 20 def draw(): screen.clear() alien.draw() def update(): alien.left += 2 if alien.left > WIDTH: alien.right = 0 score = 0 def on_mouse_down(pos): global score if alien.collidepoint(pos): score += 1 else: score -= 1 print("Nothing here") print(score) pgzrun.go()
Video nr. 4: adding animation
We add a an animation when we hit the alien.
# runs with: python filename.py import pgzrun alien = Actor('alien') alien.topright = 0, 10 WIDTH = 500 HEIGHT = alien.height + 20 def draw(): screen.clear() alien.draw() def update(): alien.left += 2 if alien.left > WIDTH: alien.right = 0 score = 0 def on_mouse_down(pos): global score if alien.collidepoint(pos): sounds.eep.play() alien.image = 'alien_hurt' score += 1 else: score -= 1 print("Nothing here") print(score) pgzrun.go()
Video nr. 5: perfecting the animation
Here we make a better animation using the clock…
# python filename.py import pgzrun alien = Actor('alien') alien.topright = 0, 10 WIDTH = 500 HEIGHT = alien.height + 20 def draw(): screen.clear() alien.draw() def update(): alien.left += 2 if alien.left > WIDTH: alien.right = 0 score = 0 def on_mouse_down(pos): global score if alien.collidepoint(pos): set_alien_hit() score += 1 print(score) else: score -= 1 print(score) def set_alien_hit(): alien.image = 'alien_hurt' sounds.eep.play() clock.schedule_unique(set_alien_normal, 0.5) def set_alien_normal(): alien.image = "alien" pgzrun.go()
The repository of Pygame zero
If you want to take a look at the code and to many examples of pygame zero go and download the repository here:
https://github.com/lordmauve/pgzero
An entire videogame with pythonista on the ipad
You may be interested in making a game with python on the Ipad. Go check this series of posts and videos I made recently (2019).
My Playlist n.1 on Youtube about Pythonista game on the Ipad
My Playlist n.2 on Youtube about Pythonista game on the Ipad (in progress)
My playlist on last pygame videos