At My Fingertips
Rapid Playground
In this activity you will implement the ghost
function to create a ghost graphic
In the official Pac-Man game there are four ghosts. They have names!
Let's define constants holding the colors of the ghosts:
ghost
functionObserve the image below to understand how to decompose a ghost into basic shapes.
We want to implement the ghost
function so that it can generate ghosts of different sizes and colors.
Since we want to be able to choose the size and the color of the ghost,
those are the values to be parameterized.
Therefore, the function should have a parameter that models the size of the ghost
(which could be something like width
, head_radius
, ...), whose type should be float
,
and a parameter that models the color of the ghost, whose type should be Color.
Now let's implement a function that produces
a ghost, given the wanted width
and color
.
But don't cram all the code into this one function: when needed, introduce addtional functions, each producing a component of the ghost.
The code should be readable and without code duplicates!
If you have useful functions in your toolbox
(e.g. equilateral_triangle
, semicircle
, ...) you can import them and use them here!
In this activity you practiced problem decomposition by decomposing
the ghost graphic into simpler graphics, and the ghost
function
into simpler helper functions.
You also used user-defined colors, created with rgb_color.
Most importantly, you had a chance to learn about the choice of parameters for a function, and what types the parameters should have.
You might also have imported some useful functions from your toolbox (like semicircle
or equilateral_triangle
).
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Ghosts
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)