Exercise 3 · Chapter: Scrolling WorldRequired+50 XP
Far and Near
Add distant hills that move slower than the ground.
- near, row
13:(col + state["t"]) % 4 == 0— one column per frame - far, row
7:(col + state["t"] // 3) % 9 == 0— one column every three
The only difference is // 3. Whole-number division means the far layer's
number only changes every third frame, so it drifts while the ground races.
Watch two frames side by side: the ground marks jump, the hills mostly don't. Your eye reads that difference as distance. There is no depth in a grid of squares — you just made some.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ A........A........A........A........A... ........................................ ........................................ ........................................ ........................................ ........................................ ###B###B###B###B###B###B###B###B###B###B ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ A........A........A........A........A... ........................................ ........................................ ........................................ ........................................ ........................................ ##B###B###B###B###B###B###B###B###B###B# ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........A........A........A........A.... ........................................ ........................................ ........................................ ........................................ ........................................ #B###B###B###B###B###B###B###B###B###B## ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........A........A........A........A.... ........................................ ........................................ ........................................ ........................................ ........................................ B###B###B###B###B###B###B###B###B###B###
Need a hint?
3 available · costs 5 XP each
python
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"
# Add the far hills on row 7, using t // 3 so they move slower.
return grid