Code

Replay the Expedition

Our game has no hidden surprises. The same map, the same start and the same commands always give the same ending. That means we can replay an expedition: run the commands again and watch what happened, turn by turn.

A replay keeps a trail: a list of every position Pip stood on. It starts with the start square and gets one entry per command. A blocked move adds the same position again.

Animate it

The pixel canvas can show the trail as an animation. Draw the map, paint Pip, then call c.show() to save a frame:

c.draw(world, COLORS)       # one pixel per tile
c.set(col, row, "gold")     # x is col, y is row!
c.show()                    # save this frame

Drawing the whole map each frame paints over the old Pip.

Pip tip: Games with dice stay replayable too: they use random.Random(42), a random generator with a fixed seed, so the "random" rolls come out the same every replay.

Assignment

  1. Complete replay(world, start, commands). Return the trail: start, then the position after each command (use walk).
  2. Complete animate(world, trail). For each (row, col) in trail: draw the map, paint Pip "gold" and call c.show().
  3. Type your own route in the Input box, like east south.