…
…
Build the rooms with a dictionary, process commands, manage the inventory; code the locked door and dark cellar rules and check them with assert tests.
In the text adventure games of the 1970s, the screen shows only text: the player types commands like "go north" or "take lantern", and the game describes what happens. In this project you will write a small adventure game with four rooms. There is a treasure in the cellar, but the cellar door is locked and it is pitch dark inside.
This is the project of the Final project lesson (40); as in lesson 39, first plan the request and the function list. The minimum requirement: at least 3 functions and at least 1 assert test for each of them.
Lessons used: functions (14–16), tests with assert (19), string methods (21), lists and copies (23–25), nested dictionaries (26–28), tuples (29), try, except, raise (32), the copy module (36), turning a problem into a program and finishing patterns (39–40).
The world is defined with nested dictionaries. reset() restarts the game from the beginning; every cell starts with it. The player's commands are in a ready-made list.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
In the WORLD dictionary, each room is a key; its value holds the name of the room, its exits (direction → room), and the items inside. Items will move during the game, so reset() uses a deep copy (copy.deepcopy) of this dictionary; this way the original map is never damaged.
look() must return the text that describes the room the player is in. There is one rule: if the player is in the cellar without the lantern, they cannot see anything, and Pitch dark. You cannot see anything. is returned.
This step's code cell is for members.Sign up free
go(direction) moves the player to the room in that direction, increases the move count, and returns the look() text of the new room. In two cases an error is raised and the player does not move:
You cannot go that way.The door is locked. You need the key.Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Locked doors are in the LOCKED dictionary, whose keys are (room, direction) tuples. LOCKED.get(...) returns None if there is no lock.
This step's code cell is for members.Sign up free
take(item) takes the item from the room, puts it into the inventory, and returns a text like Taken: lantern.. The rules:
You cannot find anything in the dark.There is no sword here.Remove the item from the room's list (remove) and add it to the inventory's list (append).
This step's code cell is for members.Sign up free
Write the command(text) function that interprets the text the player types. Convert the text to lowercase and split it on spaces; the first word is the verb:
| Command | What it does |
|---|---|
look | look() |
go <direction> | go(direction) |
take <item> | take(item) |
inventory | You carry: lantern, key (if empty, Your bag is empty.) |
An unknown command or a missing word raises the error Unknown command: dance.
This step's code cell is for members.Sign up free
The player's commands are ready. Print each command in the form > go north, then show its result or its error message. As soon as the player takes the treasure, print the congratulation message with the move count and end the game.
This step's code cell is for members.Sign up free
As a game grows, it becomes easy to break a rule. run_tests() checks the rules with assert. The ready-made tests try a wrong direction, the dark cellar, and taking an item, so each of go, look, and take has a test (the minimum requirement of the final project); the missing one is the locked door test: when a player without the key tries to go down from the library, they must get a ValueError.
A test that expects an error is written like this: try the operation inside try; if no error comes, fail the test with assert False, "message". The check stage will remove the lock and see whether your test catches it.
This step's code cell is for members.Sign up free