9. Tutorial: Maze game

In this chapter we will build a maze game together, step by step. The Python we will use is quite simple: mostly just conditionals and loops. The technique of creating a tilemap is common in games and after seeing it here you should be able to incorporate it into your own projects.

Maze game

Fig. 9.1 Maze game

9.1. Tilemap

A tilemap uses a small number of images (the tiles) and draws them many times to build a much larger game level (the map). This saves you from creating a lot of artwork and makes it very easy to change the design of the level on a whim. Here we create a maze level.

We must create three image files for the tiles: empty.png, wall.png and goal.png and save them in the mu_code/images folder (accessible with the images button in Mu).

They must each be 64×64 pixels. For the player we will use the built in alien image.

Program 9.1 Drawing a tilemap
 1TILE_SIZE = 64
 2WIDTH = TILE_SIZE * 8
 3HEIGHT = TILE_SIZE * 8
 4
 5tiles = ['empty', 'wall', 'goal']
 6
 7maze = [
 8    [1, 1, 1, 1, 1, 1, 1, 1],
 9    [1, 0, 0, 0, 1, 2, 0, 1],
10    [1, 0, 1, 0, 1, 1, 0, 1],
11    [1, 0, 1, 0, 0, 0, 0, 1],
12    [1, 0, 1, 0, 1, 0, 0, 1],
13    [1, 0, 1, 0, 1, 0, 0, 1],
14    [1, 0, 1, 0, 0, 0, 0, 1],
15    [1, 1, 1, 1, 1, 1, 1, 1]
16]
17
18player = Actor("alien", anchor=(0, 0), pos=(1 * TILE_SIZE, 1 * TILE_SIZE))
19
20def draw():
21    screen.clear()
22    for row in range(len(maze)):
23        for column in range(len(maze[row])):
24            x = column * TILE_SIZE
25            y = row * TILE_SIZE
26            tile = tiles[maze[row][column]]
27            screen.blit(tile, (x, y))
28    player.draw()

The filenames of the tile images are stored in a list, tiles. The level design is stored in a list of lists, more commonly called a two dimensional array. There are 8 rows and 8 columns in the array. If you change the size of the array you will need to change the WIDTH and HEIGHT values too. The numbers in the maze array refers to elements in the tiles array. So 0 means empty and 1 means wall, etc.

Tile grid

Fig. 9.2 Tile grid

To draw the maze we use a for loop within another for loop. The outer loop iterates over the rows and the inner loop iterates over the columns, i.e. the elements of the row.

Exercise

Create your own images empty.png, wall.png and goal.png and run the program.

Exercise

The player appears at the top left of the maze at row 1 column 1. Change the pos parameter so he appears at the bottom instead.

Exercise

Change the design of the maze by changing the numbers in the maze array.

Advanced

Make the maze bigger.

9.2. Moving the player

Add this code to the end of the program:

def on_key_down(key):
    row = int(player.y / TILE_SIZE)
    column = int(player.x / TILE_SIZE)
    if key == keys.UP:
        row = row - 1
    if key == keys.DOWN:
        row = row + 1
    if key == keys.LEFT:
        column = column - 1
    if key == keys.RIGHT:
        column = column + 1
    player.x = column * TILE_SIZE
    player.y = row * TILE_SIZE

This function will be called automatically by Pygame, like draw() and update(). However on_key_down() is not called every frame; it is only called when the player presses a key. The key that was pressed is passed to the function in the key parameter.

Exercise

Run the program and move the player. Are there any problems with the movement?

9.3. Restricting where the player can move

Delete the last two lines of the program and replace them with this modified version:

tile = tiles[maze[row][column]]
if tile == 'empty':
    player.x = column * TILE_SIZE
    player.y = row * TILE_SIZE
elif tile == 'goal':
    print("Well done")
    exit()

Exercise

Run the program and check that the player now only moves if the tile is empty.

Exercise

Check that the game ends when the player reaches the goal.

9.4. Animate the movement of the player

First, the alien Actor is bit too big. Draw a new image of size 64×64 pixels and save it as player.png in the images folder. In the program, change the line:

player = Actor("alien", anchor=(0, 0), pos=(1 * TILE_SIZE, 1 * TILE_SIZE))

to

player = Actor("player", anchor=(0, 0), pos=(1 * TILE_SIZE, 1 * TILE_SIZE))

Next, the movement of the Actor is sudden and jerky. Luckily Pygame includes a function to do smooth movement for us automatically. Find these lines of the program:

if tile == 'empty':
      player.x = column * TILE_SIZE
      player.y = row * TILE_SIZE

replace them with:

if tile == 'empty':
    x = column * TILE_SIZE
    y = row * TILE_SIZE
    animate(player, duration=0.1, pos=(x, y))

Exercise

Verify the player image has changed and moves smoothly.

9.5. Create an enemy

We will create a simple enemy that moves up and down. Add this code near the top just above the draw() function.

enemy = Actor("enemy", anchor=(0, 0), pos=(3 * TILE_SIZE, 6 * TILE_SIZE))
enemy.yv = -1

To make the enemy visible, add this line at the end of the draw() function, after the player is drawn:

enemy.draw()

enemy.yv is the velocity in the y-axis direction (up and down). Add these lines to end of the program (still inside the on_key_down() function) to make the enemy move and reverse velocity when it hits a wall.

# enemy movement
row = int(enemy.y / TILE_SIZE)
column = int(enemy.x / TILE_SIZE)
row = row + enemy.yv
tile = tiles[maze[row][column]]
if not tile == 'wall':
    x = column * TILE_SIZE
    y = row * TILE_SIZE
    animate(enemy, duration=0.1, pos=(x, y))
else:
    enemy.yv = enemy.yv * -1
if enemy.colliderect(player):
    print("You died")
    exit()

Exercise

Verify that the enemy moves up and down and kills the player.

Advanced

Make another enemy that moves horizontally (left and right).

Advanced

The collision detection is quite lenient (i.e. buggy) because it only tests for collisions between the enemy and player when a key is pressed. Define a new function called update() and move the collisions detection there so that is called every frame.

9.6. A locked door and a key

We will add two new tiles to the game. Draw images door.png and key.png and save them in images folder.

Find the tiles list near the top and change it to include the new images, and modify the maze with some number 3s and 4s where you want to new tiles to appear. Mine looks like this:

tiles = ['empty', 'wall', 'goal', 'door', 'key']
unlock = 0

maze = [
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 1, 2, 0, 1],
    [1, 0, 1, 0, 1, 1, 3, 1],
    [1, 0, 1, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 0, 1],
    [1, 0, 1, 4, 1, 0, 0, 1],
    [1, 0, 1, 0, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1, 1]
]

At the top of the program, create a new variable to store the number of keys the player is carrying:

unlock = 0

Find the if statement where we test for goal:

if tile == 'goal':
     print("Well done")
     exit()

Modify it like this to also test for the key and door tiles. Since we are modifying a global variable inside a function we must declare it.

global unlock
if tile == 'goal':
    print("Well done")
    exit()
elif tile == 'key':
    unlock = unlock + 1
    maze[row][column] = 0 # 0 is 'empty' tile
elif tile == 'door' and unlock > 0:
    unlock = unlock - 1
    maze[row][column] = 0 # 0 is 'empty' tile

9.7. Finished game

Here is the finished game with all the changes included:

Program 9.2 Finished maze game
 1TILE_SIZE = 64
 2WIDTH = TILE_SIZE * 8
 3HEIGHT = TILE_SIZE * 8
 4
 5tiles = ['empty', 'wall', 'goal', 'door', 'key']
 6unlock = 0
 7
 8maze = [
 9    [1, 1, 1, 1, 1, 1, 1, 1],
10    [1, 0, 0, 0, 1, 2, 0, 1],
11    [1, 0, 1, 0, 1, 1, 3, 1],
12    [1, 0, 1, 0, 0, 0, 0, 1],
13    [1, 0, 1, 0, 1, 0, 0, 1],
14    [1, 0, 1, 4, 1, 0, 0, 1],
15    [1, 0, 1, 0, 0, 0, 0, 1],
16    [1, 1, 1, 1, 1, 1, 1, 1]
17]
18
19player = Actor("player", anchor=(0, 0), pos=(1 * TILE_SIZE, 1 * TILE_SIZE))
20enemy = Actor("enemy", anchor=(0, 0), pos=(3 * TILE_SIZE, 6 * TILE_SIZE))
21enemy.yv = -1
22
23def draw():
24    screen.clear()
25    for row in range(len(maze)):
26        for column in range(len(maze[row])):
27            x = column * TILE_SIZE
28            y = row * TILE_SIZE
29            tile = tiles[maze[row][column]]
30            screen.blit(tile, (x, y))
31    player.draw()
32    enemy.draw()
33
34def on_key_down(key):
35    # player movement
36    row = int(player.y / TILE_SIZE)
37    column = int(player.x / TILE_SIZE)
38    if key == keys.UP:
39        row = row - 1
40    if key == keys.DOWN:
41        row = row + 1
42    if key == keys.LEFT:
43        column = column - 1
44    if key == keys.RIGHT:
45        column = column + 1
46    tile = tiles[maze[row][column]]
47    if tile == 'empty':
48        x = column * TILE_SIZE
49        y = row * TILE_SIZE
50        animate(player, duration=0.1, pos=(x, y))
51    global unlock
52    if tile == 'goal':
53        print("Well done")
54        exit()
55    elif tile == 'key':
56        unlock = unlock + 1
57        maze[row][column] = 0 # 0 is 'empty' tile
58    elif tile == 'door' and unlock > 0:
59        unlock = unlock - 1
60        maze[row][column] = 0 # 0 is 'empty' tile
61
62    # enemy movement
63    row = int(enemy.y / TILE_SIZE)
64    column = int(enemy.x / TILE_SIZE)
65    row = row + enemy.yv
66    tile = tiles[maze[row][column]]
67    if not tile == 'wall':
68        x = column * TILE_SIZE
69        y = row * TILE_SIZE
70        animate(enemy, duration=0.1, pos=(x, y))
71    else:
72        enemy.yv = enemy.yv * -1
73    if enemy.colliderect(player):
74        print("You died")
75        exit()
76
77
78
79

9.8. Ideas for extension

However that is not the end! There are many things you could add to this game.

  • Show the player score.

  • Coins that the player collects to increase score.

  • Trap tiles that are difficult to see and kill the player.

  • Treasure chest that is unlocked with the key and increases score.

  • Instead of ending the game, give the player 3 lives.

  • Add more types of tile to the map: water, rock, brick, etc.

  • Change the player image depending on the direction they are moving.