Exercise 1 · Chapter: Scrolling WorldRequired+50 XP
Ground That Moves
Mark every fourth square of the dirt row, and make the pattern slide left as the clock ticks.
if (col + state["t"]) % 4 == 0:
Without the + state["t"] you get a stationary dotted line. With it, the whole
pattern shifts one column each frame — which reads as ground rushing past.
There is no list of tiles here and nothing is stored. The ground is worked out fresh every frame from one number.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ###A###A###A###A###A###A###A###A###A###A ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ##A###A###A###A###A###A###A###A###A###A# ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ #A###A###A###A###A###A###A###A###A###A##
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 % 4 == 0: # stationary — make it slide with the clock
grid[13][col] = "sienna"
return grid