Exerpad
🔥 0
0 XP
Exercise 2 · Chapter: GravityRequired+50 XP

Hit the Floor

Right now the dot falls out of the world and crashes with an IndexError.

Stop it at the bottom. After moving, if y has reached or passed HEIGHT - 1:

  • put y back to exactly HEIGHT - 1
  • set vy to 0

Setting y back matters. Because the dot speeds up, it can jump past the floor in a single frame — from row 10 straight to row 15. Clamping it puts it where it should have landed.

Zeroing vy matters too. Leave it and the dot keeps trying to fall faster while stuck to the floor, and the moment anything lets it move again it shoots off.

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


def update(state, keys):
state["vy"] = state["vy"] + 1
state["y"] = state["y"] + state["vy"]
# Stop at the floor: clamp y to HEIGHT - 1 and zero vy.
return state


def draw(state):
grid = blank_grid()
grid[state["y"]][20] = "white"
return grid