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
- Complete
draw_sprite(canvas, rows, palette, x, y). Paint each letter's colour at(x + dx, y + dy). - Skip letters whose colour is
Noneor missing from the palette. - Skip pixels that would land off the canvas (no crash!).
- Write the loops yourself: don't call
canvas.drawin it. The program already draws Pip twice, once peeking off the edge.
- Complete
draw_sprite(canvas, rows, palette, x, y). Paint each letter's colour at(x + dx, y + dy). - Skip letters whose colour is
Noneor missing from the palette. - Skip pixels that would land off the canvas (no crash!).
- Write the loops yourself: don't call
canvas.drawin it. The program already draws Pip twice, once peeking off the edge.
main.py Python sleeping
Press Run to try your code, then Check to test it.