Fruit falls faster
Pygame challenges · Catch, step 2 of 4 · 15 XP
The fruit drifts down at one slow, steady pace, like a feather.
Your task
Things speed up as they fall, because gravity pulls on them all the time. A game copies this with two numbers per fruit: its y and its speed. Every frame, gravity adds a little to the speed, then the speed is added to y. Small amounts add up quickly: with gravity 0.15, a fruit that starts at 1 pixel a frame falls 4 pixels a frame after 20 frames.
Write fall_speed(speed, gravity, top_speed). It returns the speed for the next frame, speed + gravity, but never more than top_speed, so fruit never falls too fast to catch.
Every frame the game calls fall_speed(speed, 0.15, 5) for each fruit, then adds the new speed to the fruit's y. Your move_basket from the last step moves the basket.
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 |
|---|---|
gravity adds to the speedfall_speed(2, 1, 8) | 3 |
half a pixel counts toofall_speed(2, 0.5, 8) | 2.5 |
faster every framespeed = 1
for frame in range(3):
speed = fall_speed(speed, 1, 8)speed | 4 |
never past the top speedfall_speed(7.5, 1, 8) | 8 |
at the top speed it stays therefall_speed(8, 1, 8) | 8 |