What it shows
A dictionary of rooms links each place to the others, and a loop moves the player between them. It shows how data structures can describe a whole game world.
The science
A text adventure is a state machine: the program keeps the player’s current room and changes it according to the command typed. Storing the map as a dictionary of dictionaries means adding a room is just adding data, not new code. The loop runs until the player reaches the winning room.
Materials
Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- A computer, laptop or phone with a browser (free)
- Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
- A rough map of six rooms drawn on paper
- A notebook and a pencil
Safety
This project uses safe, everyday materials. Still, read the rules below with a parent before you start.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Draw a map with six rooms such as gate, hall, library, lab, ground and office, and note which directions join them.
Create adventure.py and write the map as rooms = {"gate": {"text": "You are at the school gate.", "go": {"north": "hall"}}, "hall": {...}} with every room having text and go.
Make the winning room, for example library, have the text "You found the lost key! You win."
Set room = "gate" and start while True:.
Inside, print(rooms[room]["text"]) and then if room == "library": break.
Ask the player with move = input("Where to? (north/south/east/west/quit) ").lower().
Add if move == "quit": break, then elif move in rooms[room]["go"]: room = rooms[room]["go"][move], and else: print("You cannot go that way").
Play and test every path; add an items list and a rule that the lab door needs a key from the ground.
What you should see
The player types directions to walk through the rooms. Wrong directions give a message, and reaching the library prints the winning text and ends the game.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What does the rooms dictionary store?
For each room, its description and where each direction leads.
2Why is this easy to extend?
New rooms are just new data; the loop does not change.
3What does "in" check here?
Whether the typed direction is a key in that room’s go dictionary.
4What is the current state of the game?
The room the player is in.
Take it further
Add an inventory list and objects in rooms that the player can take with a take command.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Computer & office
Computer Fundamentals Study Book
The complete study notes, practice questions and glossary for Computer Fundamentals
