Checkpoint

Paint Your Sprite

Game artists draw sprites: small pictures that can be placed anywhere. A handy way to store one is a list of strings, plus a palette dict that says which colour each letter means.

PIP = [
    "..gg.",
    ".gwgg",
    ".gggr",
]
palette = {"g": "green", "w": "white", "r": "red", ".": None}

None (or a letter missing from the palette) means transparent: leave that pixel alone, so the background shows.

Placing the sprite

To draw the sprite with its top-left corner at (x, y), the letter in row dy, column dx lands on pixel (x + dx, y + dy). enumerate gives you each position together with its item:

for dy, row in enumerate(PIP):
    for dx, ch in enumerate(row):
        print(dx, dy, ch)

palette.get(ch) returns the colour, or None if ch is not in the dict. Sprites near the edge may hang off the canvas, so skip any pixel outside 0..width-1 and 0..height-1.

Assignment

  1. Complete draw_sprite(canvas, rows, palette, x, y). Paint each letter's colour at (x + dx, y + dy).
  2. Skip letters whose colour is None or missing from the palette.
  3. Skip pixels that would land off the canvas (no crash!).
  4. Write the loops yourself: don't call canvas.draw in it. The program already draws Pip twice, once peeking off the edge.