Code
Recolor the Dragon
Game artists often reuse one sprite in new colours: a fire dragon becomes an ice dragon. A dict of swaps says what changes:
swaps = {"red": "sky", "orange": "snow"}
For each pixel, look the colour up. swaps.get(color, color)
returns the new colour if there is one, or the same colour if
it is not in the dict.
print(swaps.get("red", "red")) # sky
print(swaps.get("night", "night")) # night
The swap trap
What about swapping two colours, {"red": "green", "green": "red"}?
If you first turn all red into green, then all green into red,
everything ends up red! Looking up each pixel once in the
dict avoids the trap.
Build a new grid instead of changing the old one, so you still have the original dragon.
Assignment
recolor(grid, swaps)returns a new grid of the same size. Each colour found inswapsbecomesswaps[color]. Every other colour stays the same.- Don't change the original
grid. The program shows the fire dragon, the ice dragon, then a red ↔ green swap.
recolor(grid, swaps)returns a new grid of the same size. Each colour found inswapsbecomesswaps[color]. Every other colour stays the same.- Don't change the original
grid. The program shows the fire dragon, the ice dragon, then a red ↔ green swap.
main.py Python sleeping
Press Run to try your code, then Check to test it.