Checkpoint

The Bouncing Lantern

A runaway lantern zooms around the Guild hall, bouncing off the walls. Let's simulate it!

Moving in 2D is just two 1D moves: one for x, one for y. So first solve one direction. A position pos moves by speed each step. If the next position would leave 0..size-1, the lantern bounces: flip the speed (speed = -speed) before moving.

size 4, pos 1, speed 1:  1, 2, 3, 2, 1, 0, 1, 2 ...

Then zip the x list and the y list together to get (x, y) points, one per frame:

for x, y in zip(xs, ys):
    ...

Assignment

  1. bounce_1d(size, pos, speed, steps) returns a list of steps positions. The first one is pos. Before each move, if pos + speed is below 0 or above size - 1, flip speed. Then add speed to pos.
  2. Finish the animation loop: for each (x, y), clear the canvas, paint (x, y) "gold", and show the frame.