Lesson 26 of 26 · about 45 min
Boss level: build a text adventure
Put everything from this course together to build a playable game, with rooms in dictionaries, state in variables, commands parsed from strings, and functions keeping it organized.
🧭 What you need first: Dictionaries and sets , Objects and classes
Before graphics, some of the most popular computer games were made entirely of text. You’d read “You are in a dark kitchen. A door leads north,” type go north, and the world would answer. Players spent hundreds of hours in worlds built from nothing but strings and dictionaries.
That’s your boss level. Everything you’ve learned has a job here:
- Dictionaries hold the map of rooms.
- Lists hold your inventory.
- Strings turn what the player types into commands.
- if / elif decide what each command does.
- Functions keep it all organized.
- Testing makes sure the game can actually be won.
Another way: a board game
In a board game, the board is the data (it doesn’t change), and your token’s position and the cards in your hand are the state (they change every turn). Each turn, a rule reads the state and updates it. Your handle() function is the rulebook.
Another way: a choose-your-own-adventure book
“If you open the door, turn to page 42.” Each room is a page, each exit is “turn to page…”. Your rooms dictionary is the whole book, and current is your finger on the page.
Build it, then play it
The skeleton below already has the rooms, a working look command and go. Press Run and play, type look, then go north. Then finish the game: make take, inventory and unlock work, and let the player escape.
Finish the game so the checker can win it: take the key, go to the hall, unlock the door, go north and escape.
Press “Run and play”, then type commands below.
What you just used
This one program used almost every idea from the course:
| Idea | Where it shows up |
|---|---|
| Variables and state | current, inventory, door_unlocked |
| Dictionaries (nested) | rooms[current]["exits"] |
| Lists and mutation | moving an item from a room’s list to inventory |
| Strings and parsing commands | command.lower().split() → verb + thing |
| Decisions | the if / elif chain in handle |
| Functions and decomposition | start, describe, handle |
| Scope | global lets handle change the game’s state |
| Testing | the checker plays the game, including the “no key” edge case |
A design note: using global is fine for a small game, but in bigger programs it gets messy. In F03 and later courses you’ll keep state inside an object (a Game class with self.current and self.inventory) instead. Try refactoring this game into a class as an extra challenge.
Make it yours
Once the checker passes, keep going. These are optional, but this is where it gets fun:
- Add a new room and connect it with exits in both directions.
- Add a
dropcommand (the reverse oftake). - Make the hall too dark to see without the
lamp. - Add a
helpcommand that lists every command. - Refactor the whole thing into a
Gameclass, with no globals.
You add a room 'garden' with exits {'west': 'hall'}, but players can't get there. What's missing?
What does "go north".lower().split() produce?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?