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:
canvas.clear()wipes the canvas back to its background.- Draw the scene again, with things in their new places.
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:
- Clear the canvas.
- Paint the bottom row
"grass"(canvas.recthelps). - Draw the sprite with
draw_spriteat(step, y). - Show the frame.
Write walk(canvas, sprite, palette, y, steps). For each step
from 0 to steps - 1:
- Clear the canvas.
- Paint the bottom row
"grass"(canvas.recthelps). - Draw the sprite with
draw_spriteat(step, y). - Show the frame.
main.py Python sleeping
Press Run to try your code, then Check to test it.