13. Advanced topics

13.1. Instructor note

This introduces object oriented programming, but I wouldn’t attempt this with young students since it requires abstract thinking.

13.2. Classes

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 8.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.

Program 13.1 Classes
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
WIDTH = 500
HEIGHT = 500

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

ball = Sprite('alien')

def draw():
    screen.clear()
    ball.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 ball.top < 0:
        ball.vy = -ball.vy

13.3. Methods

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

Program 13.2 Class methods
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 self.top < 0:
            self.vy = -self.vy

ball = Sprite("alien")

def draw():
    screen.clear()
    ball.draw()

def update():
    ball.update()

13.4. 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.)

Program 13.3 Joystick tester
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pygame

def update():
    screen.clear()
    joystick_count = pygame.joystick.get_count()
    y = 0
    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()
        name = joystick.get_name()
        axes = joystick.get_numaxes()
        buttons = joystick.get_numbuttons()
        hats = joystick.get_numhats()
        screen.draw.text(
            "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

13.5. 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 itch.io for people to buy.

  1. Install the full version of python from www.python.org.

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

    import pgzrun
    

    At the bottom of the file add the line:

    pgzrun.go()
    

    Save the file.

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

    C:\Users\YourName>
    

    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.

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

    pip install nuitka pgzero
    
  3. 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/MY_GAME.py
    

    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.

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