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

  1. Complete place_pip(line, col) so it returns line with "P" at index col.
  2. Complete render(world, row, col) so it returns a new list. Loop over the rows of world. Add each row to picture, but send row number row through place_pip first.
  3. Run it. The map prints with P in row 2, column 1.