A text adventure
In this project you build a text adventure: a game that tells you where you are, reads what you want to do (go north, take key), and answers in text. The world isn't written into the program. It comes from a file, world.txt, so the same program can play any world that the file describes.
When it's finished, this file is a whole world: the house of an old clockmaker, with five rooms, three things to find, a locked door and a goal.
room;Workshop;Half-built clocks tick on every shelf.
room;Hallway;A long hallway. The grandfather clock here has stopped.
room;Kitchen;A kettle sits on a cold stove.
room;Attic;Dusty boxes stand under a round window.
room;Clock Tower;Huge wheels fill the tower. One gear is missing.
exit;Workshop;north;Hallway
exit;Hallway;south;Workshop
exit;Hallway;east;Kitchen
exit;Hallway;up;Attic
exit;Kitchen;west;Hallway
exit;Attic;down;Hallway
exit;Attic;up;Clock Tower
exit;Clock Tower;down;Attic
item;Workshop;key
item;Kitchen;gear
item;Attic;oil
lock;Attic;up;key
goal;Clock Tower;gear;You fit the gear into place, and the great clock starts ticking again.
A player who types these commands, one per line:
take key
go north
go east
take gear
go west
go up
go up
sees this, with the game's prompt > in front of each command:
== Workshop ==
Half-built clocks tick on every shelf.
Exits: north
You see: key
> take key
You take the key.
> go north
== Hallway ==
A long hallway. The grandfather clock here has stopped.
Exits: south, east, up
> go east
== Kitchen ==
A kettle sits on a cold stove.
Exits: west
You see: gear
> take gear
You take the gear.
> go west
== Hallway ==
A long hallway. The grandfather clock here has stopped.
Exits: south, east, up
> go up
== Attic ==
Dusty boxes stand under a round window.
Exits: down, up
You see: oil
> go up
You unlock the way up with the key.
== Clock Tower ==
Huge wheels fill the tower. One gear is missing.
Exits: down
You fit the gear into place, and the great clock starts ticking again.
You won in 5 moves.
What you need
Everything up to module 23: functions that return values (modules 13 and 14), lists (module 15), the string methods split, join and strip (module 18), dictionaries, also dictionaries inside a dictionary (module 21), a function that returns two values as a tuple (module 22), and reading a file line by line with with open(...) (module 23).
How you build it
Every line of world.txt starts with its kind (room, exit, item, lock or goal), then the name of the room it's about, and then the rest, all separated by ;. The room lines come first in the file. Each milestone teaches the program one more kind of line, with the commands that go with it:
- Rooms: the rooms from the file, and a loop that reads commands.
- Exits:
go northtakes you to the next room. - Things to carry:
take,dropandinventory. - A locked door: an exit that opens only for a player who carries the right thing.
- The goal: the game ends when you reach it, and counts your moves.
The program skips the lines of a kind it doesn't know yet, so a world file written for a later milestone still works with an earlier program.
Milestones
- Rooms
- Exits
- Things to carry
- A locked door
- The goal