Exercise 2 · Chapter: Press to FlapRequired+50 XP
It Falls Down
Same falling you wrote in Chapter 4, now upside down.
The hero starts at height 6. Each frame:
- move: add
vytoy - gravity: take
1offvy - floor: if
ydropped below0, put it back to0and zerovy
Because y is a height, gravity subtracts from vy instead of adding. The
hero drops faster and faster until it lands, and then stays put.
The trace prints y/vy. Watch the height drop 6, 5, 3, 0 — bigger gaps each
frame, because vy keeps growing more negative — and then stay on the ground.
You may notice vy twitching between 0 and -1 once it has landed. That's
gravity still pulling on something that is already resting: the floor check
only fires when y would go below zero. It never moves the hero, so it does
no harm. Real games are full of small idling details like this.
Expected output
6/-1 5/-2 3/-3 0/-4 0/0 0/-1
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"y": 6, "vy": 0}
def update(state, keys):
# Move by vy, then subtract 1 from vy, then stop at the ground.
return state
def draw(state):
grid = blank_grid()
grid[GROUND - state["y"]][6] = "gold"
return grid