Advanced topics


You’ve already been using class types provided by Pygame Zero, e.g. Rect and Actor. But if we want to store velocity as in Program 7.3 we find these classes do not include vx and vy variables inside them by default. We have to remember to add a vx and vy every time we create an Actor.

So let’s create our own class, called Sprite, that is the same as Actor but with these variables included.

WIDTH = 500
HEIGHT = 500

class Sprite(Actor):
    vx = 1
    vy = 1

ball = Sprite('alien')

def draw():

def update():
    ball.x += ball.vx
    ball.y += ball.vy
    if ball.right > WIDTH or ball.left < 0:
        ball.vx = -ball.vx
    if ball.bottom > HEIGHT or < 0:
        ball.vy = -ball.vy

Program 12.1 Classes


Classes can contain functions (called methods) as well as variables. Methods are the best place to modify the class’s variables.

WIDTH = 500
HEIGHT = 500

class Sprite(Actor):
    vx = 1
    vy = 1

    def update(self):
        self.x += self.vx
        self.y += self.vy
        if self.right > WIDTH or self.left < 0:
            self.vx = -self.vx
        if self.bottom > HEIGHT or < 0:
            self.vy = -self.vy

ball = Sprite("alien")

def draw():

def update():

Program 12.2 Class methods

Joystick tester

This program demonstrates using joysticks and for loops, but is mainly included to help you test the input from your controllers.

(I don’t suggest typing this one yourself.)

import pygame

def update():
    joystick_count = pygame.joystick.get_count()
    y = 0
    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        name = joystick.get_name()
        axes = joystick.get_numaxes()
        buttons = joystick.get_numbuttons()
        hats = joystick.get_numhats()
            "Joystick {} name: {} axes:{} buttons:{} hats:{}".format(
                i, name, axes, buttons, hats), (0, y))
        y += 14
        for i in range(axes):
            axis = joystick.get_axis(i)
            screen.draw.text("Axis {} value: {:>6.3f}".format(i, axis),
                             (20, y))
            y += 14
        for i in range(buttons):
            button = joystick.get_button(i)
            screen.draw.text("Button {:>2} value: {}".format(i, button),
                             (20, y))
            y += 14
        for i in range(hats):
            hat = joystick.get_hat(i)
            screen.draw.text("Hat {} value: {}".format(i, str(hat)),
                             (20, y))
            y += 14

Program 12.3 Joystick tester

Distributing your Pygame Zero games

This is often tricky to get working, but you can distribute your games to people who don’t have Python or Mu installed. You can put them on a USB stick, or a website for people to download, or even on for people to buy.

  1. Install the full version of python from

  2. Edit your game source code (using Mu). We will assume your source is in a file At the top of the file add the line:

    import pgzrun

    At the bottom of the file add the line:


    Save the file.

  3. Open a command shell: Click start menu and type cmd.exe. You should see a prompt similar to this:


    This means you are in the user YourName home directory, with the mu_code sub-directory inside it. If you are in a different directory you will have to change it with the cd command.

  4. Install Nuitka and PGZero. At the command prompt type:

    pip install nuitka pgzero
  5. Create the executable. At the command prompt type this (all one long line):

    python -m nuitka --onefile --include-package-data=pgzero,pygame --include-data-dir=mu_code=. --output-dir=Documents mu_code/

    Remember to replace MY_GAME with the actual name of the Python file. If Nuitka asks for confirmation, type Yes and press enter.

    This will generate a program called MY_GAME.exe in your Documents folder.

  6. In Windows Explorer, double click the MY_GAME.exe icon in Documents to play your game. To distribute your game, copy this file.

Licenses and Attributions

Speak Your Mind