8. Tutorial: Chase game
In this chapter we will build a complete chase game together, step by step. The Python we will use is very simple: just conditionals and loops.
The techniques here should be familiar to you because we used them in Program 4.5, Program 4.6, Program 5.1 and Program 5.2
Now we will show you how to put them all together in one program.
8.1. Moving a Sprite over a background
We must create two image files for our game. You can use a program such as Krita 1 to draw them and save them in the same directory as the Python script.
One is the player,
called player.png
. It should be small, about 64×64 pixels. Ideally
it should have a transparent background.
The other is the background for the game itself. It can look like
whatever you want, but it must be the same size as the game window,
which will be 600×600 pixels. Save it as background.png
1from rlzero import *
2
3WIDTH = 600
4HEIGHT = 600
5
6background = Sprite("background")
7player = Sprite("player")
8player.x = 200
9player.y = 200
10
11def draw():
12 clear()
13 background.draw()
14 player.draw()
15
16def update():
17 if keyboard.right:
18 player.x = player.x + 4
19 if keyboard.left:
20 player.x = player.x - 4
21 if keyboard.down:
22 player.y = player.y + 4
23 if keyboard.up:
24 player.y = player.y - 4
25
26run()
Exercise
Run the program and move the Sprite around with the keys.
8.2. Screen wrap-around
One problem you will soon find with the program is that you can move off
the edge of the screen and get lost. One way to solve this would be to
stop movement at the screen edges. Instead we going to make the player
teleport to the opposite edge when he leaves the screen. Add this code
to the end of the program, and make sure it is indented so that it
becomes part of the update()
function.
if player.x > WIDTH:
player.x = 0
if player.x < 0:
player.x = WIDTH
if player.y < 0:
player.y = HEIGHT
if player.y > HEIGHT:
player.y = 0
Exercise
Change the code so that the player stops at the edges rather than wraps-around.
8.3. Enemy chases the player
Let’s add an enemy to chase the player. At the top of the program, create a variable to store the enemy Actor:
enemy = Sprite("alien")
At the end of the draw()
function (but still indented so part of the
function), draw the enemy. Remember there is only ever one single
draw()
function. No program has two. All drawing goes inside this
function.
Here is the complete function including the new line at the end:
def draw():
clear()
background.draw()
player.draw()
enemy.draw()
At the end of the update()
function (but still indented so part of
the function), add these lines to move the enemy:
if enemy.x < player.x:
enemy.x = enemy.x + 1
if enemy.x > player.x:
enemy.x = enemy.x - 1
if enemy.y < player.y:
enemy.y = enemy.y + 1
if enemy.y > player.y:
enemy.y = enemy.y - 1
if player.colliderect(enemy):
exit()
Exercise
Run the program verify the enemy chases the player.
Exercise
Make the enemy faster so the game is more difficult.
Exercise
Create an image file enemy.png and save it in the program folder. Change the code so it loads “enemy” instead of “alien”.
8.4. Collecting items
Create a small image file coin.png
and save it in the program
folder. It should look like a coin or something else you would like to
collect. We will also need a variable to store the score, i.e. number
of coins collected.
coin = Sprite("coin", pos=(300,300))
score = 0
At the end of the draw()
function (but still indented so part of the
function), draw the coin. Remember there is only ever one single
draw()
function. No program has two. All drawing goes inside this
function.
Here is the complete function including the new line at the end:
def draw():
clear()
background.draw()
player.draw()
enemy.draw()
coin.draw()
At the end of the update()
function (but still indented so part of
the function), add these lines to move the coin when it is collected:
if coin.colliderect(player):
coin.x = random.randint(0, WIDTH)
coin.y = random.randint(0, HEIGHT)
score = score + 1
print("Score:", score)
Exercise
Run the program and collect a coin. What happens?
NameError: name 'random' is not defined
This happens because we are using a function randint()
to get a
random number. This function is not build-in to Python; it is part of
the random library. So at the top of the program, add the first line:
import random
Exercise
Run the program again and collect a coin. Does it work now?
No!
UnboundLocalError: local variable 'score' referenced before assignment
You will get an error because score
is a global variable and we are
trying to modify it inside the update()
function. Therefore at the
top of the update()
function, add this line to declare to Python
our intention to modify a global variable:
global score
It must be the first line in the function and it must be indented. The lines surrounding it should look like this:
def update():
global score
if keyboard.right:
Exercise
Run the program again and verify it works!
8.5. Player 2
We can make any game into a two player game. At the top of the program, create a variable to store the Actor for the second player:
player2 = Sprite("alien")
At the end of the draw()
function (but still indented so part of the
function), draw the enemy. Here is the complete function with the new
line at the end:
def draw():
clear()
background.draw()
player.draw()
enemy.draw()
coin.draw()
player2.draw()
At the end of the update()
function (but still indented so part of
the function), add these lines to move the second player:
if keyboard.d:
player2.x = player2.x + 4
if keyboard.a:
player2.x = player2.x - 4
if keyboard.s:
player2.y = player2.y + 4
if keyboard.w:
player2.y = player2.y - 4
if player.colliderect(player2):
exit()
Advanced
Create a variable score2 and store the score for player two, i.e. it goes up when he collides with a coin.
8.6. Showing the score on the screen
At the end of the draw()
function (but still indented so part of the
function), draw a title on the screen:
draw_text("My game", 200, 0, 20, RED)
The draw.text()
function is not like print()
- it can only print
strings of text, not numbers. Therefore we must convert the score into a
string. Add these lines to the end of the draw()
function:
score_string = str(score)
draw_text(score_string, 0, 0, 20, GREEN)
Exercise
Change the colour of the text.
Advanced
Display the word “Score: ” before the score.
Advanced
When the score reaches 10, show a message on the screen to congratulate the player.
8.7. Timer
Add a variable at the top of the program (but preferably after any
import
statements) to store the number of seconds of time remaining
in the game:
time = 20
RLZero calls our update()
function many times per second. We
can ask it to tell us how much time has passed by adding a parameter
to the function, delta
. We then subtract this from the remaining
time. Modify update()
so the first lines look like this:
def update(delta):
global score, time
time = time - delta
if time <= 0:
exit()
We can also display the time on the screen. At the end of the draw()
function (but still indented so part of the function) add these lines:
time_string = str(time)
draw_text(time_string, 50, 0, 20, GREEN)
Exercise
Run the program. Could the displayed time be improved (maybe you could use the round()
function somehow?)
We don’t need to see the decimal places in the time. Modify those lines
to use the round()
function, like this:
time_string = str(round(time))
draw_text(time_string, 50, 0, 20, GREEN)
8.8. Finished game
Here is the finished game with all the changes included:
1import random
2from rlzero import *
3
4WIDTH = 600
5HEIGHT = 600
6
7background = Sprite("background")
8player = Sprite("player")
9player.x = 200
10player.y = 200
11
12enemy = Sprite("alien")
13player2 = Sprite("player")
14coin = Sprite("coin", pos=(300,300))
15score = 0
16time = 20
17
18
19def draw2d():
20 clear()
21 background.draw()
22 player.draw()
23 enemy.draw()
24 player2.draw()
25 coin.draw()
26 draw_text("My game", 200, 0, 20, RED)
27 score_string = str(score)
28 draw_text(score_string, 0,0, 20, GREEN)
29 time_string = str(round(time))
30 draw_text(time_string, 50,0, 20, GREEN)
31
32def update(delta):
33 global score, time
34 time = time - delta
35 if time <= 0:
36 print("time out")
37 exit()
38 if keyboard.right:
39 player.x = player.x + 4
40 if keyboard.left:
41 player.x = player.x - 4
42 if keyboard.down:
43 player.y = player.y + 4
44 if keyboard.up:
45 player.y = player.y - 4
46
47 if player.x > WIDTH:
48 player.x = 0
49 if player.x < 0:
50 player.x = WIDTH
51 if player.y < 0:
52 player.y = HEIGHT
53 if player.y > HEIGHT:
54 player.y = 0
55
56 if enemy.x < player.x:
57 enemy.x = enemy.x + 1
58 if enemy.x > player.x:
59 enemy.x = enemy.x - 1
60 if enemy.y < player.y:
61 enemy.y = enemy.y + 1
62 if enemy.y > player.y:
63 enemy.y = enemy.y - 1
64 if player.colliderect(enemy):
65 print("player 1 hit enemy")
66 exit()
67
68 if keyboard.d:
69 player2.x = player2.x + 4
70 if keyboard.a:
71 player2.x = player2.x - 4
72 if keyboard.s:
73 player2.y = player2.y + 4
74 if keyboard.w:
75 player2.y = player2.y - 4
76 if player.colliderect(player2):
77 print("player1 hit player 2")
78 exit()
79
80 if coin.colliderect(player):
81 coin.x = random.randint(0, WIDTH)
82 coin.y = random.randint(0, HEIGHT)
83 score = score + 1
84 print("Score:", score)
85
86
87run()
8.9. Ideas for extension
There are many things you could add to this game.
Add more enemies.
Give the player three lives.
Add music and sound effects.
Create a powerup that makes the player move faster.
Make the enemies more intelligent.
Allow the player to kill the enemies.