Land on platforms
Pygame challenges · Platformer, step 2 of 4 · 30 XP
The runner drops straight through every platform, the ground too.
Your task
The runner and every platform are boxes (x, y, w, h), like the fruit and the basket in Catch. These platforms work one way, as in many platformers: you can jump up through one from below and land on it from above.
This frame, the runner has moved down by vy. Its feet are at bottom = y + h, so before the move they were at bottom - vy. It lands on a platform when all three of these are true:
- it is falling or standing still, so
vy >= 0; - it overlaps the platform across: its left edge is left of the platform's right edge, and the platform's left edge is left of its right edge;
- its feet went past the platform's top this frame: they were at or above it before the move,
bottom - vy <= top, and are at or below it now,bottom >= top.
Write land(player, vy, platforms), where platforms is a list of boxes. If the runner lands on one, put its feet on the top: return (top - h, 0, True), which is the new y, a vy of 0 and True for standing. If it lands on none, return (y, vy, False).
That last value is the on_ground your jump needs. Your jump from the last step is in the program.
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 |
|---|---|
falls onto a platformland((100, 95, 20, 30), 6, [(80, 120, 100, 20)]) | (90, 0, True) |
standing still on itland((100, 90, 20, 30), 0, [(80, 120, 100, 20)]) | (90, 0, True) |
jumping up through itland((100, 105, 20, 30), -6, [(80, 120, 100, 20)]) | (105, -6, False) |
falling past its endland((185, 95, 20, 30), 6, [(80, 120, 100, 20)]) | (95, 6, False) |
feet already below its top: it does not pop up onto itland((100, 125, 20, 30), 6, [(80, 120, 100, 20)]) | (125, 6, False) |
over two platforms, it lands on the higher oneland((300, 195, 20, 30), 8, [(250, 300, 200, 20), (260, 220, 120, 20)]) | (190, 0, True) |