Exercise 3 · Chapter: Press to FlapRequired+50 XP
Press to Jump
Give the hero a reason to exist.
At the top of update, before moving, check the space bar. If it's down,
set vy to 4.
That's the whole jump. You don't write "go up" — you set an upward speed and let the gravity you already wrote take it from there. It rises, slows, stops, falls. All of that is the same three lines doing their job.
The test's key script has a space in it, so you'll see vy jump to 4 and
then get eaten away one frame at a time.
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 space is pressed, set vy to 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