At My Fingertips
Rapid Playground
In this activity you will learn a way to build large grid-like graphics, like the maze below.
The maze is composed of different tiles. The definition and creation of those tiles is discussed in the activity Pac-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.
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!
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:
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
Creating it:
straight_tile(100, True)
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!
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?
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!
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!
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:
"-"
"|"
"-"
It's way faster both to express a maze tile by tile and to interpret it!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:
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:
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.
This way it's easy to remember which character represents which corner, even though the characters don't resemble the tiles they represent.
char_to_tile
functionLet'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.
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:
Therefore, we want the input to be a list[str]
:
a list of strings, where each string represents a separate row of the maze.
strings_to_maze
functionLet'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.
Now you are able to efficiently generate large mazes! Have fun in the code cell below and be creative!
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.
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
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)