Exercise 5 · Chapter: Scrolling WorldOptional+50 XP
The Whole World
Put every piece together into one scene:
- far hills on row
7, drifting - ground marks on row
13, racing - rocks on row
12, marching left and recycling - the hero at row
9, column6— standing still
Draw them in that order, back to front, so nearer things paint over farther ones.
Look at what you have: a hero who appears to run, a world with depth, and obstacles heading their way. Every ingredient of an endless runner is on screen.
The only thing missing is a way to do something about the rocks — and that is the next chapter, where the keyboard finally arrives.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ A........A........A........A........A... ........................................ .......BB............................... ......BBBB.............................. .......B.B.............................. .........C...........C...........C...... ###D###D###D###D###D###D###D###D###D###D ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ A........A........A........A........A... ........................................ .......BB............................... ......BBBB.............................. .......B.B.............................. ........C...........C...........C....... ##D###D###D###D###D###D###D###D###D###D#
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 {"rocks": [10, 22, 34], "t": 0}
def update(state, keys):
state["t"] = state["t"] + 1
for i in range(len(state["rocks"])):
state["rocks"][i] = state["rocks"][i] - 1
if state["rocks"][i] < 0:
state["rocks"][i] = WIDTH - 1
return state
def draw(state):
grid = blank_grid()
# Far hills, ground, rocks, hero — in that order.
return grid