
Code
One Turn at a Time
Real games split their work into three jobs that repeat:
- Read a command.
- Update the state.
- Draw the picture.
We keep all the state in one dict, and give each job its own function:
state = {"world": [".C.", "#.."],
"pos": (0, 0), "backpack": []}
for command in commands:
message = step(state, command) # update
print(message)
for line in draw(state): # draw
print(line)
step(state, command) changes the state and returns a
message. It never prints. Because the dict passed in is the
same dict, state["pos"] = new sticks after step returns.
Why keep them apart? You can test step without a screen, and
later swap the text drawing for pixel art without touching the
game rules.
Assignment
Complete step(state, command). First clean the command with
.strip().lower(). Then:
- A direction in
MOVES: ifcan_enterthe new square, move there and returnf"Pip walks {cmd}.". If not, return"Something blocks the way." "take": if the tile under Pip is inITEMS, append its name, set that tile to"."and returnf"Pip picks up the {name}."Otherwise return"Nothing to take here."- Anything else: return
f"Pip doesn't understand '{cmd}'."
Complete step(state, command). First clean the command with
.strip().lower(). Then:
- A direction in
MOVES: ifcan_enterthe new square, move there and returnf"Pip walks {cmd}.". If not, return"Something blocks the way." "take": if the tile under Pip is inITEMS, append its name, set that tile to"."and returnf"Pip picks up the {name}."Otherwise return"Nothing to take here."- Anything else: return
f"Pip doesn't understand '{cmd}'."
main.py Python sleeping
Press Run to try your code, then Check to test it.