Editor

Jump from the ground

Pygame challenges · Platformer, step 1 of 4 · 20 XP

Press jump in mid-air and the runner jumps again, so you can fly over everything.

Your task

The runner has a speed up or down, vy. Up the screen is negative and down is positive, because y grows downwards. Every frame gravity adds a little to vy, the way your fall_speed did in Catch, and then vy is added to y. So a jump is one push upwards, a big negative vy, which gravity slowly turns into a fall.

Write jump(vy, on_ground, pressed, power). pressed is True on the frame the jump key goes down, and on_ground is True while the runner stands on something. When both are True, return -power, the push upwards. In every other case return vy unchanged, so pressing jump in mid-air does nothing.

The game calls vy = jump(vy, on_ground, pressed, 9.5) every frame, before gravity. Up or Space jumps: clear 25 barrels to win.

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

CallShould give
a jump from the groundjump(0, True, True, 10)-10
no jump in mid-airjump(3, False, True, 10)3
still rising: a second press does nothingjump(-4, False, True, 10)-4
on the ground with no key, it stays putjump(0, True, False, 10)0
falling with no key, it keeps fallingjump(6, False, False, 10)6