At My Fingertips

Documentation

Pac-Man Maze Tiles

In this activity you will implement different functions to create different tiles, that composed together can ultimately generate a Pac-Man maze like the one below.

pacman maze

This activity only focuses on creating the tiles and composing a small maze. If after this you want to learn how to create bigger mazes you should save all the functions that create tiles in your toolbox, you will need them when you do the PyTamaro iconPac-Man Maze activity after this one.

The tiles

While different tiles have different patterns, they are all square shapes with a black backround. To avoid having to write

rectangle(side, side, black)

for each of the tiles, let's implement a black_square function that simply returns a black square with the given side. This way you can simply write

black_square(side)

to obtain a background!

You can use the rectangle function, but if you have a square function in your toolbox that's even better! (remember to import it if that is the case)

Loading...

The straight tiles

Implement the straight_tile function that generates a straight tile to use for the border.

The tile should be horizontal if the value of boolean parameter horizontal is True, otherwise it should be vertical.

The blue line that goes over the straight tile should be centered and the thickness should be 1/5 of the tile's side.

Use the black_square function you implemented for the background.

horizontal straight tile

vertical straight tile

Loading...

The corner tiles

Implement the function corner_tile to generate a corner tile rotated according to the rotation parameter, that represents the angle of rotation of the corner. Since the paramter represents an angle, the permitted values are 0, 90, 180 or 270, where:

  • corner_tile(100, 0) should return a top-right corner
  • corner_tile(100, 90) should return a top-left corner
  • corner_tile(100, 180) should return a bottom-left corner
  • corner_tile(100, 270) should return a bottom-right corner Since any other value is not permitted, you should assert that the given parameter is valid. Complete the assertion in the function to ensure the values are valid!

The blue line should have thickness of 1/5 times the tile's size, and should be able to match a straight tile. This implies that the blue line should pass through the middle of the sides of the corner tile, like a continuation of a straight tile.

corner tile rotation 0

corner tile rotation 90

corner tile rotation 180

corner tile rotation 270

Loading...

The floor tiles

Implement the function floor_tile to generate a floor tile that can be empty or have a dot or a pill, depending on the value of boolean parameters dot and pill.

  • If dot and pill are both False, then the floor tile should be empty.

    empty floot tile

  • If dot is True, then the tile should contain a white circle with diameter of 1/5 of the tile's side

    dot floor tile

  • If pill is True, then the tile should contain a white circle with diameter equal to the tile's side

    pill floor tile

  • dot and pill should never be both True. You can ensure this with an assertion (remember them from the PyTamaro iconPac-Man activity?)

Loading...

The Pac-Man tile

Lastly, implement the function pacman_tile that generates a square tile of the given side, with a Pac-Man with the given mouth_angle on top of it.

The pacman function

The pacman function should produce a Pac-Man graphic of the given radius and whose mouth is open at the given angle mouth_angle.

If you haven't, you should solve the PyTamaro iconPac-Man activity first, and save the pacman function in your toolbox by clicking on the icon

SaveToToolboxButtonSurrounded.png

on the cell containing the pacman function definition.

Once you are done, come back here and import the pacman function from your toolbox by running the next code cell.

Loading...

Now implement the pacman_tile function using the pacman function you imported. This is the result it should produce.

Pac-Man tile

The Pac-Man's position should be fixed at the left of the background. Use pin, compose and center_left, to obtain the wanted result.

Loading...

A simple maze

Generate some tiles using the functions you implemented, and combine them together using beside and above to obtain a simple maze like the following one:

simple maze

Implement a simple_maze function that takes as a parameter the side that each tile should have.

But don't cram all the code into this one function: when needed, introduce addtional helper functions.

The code should be readable and without code duplicates!

Loading...

As you likely have noticed, it is very painful to combine the tiles in this way, even for such a small maze. Imagine building the big maze we showed at the beginning!

There must be a way to make things easier... we discuss it in the PyTamaro iconPac-Man Maze activity! Go take a look!

What You Learned

You built simple tiles that can be used to generate a Pac-Man maze!

You practiced function definition and implemented multiple functions that take boolean values as parameters. You used conditionals to handle all the cases and return the necessary simple tile graphic.

For the corner tiles, you practiced the use of pin and compose, and for the Pac-Man tile, you had to import the pacman function from your toolbox.

You practiced assertion to ensure the validity of the values of the parameters of the corner_tile funciton.

The introduction of the black_square helper function is a form of problem decomposition, since in this case all the tiles had the same background in common.

You had to build a simple maze and got to the conclusion that building bigger mazes tile by tile is too laborious.


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

Pac-Man Maze Tiles

Logo of PyTamaro

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

Privacy PolicyPlatform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)