Exercise 3 · Chapter: Colour and TimeRequired+50 XP
A Sliding Stripe
Paint a white stripe down the whole height of the picture, at column
state["t"] % WIDTH.
Every frame the clock ticks, so the stripe steps one column right. When it
reaches the far edge it reappears on the left — and you don't write a single
line to make that happen. % cannot produce a number as big as WIDTH, so the
wrap is automatic.
Compare this with Chapter 2's wrap-around dot. Same idea, and this time you get it for free just by deriving the position from the clock.
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.................................... ...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()
# Paint a white stripe down every row, at column state["t"] % WIDTH.
return grid