11. Tutorial: Race game
In this chapter we will build a racing game together, step by step. The Python we will use is: conditionals, loops, lists, functions and tuples. We will show use of velocity, high score and a title screen.
11.1. Basic game
Similar to the shooter game, we will begin with a complete program
listing but with empty bodies for some of the functions that we will
fill in later. (Python will not run a program with completely empty
functions, so they just contain pass
to indicate to Python they do
nothing.)
Like the shooter program, we begin we three things:
Definitions of global variables.
A
draw()
function.An
update()
function.
These functions now check a boolean variable playing
. If False
then instead of drawing/updating the game we show the title screen.
The only really complicated part of this program is how we store the
shape of the tunnel the player is racing down. lines
is a list of
tuples. A tuple is like a list but cannot be modified and can be
unpacked into separate variables. Each tuple will represent one
horizontal line of the screen. It will have three values, x
, x2
and color
, representing the position of the left wall, the gap
between the left wall and the right wall and the colour of the wall.
1from rlzero import *
2import random
3import math
4
5WIDTH = 600
6HEIGHT = 800
7
8player = Sprite("alien.png", (300, 750))
9player.vx = 0 # horizontal velocity
10player.vy = 1 # vertical velocity
11
12lines = [] # list of tuples of horizontal lines of walls
13wall_gradient = -3 # steepness of wall
14left_wall_x = 200 # x-coordinate of wall
15distance = 0 # how far player has travelled
16time = 15 # time left until game ends
17playing = False # True when in game, False when on title screen
18best_distance = 0 # remember the highest distance scored
19
20def draw():
21 if playing: # we are in game
22 for i in range(0, len(lines)): # draw the walls
23 x, x2, color = lines[i]
24 draw_line(0, i, int(x), i, color)
25 draw_line(int(x + x2), i, WIDTH, i, color)
26 player.draw()
27 else: # we are on title screen
28 draw_text("PRESS SPACE TO START", 150, 300, 30, GREEN)
29 draw_text("BEST DISTANCE: "+str(int(best_distance / 10)),
30 170, 400, 30, GREEN)
31 draw_text("SPEED: " + str(int(player.vy)),
32 0, 0, 30, GREEN)
33 draw_text("DISTANCE: " + str(int(distance / 10)),
34 200, 0, 30, GREEN)
35 draw_text("TIME: " + str(int(time)),
36 480, 0, 30, GREEN)
37
38
39def update(delta):
40 global playing, distance, time
41 if playing:
42 wall_collisions()
43 scroll_walls()
44 generate_lines()
45 player_input()
46 timer(delta)
47 elif keyboard.space:
48 playing = True
49 distance = 0
50 time = 10
51
52
53def player_input():
54 pass
55
56def generate_lines():
57 pass
58
59generate_lines()
60
61def scroll_walls():
62 pass
63
64def wall_collisions():
65 pass
66
67def timer(delta):
68 pass
69
70def on_mouse_move(pos):
71 pass
72
73run()
Exercise
Run the program. Verify it has a title screen and you can start the game and see the player. (That is all it will do until we fill in the remaining functions.)
11.2. Player input
Replace the definiton of player_input()
with this:
def player_input():
if keyboard.up:
player.vy += 0.1
if keyboard.down:
player.vy -= 0.1
if player.vy < 1:
player.vy = 1
if keyboard.right:
player.vx += 0.4
if keyboard.left:
player.vx -= 0.4
player.x += player.vx
Exercise
Run the program. Verify the player can move left and right and has momentum. Try adjusting the speed or making a limit so you can’t go too fast.
11.3. Generate the walls
We already have code to draw the walls, but currently lines
is empty
so nothing gets drawn. Replace the function generate_lines()
with
this. Note that we immediately call generate_lines()
after defining
it to generate the walls for the start of the game.
def generate_lines():
global wall_gradient, left_wall_x
gap_width = 300 + math.sin(distance / 3000) * 100
while len(lines) < HEIGHT:
pretty_colour = (255, 0, 0)
lines.insert(0, (left_wall_x, gap_width, pretty_colour))
left_wall_x += wall_gradient
if left_wall_x < 0:
left_wall_x = 0
wall_gradient = random.random() * 2 + 0.1
elif left_wall_x + gap_width > WIDTH:
left_wall_x = WIDTH - gap_width
wall_gradient = -random.random() * 2 - 0.1
generate_lines()
Advanced
Run the program. Change the colour of the walls from red to green.
11.4. Make the walls colourful
Modify the line that sets the colour of the generated line to this:
pretty_colour = (255, min(left_wall_x, 255), min(time * 20, 255))
11.5. Scrolling
Modify the scroll_walls()
function so it removes lines from the
bottom of the screen according to the player’s vertical velocity.
def scroll_walls():
global distance
for i in range(0, int(player.vy)):
lines.pop()
distance += 1
Exercise
Modify scroll_walls() as above and check that the player can now accelerate forward.
Advanced
Change the amount of the forward acceleration to make the game faster or slower.
11.6. Wall collisions
Currently the player can move through the walls - we don’t want to allow this. Also we want the player to lose all their velocity each time they collide as a penalty.
def wall_collisions():
a, b, c = lines[-1]
if player.x < a:
player.x += 5
player.vx = player.vx * -0.5
player.vy = 0
if player.x + 50 > a + b:
player.x -= 5
player.vx = player.vx * -0.5
player.vy = 0
Exercise
Modify wall_collisions() as above and check that the player now bounces off the walls.
Advanced
Make the collision more bouncy, i.e. the player bounces further when he hits the wall.
11.7. Timer
Currently the player has infinite time. We want decrease the time
variable by how much time has passed and end the game when time runs
out.
def timer(delta):
global time, playing, best_distance
time -= delta
if time < 0:
playing = False
if distance > best_distance:
best_distance = distance
Exercise
Modify the timer() function as above. Verify the game ends after 15 seconds.
Exercise
Make the game last for 30 seconds.
11.8. Mouse movement
The game is easier but perhaps more fun if you can play it with mouse. Pygame will call this function for us automatically.
def on_mouse_move(pos):
x, y = pos
player.x = x
player.vy = (HEIGHT - y) / 20
Exercise
Modify the on_mouse_move() function as above. How does the player accelerate using the mouse?
11.9. Ideas for extension
Draw a new image for the player. Make the Actor show a different image depending on if the player is steering left or right.
Give the player a goal distance that must be reached. If the player reaches this distance he gets extra time added to allow him to continue.
Add sound effects and music.
If you have a larger screen, make the game window taller (and make sure the alien appears at the bottom still).