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 7.4 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
 1from rlzero import *
 2
 3
 4WIDTH = 500
 5HEIGHT = 500
 6
 7class MySprite(Sprite):
 8    vx = 1
 9    vy = 1
10
11ball = MySprite('alien.png')
12
13def draw():
14    clear()
15    ball.draw()
16
17def update():
18    ball.x += ball.vx
19    ball.y += ball.vy
20    if ball.x > WIDTH or ball.x < 0:
21        ball.vx = -ball.vx
22    if ball.y > HEIGHT or ball.y < 0:
23        ball.vy = -ball.vy
24
25
26run()

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
 1from rlzero import *
 2
 3WIDTH = 500
 4HEIGHT = 500
 5
 6class MySprite(Sprite):
 7    vx = 1
 8    vy = 1
 9
10    def update(self):
11        self.x += self.vx
12        self.y += self.vy
13        if self.x > WIDTH or self.x < 0:
14            self.vx = -self.vx
15        if self.y > HEIGHT or self.y < 0:
16            self.vy = -self.vy
17
18ball = MySprite("alien.png")
19
20def draw():
21    clear()
22    ball.draw()
23
24def update():
25    ball.update()
26
27run()

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
 1import pygame
 2
 3def update():
 4    screen.clear()
 5    joystick_count = pygame.joystick.get_count()
 6    y = 0
 7    for i in range(joystick_count):
 8        joystick = pygame.joystick.Joystick(i)
 9        joystick.init()
10        name = joystick.get_name()
11        axes = joystick.get_numaxes()
12        buttons = joystick.get_numbuttons()
13        hats = joystick.get_numhats()
14        screen.draw.text(
15            "Joystick {} name: {} axes:{} buttons:{} hats:{}".format(
16                i, name, axes, buttons, hats), (0, y))
17        y += 14
18        for i in range(axes):
19            axis = joystick.get_axis(i)
20            screen.draw.text("Axis {} value: {:>6.3f}".format(i, axis),
21                             (20, y))
22            y += 14
23        for i in range(buttons):
24            button = joystick.get_button(i)
25            screen.draw.text("Button {:>2} value: {}".format(i, button),
26                             (20, y))
27            y += 14
28        for i in range(hats):
29            hat = joystick.get_hat(i)
30            screen.draw.text("Hat {} value: {}".format(i, str(hat)),
31                             (20, y))
32            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.