Code

Flipbook

A cartoon is lots of pictures shown quickly, each a tiny bit different. Each picture is a frame. Every canvas.show() adds one frame, so calling it in a loop makes an animation!

Each frame follows the same recipe:

  1. canvas.clear() wipes the canvas back to its background.
  2. Draw the scene again, with things in their new places.
  3. canvas.show() saves the frame.
for step in range(5):
    c.clear()
    c.set(step, 2, "gold")   # moves 1 pixel right
    c.show()

Forget clear() and the old pictures stay put, leaving a smeared trail behind.

Pip tip: A run can show up to 120 frames. Press play on the canvas to watch them.

Assignment

Write walk(canvas, sprite, palette, y, steps). For each step from 0 to steps - 1:

  1. Clear the canvas.
  2. Paint the bottom row "grass" (canvas.rect helps).
  3. Draw the sprite with draw_sprite at (step, y).
  4. Show the frame.