Collect the coins
Pygame challenges · Platformer, step 3 of 4 · 30 XP
Run through a coin and nothing happens: it stays there and the count stays at 0.
Your task
The coins are boxes (x, y, w, h) in a list. Every frame the game hands your function the runner's box and the list of coins still in the level.
Write collect(player, coins) and return two values as a tuple:
- a new list of the coins the runner does not touch, in the same order: the game keeps these as the coins left;
- how many coins it touches: the game adds that to your count.
The program already has overlap(a, b), the same check you wrote as caught in Catch, which is True when two boxes overlap. Build a new list and leave coins as it is: removing items from a list while a for loop walks through it makes the loop skip some of them.
Take every coin, then reach the flag.
The function is at the top of the program, marked fix this. Press Run to play the game as it is, then Check when your function is ready.
What Check tests
| Call | Should give |
|---|---|
touching one coin of twocollect((10, 10, 20, 30), [(15, 20, 10, 10), (100, 20, 10, 10)]) | ([(100, 20, 10, 10)], 1) |
touching nonecollect((10, 10, 20, 30), [(100, 20, 10, 10)]) | ([(100, 20, 10, 10)], 0) |
two at oncecollect((10, 10, 20, 30), [(0, 0, 12, 12), (50, 0, 10, 10), (25, 35, 10, 10)]) | ([(50, 0, 10, 10)], 2) |
no coins leftcollect((10, 10, 20, 30), []) | ([], 0) |
the list it was given stays the samebefore = [(15, 20, 10, 10), (100, 20, 10, 10)]
collect((10, 10, 20, 30), before)before | [(15, 20, 10, 10), (100, 20, 10, 10)] |