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

Crash

Make the rocks matter.

After moving them, check every rock: if one is in column 6 — the hero's column — and the hero is on the ground (y == 0), the game is over. Set state["is_playing"] to False.

The first rock starts at column 10, so it arrives after four frames. The trace prints is_playing as 1 or 0, so you should see 1 1 1 0.

Check after the rocks move. Check before, and you're asking where they were last frame — the classic reason a game says you crashed when you clearly didn't.

Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"y": 0, "vy": 0, "rocks": [10, 22, 34], "is_playing": True}


def update(state, keys):
for i in range(len(state["rocks"])):
state["rocks"][i] = state["rocks"][i] - 1
if state["rocks"][i] < 0:
state["rocks"][i] = WIDTH - 1
# Now check: is any rock in column 6 while the hero is on the ground?
return state


def draw(state):
grid = blank_grid()
for i in range(len(state["rocks"])):
grid[GROUND][state["rocks"][i]] = "dimgray"
grid[GROUND - state["y"]][6] = "gold"
return grid