PyGame Zero is a wrapper around PyGame so you don’t have to do quite so much work. PyGame Zero objects are PyGame objects. PyGame Zero defines a default game loop with some standard handlers and lets you get on with the game logic. Once you outgrow its simplicity you can easily switch to “proper” PyGame.
When you run an empty PyGame Zero file with the
pgzrun command, this code (roughly)
is already running for you behind the scenes:
import pygame pygame.init() size = 800, 600 screen = pygame.display.set_mode(size) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q and \ event.mod & (pygame.KMOD_CTRL | pygame.KMOD_META): sys.exit() screen.fill(pygame.Color("black")) pygame.display.flip() clock.tick(60)
PyGame Zero defines a few constants (which much be spelt exactly as they are below) which have a special meaning.
WIDTHdetermines how wide the game window is
HEIGHTdetermines how high the game window is
TITLEis the text which will be in the titlebar of the game window
ICONis the path to an image file which will be used as the icon for the game window
PyGame Zero defines a few functions which you can write and which are called in a particular way as part of the game loop.
In this function you should perform any work which involves changing the positions, sizes, images &c. of objects which will be drawn in the draw function.
Do not draw/blit anything to the screen here: that should be done in the
Draw whatever’s needed to the screen.
Do not do calculations here: those should be done in the
Called when a mouse button is pressed. Provides pos as x, y and the number of the button pressed
Called when a mouse button is released. Provides pos as x, y and the number of the button pressed
on_mouse_move(pos, rel, buttons)¶
Called when the mouse is moved Provides pos as x, y; rel as the relative movement dx, dy; and buttons which were pressed
Called when a key is pressed Provides: key as a PyGame integer; mod as a bitmask of modifiers
Called when a key is released Provides: key as a PyGame integer; mod as a bitmask of modifiers
Colours can be any of:
- An (R, G, B) sequence, eg
(255, 0, 0)for solid red
- An HTML colour name, eg
- An HTML hex colour string, eg