
Code
Draw the Island
Every adventure needs a map. In Python, a simple game map can be a list of strings. Each string is one row, and each character in it is one square, called a tile.
island = [
"......",
".##...",
"....#.",
]
print(island[1]) # .##...
print(island[1][2]) # #
island[1] picks row 1. Then [2] picks the tile at index 2
of that row. As always, counting starts at 0.
Here . is open ground and # is a rock.
Putting Pip on the map
Strings can't be changed in place. So to show Pip, we build a
new row with slices and put a P in the middle:
line = "......"
col = 2
print(line[:col] + "P" + line[col + 1:]) # ..P...
line[:col] is everything before Pip. line[col + 1:] is
everything after. The real map never changes: we draw a fresh
picture each time Pip moves.
Pip tip: The starter also paints your picture on the pixel canvas, one pixel per tile. Look for the gold pixel: that's me!
Assignment
- Complete
place_pip(line, col)so it returnslinewith"P"at indexcol. - Complete
render(world, row, col)so it returns a new list. Loop over the rows ofworld. Add each row topicture, but send row numberrowthroughplace_pipfirst. - Run it. The map prints with
Pin row 2, column 1.
- Complete
place_pip(line, col)so it returnslinewith"P"at indexcol. - Complete
render(world, row, col)so it returns a new list. Loop over the rows ofworld. Add each row topicture, but send row numberrowthroughplace_pipfirst. - Run it. The map prints with
Pin row 2, column 1.
main.py Python sleeping
Press Run to try your code, then Check to test it.