Books / Coding Games With Pygame Zero & Python / Chapter 6
Improving your games
In this chapter
Here are some tips that will enable you to enhance your games.
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 doesn’t 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.
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
Program 6.1 Joystick input
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!
Colors
Instead of using ready made colors 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.
# my_colors = (0,0,0) # makes black
# my_colors = (255,255,255) # makes white
red_amount = 0
green_amount = 0
blue_amount = 0
def draw():
my_colors = (red_amount, green_amount, blue_amount)
screen.fill(my_colors)
# This function makes the colors change every frame
# Remove it if you just want to see a static colors.
def update():
global red_amount
red_amount += 1
red_amount = red_amount % 255
Program 6.2 RGB colors
Advanced
Change the green and blue amounts to make different colors.
Exercise
Make a gray colors.
Advanced
Make random colors.
Using loops
The loops from code-loop
{.interpreted-text role=”numref”} 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.
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")
Program 6.3 Loops are useful
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 colors and make random colors (see Program 6.2).
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.
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()
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.
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
Program 6.5 Keeping score in a variable and displaying it
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.