Exercise 2 · Chapter: BounceRequired+50 XP
Turn Around
Start the dot at column 35 so it reaches the wall quickly.
When it gets to the right-hand edge, flip dx to -1 so it heads back the
other way. It only has to turn around once here — the left wall comes next.
Order matters: move first, then check whether you've arrived. Checking before you move tests where the dot used to be.
Expected output
36/1 37/1 38/1 39/-1 38/-1 37/-1 36/-1 35/-1
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"x": 35, "dx": 1}
def update(state, keys):
state["x"] = state["x"] + state["dx"]
# If x has reached the right-hand wall, set dx to -1.
return state
def draw(state):
grid = blank_grid()
grid[6][state["x"]] = "red"
return grid