6. Improving your games

Here are some tips that will enable you to enhance your games.

6.1. Joystick input

You may call them gamepads or controllers, but Pygame calls them joysticks.

Some controllers have different inputs and some are not compatible at all so don’t be surprised if this doesnt work properly! PS4 and Xbox One controllers connected by USB cable seems to work best. Use Program 12.3 to test yours and find out what inputs it has. Note: if you run this program with no controller plugged in you will get an error.

Program 6.1 Joystick input
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import pygame
WIDTH = 500
HEIGHT = 500

joystick = pygame.joystick.Joystick(0)
joystick.init()

alien = Actor("alien")
alien.pos = (0, 50)

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

def update():
    if keyboard.right or joystick.get_axis(0) > 0.1:
        alien.x = alien.x + 2
    elif keyboard.left or joystick.get_axis(0) < -0.1:
        alien.x = alien.x - 2

Optional, if you have a controller

Make the alien move up and down as well as left and right using the controller. Do the same for any other examples that use the keyboard!

6.2. Colours

Note that sometimes colour is spelt color in American programs.

Instead of using ready made colours like ‘red’, ‘yellow’, etc. you can create your own color with a tuple of three numbers. The numbers must be between 0 - 255 and represent how much red, green and blue to mix together.

Program 6.2 RGB colours
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# my_colour = (0,0,0) # makes black
# my_colour = (255,255,255) # makes white

red_amount = 0
green_amount = 0
blue_amount = 0

def draw():
    my_colour = (red_amount, green_amount, blue_amount)
    screen.fill(my_colour)

# This function makes the colour change every frame
# Remove it if you just want to see a static colour.
def update():
    global red_amount
    red_amount += 1
    red_amount = red_amount % 255

Advanced

Change the green and blue amounts to make different colours.

Exercise

Make a gray colour.

Advanced

Make random colours.

6.3. Using loops

The loops from code-loop are useful for graphical games too! Here we draw red circles using a for loop.

We draw green circles using a loop within another loop.

Program 6.3 Loops are useful
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
WIDTH = 500
HEIGHT = 500

def draw():
    screen.clear()
    for x in range(0, WIDTH, 40):
        screen.draw.filled_circle((x, 20), 20, "red")

    for x in range(0, WIDTH, 40):
        for y in range(60, HEIGHT, 40):
            screen.draw.filled_circle((x, y), 10, "green")

Exercise

import random at the top of the program and then make the positions random, e.g:

x = random.randint(0, 100)

Advanced

Learn about RGB colour and make random colours (see Program 6.2).

Advanced

Create a timer variable and change colours based on time (see Program 7.5)

6.4. Fullscreen mode

Sometimes it is nice to play your game using the entire screen rather than in a window. Add these lines to any game to enable fullscreen mode. Then press F to go fullscreen, ESCAPE to quit.

Program 6.4 Fullscreen mode
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pygame

WIDTH = 500
HEIGHT = 500

alien = Actor("alien")

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

def update():
    if keyboard.f:
        pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
    if keyboard.escape:
        exit()

6.5. Displaying the score

This game shows how you can keep the score in a variable and print it on to the game screen. You can display any other messages to the player the same way.

Program 6.5 Keeping score in a variable and displaying it
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
WIDTH = 500
HEIGHT = 500

score = 0

def draw():
    screen.clear()
    screen.draw.text(f"Player 1 score: {score}", (0, 0))
    screen.draw.textbox("Hello mum", Rect(50, 50, 200, 200))

# This is another special function that is called by Pygame automatically
# each time a key is pressed. That way player cannot just hold down key!

def on_key_down(key):
    global score
    if key == keys.SPACE:
        score += 1

Exercise

Make the score text larger.

Advanced

Add a second player who presses a different key and show their score too.

Exercise

Add text to one of your other games.