At My Fingertips

Pac-Man Maze

In this activity you will learn a way to build large grid-like graphics, like the maze below.

pacman maze

The Tiles

The maze is composed of different tiles. The definition and creation of those tiles is discussed in the activity PyTamaro iconPac-Man Maze Tiles. Solve that activity first, save all the functions that create tiles in your toolbox and come back here.

Import the functions straight_tile, corner_tile, floor_tile and pacman_tile from your toolbox in the code cell below.

Loading...

That should have left you wondering how you could find an easier way to compose a large maze. As always in informatics there are multiple approaches, we will guide you through one of the possible solutions, that maps lists of strings to a maze!

Expressing a Tile

Imagine you pick a tile and you want a reader to understand what is the tile you are referring to, let's say an horizontal straight tile. There are multiple ways to express an horizontal straight tile:

  1. Describing it:

    An horizontal straight tile is composed of a blue rectangle with width five times its height, overlayed on top of a black square with side equal to the rectangle's width

  2. Creating it:

    straight_tile(100, True)
  3. Giving it a name:

    h_tile = ...

These are all good ways to express a tile, but let's see how they fit in the context of expressing a maze composed of multiple tiles!

Expressing a Maze

Let's try to express the following minimalistic maze tile by tile, using the strategies described before. Are these good ways to express the maze itself?

simple maze

  1. Describing the tiles:

    An horizontal straight tile is composed of a blue rectangle ...

    A vertical straight tile is composed of a blue rectangle ...

    An horizontal straight tile is composed of a blue rectangle ...

    It is truly not optimal to read this and manage to understand what the maze is supposed to look like!

  2. Creating the tiles:

    straight_tile(100, True)
    straight_tile(100, False)
    straight_tile(100, True)

    A little bit better but it's not quickly interpretable either!

  3. Naming the tiles:

    h_tile
    v_tile
    h_tile

    This is way better, but imagine trying to visualize a large maze using this approach. Still not optimal.

What about this:

  1. Assigning a Character to the tiles:
    "-"
    "|"
    "-"
    It's way faster both to express a maze tile by tile and to interpret it!

Mapping Characters to Tiles

Let's define a way to associate a character to a tile! Let's choose characters that look similar to the tiles that we want them to represent, for example:

  • "|" to represent a vertical straight tile
  • "-" to represent an horizontal straight tile
  • " " to represent an empty floor tile
  • "." to represent a floor tile with a dot
  • "o" to represent a floor tile with a pill
  • "P" to represent a pacman tile

These are the ones we used, you can choose your own if you have other preferences!

For corner tiles the choice is a little bit harder: "L" can be an easy pick for a bottom-left corner, but what about a top-left corner? Not so easy right?

This is what we chose:

  • "w" to represent a top-right corner tile (rotation 0)
  • "q" to represent a top-left corner tile (rotation 90)
  • "a" to represent a bottom-left corner tile (with rotation 180)
  • "s" to represent a bottom-right corner tile (rotation 270)

Are you wondering why? Try to find the keys to those letters in your keyboard! If you have a QWERTY or a QWERTZ it should form something like a square.

keyboard

This way it's easy to remember which character represents which corner, even though the characters don't resemble the tiles they represent.

The char_to_tile function

Let's start by implementing a char_to_tile function that simply takes a character (a string of length 1) and returns the tile represented by that character.

For each of the possible values passed to the function, return the corresponding tile.

Loading...

Mapping Strings to a Maze

Now that we can express a tile with a character, we can express a maze with a string (a string is a collection of characters)!

We want the input to look something like this:

my_maze = [
    "q-w",
    "| |",
    "a-s"
]

and obtain:

example maze

Therefore, we want the input to be a list[str]: a list of strings, where each string represents a separate row of the maze.

The strings_to_maze function

Let's implement a strings_to_maze function that maps a list of strings to a whole maze.

Strings are composed of characters. Each string represents all the tiles in a single row. Therefore, all the tiles represented by a single string should be placed one beside the other, to compose a singular row.

Each row should then be placed one above the other to obtain the final maze.

Loading...

Now you are able to efficiently generate large mazes! Have fun in the code cell below and be creative!

Loading...

Extra

You can also try to add tiles for oriented Pac-Mans (looking left, right, up, or down), maybe represented by characters "l", "r", "u", "d"

Remember that you have to map the tiles to a character in the char_to_tile function, and run the code cells again.

What You Learned

You imported all the tile functions from another activity using your toolbox.

Most importantly, you learned about characters and strings and how you can map from a character to a graphic using conditionals.

You learned about representing a maze using a list of strings, and how to convert from this representation to a final maze graphic, using loops to combine tiles together into rows, and rows together into a maze.


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Pac-Man Maze

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version b744b47 (Tue, 08 Oct 2024 16:30:14 GMT)