Exercise 2 · Chapter: Draw a SpriteRequired+50 XP
A Bigger Sprite
Here is the payoff for using len.
Fill in the TREE sprite — 5 rows by 5 columns — so it looks like this:
..X.. .XXX. XXXXX ..X.. ..X..
Put 1 where an X is and 0 everywhere else. stamp is already written and
you should not need to change it at all.
If you had hard-coded 3 and 4 in the last exercise, this is where it would
have broken. A function built on len doesn't care what size you hand it.
Expected output
........................................ ........................................ ........................................ ........................................ ....................A................... ...................AAA.................. ..................AAAAA................. ....................A................... ....................A................... ........................................ ........................................ ........................................ ........................................ ########################################
Need a hint?
3 available · costs 5 XP each
python
TREE = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
]
def stamp(grid, sprite, top, left, colour):
for r in range(len(sprite)):
for c in range(len(sprite[r])):
if sprite[r][c] == 1:
grid[top + r][left + c] = colour
def draw(state):
grid = blank_grid()
stamp(grid, TREE, 4, 18, "green")
return grid