Exercise 1 · Chapter: Draw a SpriteRequired+50 XP
Stamp It
Write the stamp function.
It takes a grid, a sprite (a list of lists of 0s and 1s), a top row, a
left column and a colour. Wherever the sprite has a 1, paint that square.
draw already calls it for you:
stamp(grid, HERO, 5, 10, "gold")
so a correct stamp puts the hero at row 5, column 10.
Use len(sprite) and len(sprite[r]) rather than typing 3 and 4. The
sprite changes size in the next exercise, and a stamp built on len will
handle it without you touching a line.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ...........AA........................... ..........AAAA.......................... ...........A.A.......................... ........................................ ........................................ ........................................ ........................................ ........................................ ########################################
Need a hint?
3 available · costs 5 XP each
python
HERO = [
[0, 1, 1, 0],
[1, 1, 1, 1],
[0, 1, 0, 1],
]
def stamp(grid, sprite, top, left, colour):
# Paint colour wherever the sprite has a 1.
pass
def draw(state):
grid = blank_grid()
stamp(grid, HERO, 5, 10, "gold")
return grid