Exerpad
🔥 1
10 XP
Lesson · Chapter: Scrolling World+10 XP for reading

The Treadmill

Every endless runner you've played is a lie, and it's a good one.

The hero isn't going anywhere. They stay in the same column, frame after frame. It's the world that slides past — ground, trees, obstacles, all moving left while the hero jogs on the spot.

Once you see it you can't unsee it, and it makes the game far easier to write. A hero that never moves can never run off the edge, and a world made of numbers can go on forever.

The whole trick

python
for col in range(WIDTH):
if (col + state["t"]) % 4 == 0:
grid[13][col] = "sienna"
Output
Press Run to see the output.

Marks every fourth column — and because t grows each frame, the pattern slides one column left every time. There is no list of ground tiles. There is a formula, and a clock.

Parallax: distance for free

Real distance has a side effect: far things appear to move slower than near things. Copy it and a flat picture suddenly has depth.

python
near = (col + state["t"]) % 6 == 0 # moves one column per frame
far = (col + state["t"] // 3) % 9 == 0 # moves one column every 3 frames
Output
Press Run to see the output.

t // 3 is whole-number division — it only changes every third frame, so that layer crawls. Draw the far layer higher up and the near layer along the ground, and your eye reads it as a horizon.

This is called parallax, it costs one extra division, and it is the single cheapest way to make a 2D scene look like it has depth.

Things that come towards you

Obstacles are just numbers that get smaller:

python
for i in range(len(state["rocks"])):
state["rocks"][i] = state["rocks"][i] - 1
if state["rocks"][i] < 0:
state["rocks"][i] = WIDTH - 1
Output
Press Run to see the output.

Each rock is a column. Every frame it moves one to the left. When it passes the left edge it reappears on the right — the same recycling you did with raindrops in Chapter 5, turned on its side.

Three rocks, reused forever, and the player believes the world is endless.