Exercise 3 · Chapter: DodgeRequired+50 XP
Stop the World
Right now the game "ends" but the rocks keep rolling. Freeze everything.
Put a check at the top of update:
if not state["is_playing"]:
return stateReturn the state unchanged and nothing below runs — no rocks, no gravity, no collision. The picture stops dead.
The trace prints the rocks and the flag together. Once the flag hits 0, the
rock numbers should stop changing entirely.
One line, at the top of one function, and the whole game has an off switch. That's worth remembering: a flag checked early is often cheaper than special cases scattered everywhere.
Expected output
[9, 21, 33]/1 [8, 20, 32]/1 [7, 19, 31]/1 [6, 18, 30]/0 [6, 18, 30]/0 [6, 18, 30]/0
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):
# If the game is over, return the state unchanged.
for i in range(len(state["rocks"])):
state["rocks"][i] = state["rocks"][i] - 1
if state["rocks"][i] < 0:
state["rocks"][i] = WIDTH - 1
for i in range(len(state["rocks"])):
if state["rocks"][i] == 6 and state["y"] == 0:
state["is_playing"] = False
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