Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: Press to FlapRequired+50 XP

No Flying

Hold space in the last exercise and the hero never comes down. Every frame resets vy to 4, so gravity never gets a chance.

Fix it: only jump when the hero is on the ground.

if keys["space"] and state["y"] == 0:

Now holding the button does nothing extra. You have to land before you can jump again, which is what makes jumping a decision instead of a switch.

The test holds space for six frames straight. A correct answer jumps exactly once.

Expected output
4/3 7/2 9/1 10/0 10/-1 9/-2 7/-3 4/-4 0/-5
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"y": 0, "vy": 0}


def update(state, keys):
if keys["space"]: # lets the hero fly — only allow it on the ground
state["vy"] = 4
state["y"] = state["y"] + state["vy"]
state["vy"] = state["vy"] - 1
if state["y"] < 0:
state["y"] = 0
state["vy"] = 0
return state


def draw(state):
grid = blank_grid()
grid[GROUND - state["y"]][6] = "gold"
return grid