Follow the runner
Pygame challenges · Platformer, step 4 of 4 · 35 XP
Run to the right and the runner leaves the screen: the view never moves.
Your task
This level is 1920 pixels wide, four screens. The game draws everything shifted to the left by camera: a thing at x in the level appears at x - camera on the screen. So camera is where the screen's left edge is in the level, and making it bigger scrolls the view to the right.
Write camera_x(center, level_width, screen_width), where center is the middle of the runner across:
- to keep the runner in the middle of the screen, the camera is
center - screen_width // 2; - near the start that is less than 0, which would show empty space before the level begins, so the camera stops at 0;
- near the end it is more than
level_width - screen_width, which would show empty space past the end, so it stops there.
The game calls camera_x(x + 11, 1920, 480) every frame. Take the coins, squash the blobs by landing on them, keep out of the pits and 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 |
|---|---|
mid-level, the runner is in the middlecamera_x(1000, 1920, 480) | 760 |
at the start, the view stays at 0camera_x(100, 1920, 480) | 0 |
where the following beginscamera_x(240, 1920, 480) | 0 |
at the end, the view stopscamera_x(1850, 1920, 480) | 1440 |
a level as wide as the screen never scrollscamera_x(300, 480, 480) | 0 |