Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: Scrolling WorldRequired+50 XP

Rocks Coming

Three rocks travel towards the hero and wrap around forever.

new_game gives you {"rocks": [10, 22, 34], "t": 0}. Each number is a column. Every frame, move each rock one column left, and when it goes past the left edge, send it back to WIDTH - 1.

This is the raindrop recycling from Chapter 5, turned sideways: falling down becomes travelling left. Three rocks, reused forever, and the world looks endless.

The trace prints the list, so you'll see the numbers count down together and one of them jump back to 39.

Expected output
[9, 21, 33] [8, 20, 32] [7, 19, 31] [6, 18, 30] [5, 17, 29] [4, 16, 28] [3, 15, 27] [2, 14, 26] [1, 13, 25] [0, 12, 24] [39, 11, 23]
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"rocks": [10, 22, 34], "t": 0}


def update(state, keys):
state["t"] = state["t"] + 1
for i in range(len(state["rocks"])):
# Move this rock one column left, and wrap it to WIDTH - 1 past the edge.
pass
return state


def draw(state):
grid = blank_grid()
for i in range(len(state["rocks"])):
grid[12][state["rocks"][i]] = "dimgray"
return grid