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

Coin Quest: Scroll the World

What this step looks like when it works

🪙 The hero jumps, but he's jumping on the spot. Let's make the world move.

Real runner games cheat: the hero never actually moves forward. The world slides past him. All you need is a counter.

1. Write new_game() — it returns the state the game starts with. The engine calls it once at the start, so this is where every key gets its first value:

{"y": 0, "vy": 0, "x": 0, "score": 0, "is_playing": True}

2. In update, add one line: state["x"] = state["x"] + 1

3. In draw, paint stripes on the dirt so you can see the motion. Loop over every column and paint "white" at row GROUND + 1 when (col + state["x"]) % 4 equals 0.

Draw the stripes before the hero, so he stands in front of them.

Where you'll use this
Nothing is really moving. `x` counts up, the stripes are drawn one square further left each frame, and your eye invents the running. Almost every side-scrolling game ever made is this trick.
Expected output
1 2 3 4 5 6 7 8
Need a hint?
3 available · costs 5 XP each
python
def new_game():
# Add the rest of the starting values: x, score, is_playing
return {"y": 0, "vy": 0}


def update(state, keys):
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

# Move the world one square

return state


def draw(state):
grid = blank_grid()

# Paint the moving stripes on row GROUND + 1

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