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
bounce_1d(size, pos, speed, steps)returns a list ofstepspositions. The first one ispos. Before each move, ifpos + speedis below0or abovesize - 1, flipspeed. Then addspeedtopos.- Finish the animation loop: for each
(x, y), clear the canvas, paint(x, y)"gold", and show the frame.
bounce_1d(size, pos, speed, steps)returns a list ofstepspositions. The first one ispos. Before each move, ifpos + speedis below0or abovesize - 1, flipspeed. Then addspeedtopos.- Finish the animation loop: for each
(x, y), clear the canvas, paint(x, y)"gold", and show the frame.
main.py Python sleeping
Press Run to try your code, then Check to test it.