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

Running on the Spot

Put the hero on the moving ground — and don't move the hero.

Stamp HERO at row 9, column 6, every single frame. The same place, always. Only the ground slides.

Run it and watch: the hero looks like it's sprinting. Nothing about the hero changed. Your eye sees the ground move, decides the hero must be the thing travelling, and invents the running for you.

This is the illusion the whole chapter is built on, and every endless runner ever made uses it.

Expected output
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
.......AA...............................
......AAAA..............................
.......A.A..............................
........................................
###B###B###B###B###B###B###B###B###B###B
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
.......AA...............................
......AAAA..............................
.......A.A..............................
........................................
##B###B###B###B###B###B###B###B###B###B#
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
.......AA...............................
......AAAA..............................
.......A.A..............................
........................................
#B###B###B###B###B###B###B###B###B###B##
Need a hint?
3 available · costs 5 XP each
python
HERO = [
[0, 1, 1, 0],
[1, 1, 1, 1],
[0, 1, 0, 1],
]


def stamp(grid, sprite, top, left, colour):
for r in range(len(sprite)):
for c in range(len(sprite[r])):
if sprite[r][c] == 1:
if 0 <= top + r < HEIGHT and 0 <= left + c < WIDTH:
grid[top + r][left + c] = colour


def new_game():
return {"t": 0}


def update(state, keys):
state["t"] = state["t"] + 1
return state


def draw(state):
grid = blank_grid()
for col in range(WIDTH):
if (col + state["t"]) % 4 == 0:
grid[13][col] = "sienna"
# Stamp the hero at row 9, column 6.
return grid