Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: Coin QuestOptional+50 XP

Coin Quest: Watch Out for Rocks

What this step looks like when it works

🪙 Jumping is only fun when there's something to jump over.

1. In new_game(), add a list of rocks: "rocks": [14, 26]

Those are column numbers — where each rock sits on the track right now.

2. In update, move every rock one square left. You can't change a list while you loop over it, so build a new one:

moved = []
for rock in state["rocks"]:
    moved.append(rock - 1)
state["rocks"] = moved

3. Check for a crash. The hero stands at columns 5 and 6. If a rock is at column 5 or 6 and state["y"] is 0, he's hit the ground-level rock — set state["is_playing"] to False.

4. Stop the world when he's out. Make the very first line of update:

if not state["is_playing"]:
    return state

5. In draw, paint each rock "dimgray" at row GROUND — but only when the rock is still on screen (column 0 up to WIDTH - 1).

Jump the rocks. Miss one and everything freezes.

Expected output
1 1 1 1 1 1 1 0 0 0 0 0
Need a hint?
4 available · costs 5 XP each
python
def new_game():
return {"y": 0, "vy": 0, "x": 0, "score": 0, "is_playing": True}


def update(state, keys):
# Stop everything once the game is over

if keys["space"] and state["y"] == 0:
state["vy"] = 3
state["y"] = state["y"] + state["vy"]
state["vy"] = state["vy"] - 1
if state["y"] < 0:
state["y"] = 0
state["vy"] = 0
state["x"] = state["x"] + 1

# Move every rock one square left

# If a rock reached column 5 or 6 while y is 0, the game is over

return state


def draw(state):
grid = blank_grid()
for col in range(WIDTH):
if (col + state["x"]) % 4 == 0:
grid[GROUND + 1][col] = "white"

# Paint each rock that is still on screen

row = GROUND - state["y"]
grid[row][5] = "gold"
grid[row][6] = "gold"
grid[row - 1][5] = "gold"
grid[row - 1][6] = "gold"