At My Fingertips

Documentation

Pacman Film

In this activity you will exercise function definition, lists and repetition and implement the pacman_film function to recreate the following graphic.

pacman-film

The Pac-Man

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...

A List of Pacmans

In this activity we want to produce a list of Graphics (a list[Graphic]), such that each element of the list is a Graphic of a Pac-Man whose mouth is open a little bit wider than the previous Pac-Man's.

We should therefore implement a function that returns such a list. Let's call it pacman_list.

If you have already solved the Pac-Man Animation activity, you will have implemented a function with the same name and that does almost the same thing. The only difference in this case is that the pacmans will not have a background. In this activity you will be asked to implement basically the same code again, which (as we discussed in the Welcome to Pytamaro Curriculum) is called code duplication, and it's bad.

Even though you have already solved something very similar before, in this case we want you to do it again as a way to practice, so don't use your toolbox for the pacman_list function!

Description of the Function

Let's now focus on the description. We want this function to produce a list[Graphic] (a list of Graphics), containing an arbitrary number of Pac-Man graphics, where the first Pac-Man starts with an arbitrary minimum mouth angle and each of the following Pac-Mans have their mouth open a little bit wider, until the last Pac-Man has a final arbitrary maximum mouth angle. The radius of the Pac-Mans can also be arbitrary but all Pac-Mans in the same list should have the same radius.

Here are two examples of elements of the lists of graphics that you should be able to obtain with your pacman_list function

five pacmans

three pacmans

Choosing the Parameters

The choice of parameters depends on what we want the function to be able to do. If we want to be able to choose the color of the Pac-Mans, then parameterizing the color would be a good idea. In our case we want the color to be fixed (yellow), therefore the color of the Pac-Mans should not be a parameter of the function. On the other hand, we want to be able to choose the number of Pac-Mans in our list, therefore one of the parameters of the function would be the number of Pac-Mans.

Given the description of the function, it should take the following parameters:

  • radius: the radius of each Pac-Man
  • min_angle: the angle of the mouth opening of the leftmost Pac-Man, in degrees
  • max_angle: the angle of the mouth opening of the rightmost Pac-Man, in degrees
  • pacman_count: the wanted number of Pac-Mans in the film

What about Types?

The radius should be of type float, since it could also take floating point numbers (like 120.5). The type of the mouth angles should also be float, since a mouth angle like 60.2° would be valid. The type of pacman_count (and for numbers used for counting in general) should be int, since you can only have an integer amount of Pac-Mans.

Therefore, these will be the parameters of our function:

def pacman_list(radius: float, min_angle: float, max_angle: float, pacman_count: int)

Implement the function

Now, implement the pacman_list function so that it creates a list of Pac-Man graphics as we discussed!

Remember to use the pacman function you imported at the beginning of the activity.

Loading...

List Comphrehensions

Did you write something like this?

def pacman_list(...) -> list[Graphic]:
    step_angle = (max_angle - min_angle) / (pacman_count - 1)
    result = []
    for i in range(pacman_count):
        result.append(pacman(radius, min_angle + i * step_angle))
    return result

Well done! Did you know that python offers the ability to refactor this code using list comprehensions, that allow you to fill a list based on values from another list (like a range of values)? The following code does the same thing as the one above!

def pacman_list(...) -> list[Graphic]:
    step_angle = (max_angle - min_angle) / (pacman_count - 1)
    return [pacman(radius, min_angle + i * step_angle) for i in range(pacman_count)]

Do you understand this code? Do you agree that the two implementations are equivalent?

The Pacman Film

To obtain the Pac-Man film Graphic, you now have to generate a list using pacman_list, and place each of the elements in the list one beside the other.

Implement the pacman_film function: note that it takes a parameter list_of_pacmans of type list[Graphic]. The function should return a Graphic composed of the elements of the given list placed one beside the other.

Loading...

Now, use a combination of show_graphic, pacman_film and pacman_list to output something similar to the following graphic:

pacman-film

Remember that pacman_film takes a list of graphics as a parameter.

Loading...

What You Learned

In this activity you learned how to place multiple graphics beside eachother by placing them in a list and using loops.

You practiced the creation of lists, and you were introduced to the concept of list comprehensions.

You also had a chance to practice choice of parameters when defining the pacman_listfunction and you even chose the types of the parameters.

You learned how to import the pacman function from your toolbox.


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

Pacman Film

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)