Stage 8 - Useability¶
Introduction¶
Our dungeon works, but before we finish we need to make it easier to use and clean up the code.
We will:
Pseudocode
add a help command
make the program easier to read and use
add a goodbye message at the end
write more comments in the code
delete code we don’t need
make our spacing neat and consistent
Help command¶
We know the commands because we wrote the code. A new player won’t know what they’re allowed to type. Right now, if they type something wrong, the program just says “I don’t understand,” which isn’t very helpful.
To fix this, we should add a help command that shows all the commands. But players won’t know the help command exists unless we tell them. So we update the final else: section to remind them to type “help” when they get something wrong.
Now add the highlighted code to main.py.
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23
24# create characters
25ugine = Enemy("Ugine")
26ugine.description = "a huge troll with rotting teeth."
27ugine.weakness = "cheese"
28
29nigel = Friend("Nigel")
30nigel.description = "a burly dwarf with golden bead in woven through his beard."
31nigel.conversation = "Well youngan, what are you doing here?"
32
33# add characters to rooms
34armoury.character = ugine
35lab.character = nigel
36
37# create items
38cheese = Item("Cheese")
39cheese.description = "super smelly"
40
41chair = Item("Chair")
42chair.description = "designed to be sat on"
43
44elmo = Item("Elmo")
45elmo.description = "wanting to be tickled"
46
47# add items to rooms
48cavern.item = chair
49armoury.item = elmo
50lab.item = cheese
51
52'''
53# describe the rooms
54cavern.describe()
55armoury.describe()
56lab.describe()
57'''
58
59# initialise variables
60running = True
61current_room = cavern
62backpack = []
63
64# ----- MAIN LOOP -----
65while running:
66 current_room.describe()
67
68 command = input("> ").lower()
69
70 if command in ["north", "south", "east", "west"]:
71 current_room = current_room.move(command)
72 elif command == "talk":
73 if current_room.character is not None:
74 current_room.character.talk()
75 else:
76 print("There is no one here to talk to")
77 elif command == "hug":
78 if current_room.character is not None:
79 current_room.character.hug()
80 else:
81 print("There is no one here to hug")
82 elif command== "fight":
83 if current_room.character is not None:
84 weapon = input("What will you fight with? > ").lower()
85 available_weapons = []
86 for item in backpack:
87 available_weapons.append(item.name)
88 if weapon in available_weapons:
89 if current_room.character.fight(weapon):
90 cif isinstance(current_room.character, Enemy):
91 current_room.character = None
92 if Enemy.get_num_of_enemy() == 0:
93 print("You have slain all the enemies. You are victorious!")
94 running = False
95 else:
96 running = False
97 else:
98 print(f"You don't have {weapon}")
99 print(f"{current_room.character.name} strikes you down.")
100 running = False
101 else:
102 print("There is no one here to fight")
103 elif command == "take":
104 if current_room.item is not None:
105 backpack.append(current_room.item)
106 print(f"You put {current_room.item.name} into your backpack")
107 current_room.item = None
108 else:
109 print("There is nothing here to take")
110 elif command == "backpack":
111 if backpack == []:
112 print("It is empty")
113 else:
114 print("You have:")
115 for item in backpack:
116 print(f"- {item.name.capitalize()}")
117 elif command == "help":
118 print("Type which direction you wish to move,")
119 print("or use one of these commands:")
120 print("- Talk")
121 print("- Fight")
122 print("- Hug")
123 print("- Take")
124 print("- Backpack")
125 elif command == "quit":
126 running = False
127 else:
128 print("Enter 'help' to list the copmmands.")
You’ve seen this kind of code many times now, so you should already understand how it works without investigating it.
Improving the UI and UX¶
Even though our program looks simple, people still have to use it, so we need to think about the UI and UX — how it looks and how easy it is to use.
UI and UX
UI means User Interface. It’s the stuff you actually see on the screen when you use an app or website — things like buttons, menus, icons, and colours. UI is about making everything look clear and easy to use.
UX means User Experience. It’s about what it feels like to use the app — if it’s easy, confusing, fun, or annoying. UX is about making sure the user has a smooth and enjoyable time using the program.
We’ve already fixed some UI and UX issues by adding the help command. Now play the game and see if you can spot anything else that feels confusing.
You might notice that after you type a command, the game shows the response and then instantly prints the room description again. This makes it easy to miss what the game just told you. To fix that, we’ll show the response and then make the user press a key before the game continues.
To do this, add the highlighted code below.
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23
24# create characters
25ugine = Enemy("Ugine")
26ugine.description = "a huge troll with rotting teeth."
27ugine.weakness = "cheese"
28
29nigel = Friend("Nigel")
30nigel.description = "a burly dwarf with golden bead in woven through his beard."
31nigel.conversation = "Well youngan, what are you doing here?"
32
33# add characters to rooms
34armoury.character = ugine
35lab.character = nigel
36
37# create items
38cheese = Item("Cheese")
39cheese.description = "super smelly"
40
41chair = Item("Chair")
42chair.description = "designed to be sat on"
43
44elmo = Item("Elmo")
45elmo.description = "wanting to be tickled"
46
47# add items to rooms
48cavern.item = chair
49armoury.item = elmo
50lab.item = cheese
51
52'''
53# describe the rooms
54cavern.describe()
55armoury.describe()
56lab.describe()
57'''
58
59# initialise variables
60running = True
61current_room = cavern
62backpack = []
63
64# ----- MAIN LOOP -----
65while running:
66 current_room.describe()
67
68 command = input("> ").lower()
69
70 if command in ["north", "south", "east", "west"]:
71 current_room = current_room.move(command)
72 elif command == "talk":
73 if current_room.character is not None:
74 current_room.character.talk()
75 else:
76 print("There is no one here to talk to")
77 elif command == "hug":
78 if current_room.character is not None:
79 current_room.character.hug()
80 else:
81 print("There is no one here to hug")
82 elif command== "fight":
83 if current_room.character is not None:
84 weapon = input("What will you fight with? > ").lower()
85 available_weapons = []
86 for item in backpack:
87 available_weapons.append(item.name)
88 if weapon in available_weapons:
89 if current_room.character.fight(weapon):
90 if isinstance(current_room.character, Enemy):
91 current_room.character = None
92 if Enemy.get_num_of_enemy() == 0:
93 print("You have slain all the enemies. You are victorious!")
94 running = False
95 else:
96 running = False
97 else:
98 print(f"You don't have {weapon}")
99 print(f"{current_room.character.name} strikes you down.")
100 running = False
101 else:
102 print("There is no one here to fight")
103 elif command == "take":
104 if current_room.item is not None:
105 backpack.append(current_room.item)
106 print(f"You put {current_room.item.name} into your backpack")
107 current_room.item = None
108 else:
109 print("There is nothing here to take")
110 elif command == "backpack":
111 if backpack == []:
112 print("It is empty")
113 else:
114 print("You have:")
115 for item in backpack:
116 print(f"- {item.name.capitalize()}")
117 elif command == "help":
118 print("Type which direction you wish to move,")
119 print("or use one of these commands:")
120 print("- Talk")
121 print("- Fight")
122 print("- Hug")
123 print("- Take")
124 print("- Backpack")
125 elif command == "quit":
126 running = False
127 else:
128 print("Enter 'help' to list the copmmands.")
129 input("\nPress <Enter> key to continue")
Save the file, predict and then run the code.
How does that work? Let’s investigate:
Code Explaination
input("\nPress <Enter> key to continue")is basically a little trick. We’re usinginputin a way it’s not normally meant to be used.Normally,
inputwaits for the user to type something, which is why it pauses the game.The user presses Enter to continue, which ends the pause.
We don’t save what the user types, so whatever they enter is ignored and disappears.
Farewell message¶
When the game ends, it just shuts off straight away. No message, even if you win or lose. To make the ending feel a bit nicer, we should add a goodbye message.
Add the highlighted code below to include the farewell message.
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23
24# create characters
25ugine = Enemy("Ugine")
26ugine.description = "a huge troll with rotting teeth."
27ugine.weakness = "cheese"
28
29nigel = Friend("Nigel")
30nigel.description = "a burly dwarf with golden bead in woven through his beard."
31nigel.conversation = "Well youngan, what are you doing here?"
32
33# add characters to rooms
34armoury.character = ugine
35lab.character = nigel
36
37# create items
38cheese = Item("Cheese")
39cheese.description = "super smelly"
40
41chair = Item("Chair")
42chair.description = "designed to be sat on"
43
44elmo = Item("Elmo")
45elmo.description = "wanting to be tickled"
46
47# add items to rooms
48cavern.item = chair
49armoury.item = elmo
50lab.item = cheese
51
52'''
53# describe the rooms
54cavern.describe()
55armoury.describe()
56lab.describe()
57'''
58
59# initialise variables
60running = True
61current_room = cavern
62backpack = []
63
64# ----- MAIN LOOP -----
65while running:
66 current_room.describe()
67
68 command = input("> ").lower()
69
70 if command in ["north", "south", "east", "west"]:
71 current_room = current_room.move(command)
72 elif command == "talk":
73 if current_room.character is not None:
74 current_room.character.talk()
75 else:
76 print("There is no one here to talk to")
77 elif command == "hug":
78 if current_room.character is not None:
79 current_room.character.hug()
80 else:
81 print("There is no one here to hug")
82 elif command== "fight":
83 if current_room.character is not None:
84 weapon = input("What will you fight with? > ").lower()
85 available_weapons = []
86 for item in backpack:
87 available_weapons.append(item.name)
88 if weapon in available_weapons:
89 if current_room.character.fight(weapon):
90 if isinstance(current_room.character, Enemy):
91 current_room.character = None
92 if Enemy.get_num_of_enemy() == 0:
93 print("You have slain all the enemies. You are victorious!")
94 running = False
95 else:
96 running = False
97 else:
98 print(f"You don't have {weapon}")
99 print(f"{current_room.character.name} strikes you down.")
100 running = False
101 else:
102 print("There is no one here to fight")
103 elif command == "take":
104 if current_room.item is not None:
105 backpack.append(current_room.item)
106 print(f"You put {current_room.item.name} into your backpack")
107 current_room.item = None
108 else:
109 print("There is nothing here to take")
110 elif command == "backpack":
111 if backpack == []:
112 print("It is empty")
113 else:
114 print("You have:")
115 for item in backpack:
116 print(f"- {item.name.capitalize()}")
117 elif command == "help":
118 print("Type which direction you wish to move,")
119 print("or use one of these commands:")
120 print("- Talk")
121 print("- Fight")
122 print("- Hug")
123 print("- Take")
124 print("- Backpack")
125 elif command == "quit":
126 running = False
127 else:
128 print("Enter 'help' to list the copmmands.")
129 input("\nPress <Enter> key to continue")
130print("Thank you for playing Deepest Dungeon")
In-code comments¶
We already have a few comments in the code to help explain what parts do, but most of the main loop doesn’t have any. Adding comments there will make the code easier to read and easier to fix later.
Code maintainability
Code maintainability means making your code easy to understand, fix, and update later. If your code is neat, well-organised, and has clear comments, you or someone else can quickly figure out how it works. This makes it easier to find bugs, add new features, and change things without breaking the program.
Let’s first start with main.py
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23
24# create characters
25ugine = Enemy("Ugine")
26ugine.description = "a huge troll with rotting teeth."
27ugine.weakness = "cheese"
28
29nigel = Friend("Nigel")
30nigel.description = "a burly dwarf with golden bead in woven through his beard."
31nigel.conversation = "Well youngan, what are you doing here?"
32
33# add characters to rooms
34armoury.character = ugine
35lab.character = nigel
36
37# create items
38cheese = Item("Cheese")
39cheese.description = "super smelly"
40
41chair = Item("Chair")
42chair.description = "designed to be sat on"
43
44elmo = Item("Elmo")
45elmo.description = "wanting to be tickled"
46
47# add items to rooms
48cavern.item = chair
49armoury.item = elmo
50lab.item = cheese
51
52'''
53# describe the rooms
54cavern.describe()
55armoury.describe()
56lab.describe()
57'''
58
59# initialise variables
60running = True
61current_room = cavern
62backpack = []
63
64# ----- MAIN LOOP -----
65while running:
66 current_room.describe()
67
68 command = input("> ").lower()
69
70 # move
71 if command in ["north", "south", "east", "west"]:
72 current_room = current_room.move(command)
73 # talk
74 elif command == "talk":
75 if current_room.character is not None:
76 current_room.character.talk()
77 else:
78 print("There is no one here to talk to")
79 # hug
80 elif command == "hug":
81 if current_room.character is not None:
82 current_room.character.hug()
83 else:
84 print("There is no one here to hug")
85 # fight
86 elif command== "fight":
87 if current_room.character is not None:
88 weapon = input("What will you fight with? > ").lower()
89 available_weapons = []
90 for item in backpack:
91 available_weapons.append(item.name)
92 if weapon in available_weapons:
93 if current_room.character.fight(weapon):
94 if isinstance(current_room.character, Enemy):
95 current_room.character = None
96 if Enemy.get_num_of_enemy() == 0:
97 print("You have slain all the enemies. You are victorious!")
98 running = False
99 else:
100 running = False
101 else:
102 print(f"You don't have {weapon}")
103 print(f"{current_room.character.name} strikes you down.")
104 running = False
105 else:
106 print("There is no one here to fight")
107 # take
108 elif command == "take":
109 if current_room.item is not None:
110 backpack.append(current_room.item)
111 print(f"You put {current_room.item.name} into your backpack")
112 current_room.item = None
113 else:
114 print("There is nothing here to take")
115 # backpack
116 elif command == "backpack":
117 if backpack == []:
118 print("It is empty")
119 else:
120 print("You have:")
121 for item in backpack:
122 print(f"- {item.name.capitalize()}")
123 # help
124 elif command == "help":
125 print("Type which direction you wish to move,")
126 print("or use one of these commands:")
127 print("- Talk")
128 print("- Fight")
129 print("- Hug")
130 print("- Take")
131 print("- Backpack")
132 # quit
133 elif command == "quit":
134 running = False
135 # incorrect command
136 else:
137 print("Enter 'help' to list the copmmands.")
138 input("\nPress <Enter> key to continue")
139print("Thank you for playing Deepest Dungeon")
When you make classes, it’s a good idea to add a comment explaining what each method does. If you check the classes in room.py, item.py, and character.py, you’ll see we’ve already done this there.
Remove unused code¶
Remember the code in main.py that we commented out. Well we no longer need it. Go ahead and delete the code highlighted below.
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23
24# create characters
25ugine = Enemy("Ugine")
26ugine.description = "a huge troll with rotting teeth."
27ugine.weakness = "cheese"
28
29nigel = Friend("Nigel")
30nigel.description = "a burly dwarf with golden bead in woven through his beard."
31nigel.conversation = "Well youngan, what are you doing here?"
32
33# add characters to rooms
34armoury.character = ugine
35lab.character = nigel
36
37# create items
38cheese = Item("Cheese")
39cheese.description = "super smelly"
40
41chair = Item("Chair")
42chair.description = "designed to be sat on"
43
44elmo = Item("Elmo")
45elmo.description = "wanting to be tickled"
46
47# add items to rooms
48cavern.item = chair
49armoury.item = elmo
50lab.item = cheese
51
52'''
53# describe the rooms
54cavern.describe()
55armoury.describe()
56lab.describe()
57'''
58
59# initialise variables
60running = True
61current_room = cavern
62backpack = []
63
64# ----- MAIN LOOP -----
65while running:
66 current_room.describe()
67
68 command = input("> ").lower()
69
70 # move
71 if command in ["north", "south", "east", "west"]:
72 current_room = current_room.move(command)
73 # talk
74 elif command == "talk":
75 if current_room.character is not None:
76 current_room.character.talk()
77 else:
78 print("There is no one here to talk to")
79 # hug
80 elif command == "hug":
81 if current_room.character is not None:
82 current_room.character.hug()
83 else:
84 print("There is no one here to hug")
85 # fight
86 elif command== "fight":
87 if current_room.character is not None:
88 weapon = input("What will you fight with? > ").lower()
89 available_weapons = []
90 for item in backpack:
91 available_weapons.append(item.name)
92 if weapon in available_weapons:
93 if current_room.character.fight(weapon):
94 current_room.character = None
95 if Enemy.get_num_of_enemy() == 0:
96 print("You have slain all the enemies. You are victorious!")
97 running = False
98 else:
99 running = False
100 else:
101 print(f"You don't have {weapon}")
102 print(f"{current_room.character.name} strikes you down.")
103 running = False
104 else:
105 print("There is no one here to fight")
106 # take
107 elif command == "take":
108 if current_room.item is not None:
109 backpack.append(current_room.item)
110 print(f"You put {current_room.item.name} into your backpack")
111 current_room.item = None
112 else:
113 print("There is nothing here to take")
114 # backpack
115 elif command == "backpack":
116 if backpack == []:
117 print("It is empty")
118 else:
119 print("You have:")
120 for item in backpack:
121 print(f"- {item.name.capitalize()}")
122 # help
123 elif command == "help":
124 print("Type which direction you wish to move,")
125 print("or use one of these commands:")
126 print("- Talk")
127 print("- Fight")
128 print("- Hug")
129 print("- Take")
130 print("- Backpack")
131 # quit
132 elif command == "quit":
133 running = False
134 # incorrect command
135 else:
136 print("Enter 'help' to list the copmmands.")
137 input("\nPress <Enter> key to continue")
138print("Thank you for playing Deepest Dungeon")
Final code¶
Whitespace is the empty lines in your code. You can use it to break your code into clear sections and make it easier to read.
Finalise your code by adjusting it so to look the same as the following code:
main.py¶
1# main.py
2
3from room import Room
4from character import Enemy, Friend
5from item import Item
6
7# create rooms
8cavern = Room("Cavern")
9cavern.description = ("A room so big that the light of your torch doesn’t reach the walls.")
10
11armoury = Room("Armoury")
12armoury.description = ("The walls are lined with racks that once held weapons and armour.")
13
14lab = Room("Laboratory")
15lab.description = ("A strange odour hangs in a room filled with unknownable contraptions.")
16
17# link rooms
18cavern.link_rooms(armoury,"south")
19armoury.link_rooms(cavern,"north")
20armoury.link_rooms(lab,"east")
21lab.link_rooms(armoury,"west")
22
23# create characters
24ugine = Enemy("Ugine")
25ugine.description = "a huge troll with rotting teeth."
26ugine.weakness = "cheese"
27
28nigel = Friend("Nigel")
29nigel.description = "a burly dwarf with golden bead in woven through his beard."
30nigel.conversation = "Well youngan, what are you doing here?"
31
32# add characters to rooms
33armoury.character = ugine
34lab.character = nigel
35
36# create items
37cheese = Item("Cheese")
38cheese.description = "super smelly"
39
40chair = Item("Chair")
41chair.description = "designed to be sat on"
42
43elmo = Item("Elmo")
44elmo.description = "wanting to be tickled"
45
46# add items to rooms
47cavern.item = chair
48armoury.item = elmo
49lab.item = cheese
50
51# initialise variables
52running = True
53current_room = cavern
54backpack = []
55
56# ----- MAIN LOOP -----
57while running:
58 current_room.describe()
59
60 command = input("> ").lower()
61
62 # move
63 if command in ["north", "south", "east", "west"]:
64 current_room = current_room.move(command)
65 print(f"You travel {command}")
66 # talk
67 elif command == "talk":
68 if current_room.character is not None:
69 current_room.character.talk()
70 else:
71 print("There is no one here to talk to")
72 # hug
73 elif command == "hug":
74 if current_room.character is not None:
75 current_room.character.hug()
76 else:
77 print("There is no one here to hug")
78 # fight
79 elif command== "fight":
80 if current_room.character is not None:
81 weapon = input("What will you fight with? > ").lower()
82 available_weapons = []
83 for item in backpack:
84 available_weapons.append(item.name)
85 if weapon in available_weapons:
86 if current_room.character.fight(weapon):
87 current_room.character = None
88 if Enemy.num_of_enemy == 0:
89 print("You have slain the enemy. You are victorious!")
90 running = False
91 else:
92 running = False
93 else:
94 print(f"You don't have {weapon}")
95 print(f"{current_room.character.name} strikes you down.")
96 running = False
97 else:
98 print("There is no one here to fight")
99 # take
100 elif command == "take":
101 if current_room.item is not None:
102 backpack.append(current_room.item)
103 print(f"You put {current_room.item.name} into your backpack")
104 current_room.item = None
105 else:
106 print("There is nothing here to take")
107 # backpack
108 elif command == "backpack":
109 if backpack == []:
110 print("It is empty")
111 else:
112 print("You have:")
113 for item in backpack:
114 print(f"- {item.name.capitalize()}")
115 # help
116 elif command == "help":
117 print("Type which direction you wish to move,")
118 print("or use one of these commands:")
119 print("- Talk")
120 print("- Fight")
121 print("- Hug")
122 print("- Take")
123 print("- Backpack")
124 # quit
125 elif command == "quit":
126 running = False
127 # incorrect command
128 else:
129 print("Enter 'help' for list of commands")
130 input("\nPress <Enter> to continue")
131
132print("Thank you for playing Darkest Dungeon")
room.py¶
1# room.py
2
3class Room():
4
5 def __init__(self,room_name):
6 # initialises the room object
7 self.name = room_name.lower()
8 self.description = None
9 self.linked_rooms = {}
10 self.character = None
11 self.item = None
12
13 def describe(self):
14 # sends a description of the room to the terminal
15 print(f"\nYou are in the {self.name}")
16 print(self.description)
17 if self.character is not None:
18 self.character.describe()
19 if self.item is not None:
20 self.item.describe()
21 for direction in self.linked_rooms.keys():
22 print(f"To the {direction} is the {self.linked_rooms[direction].name}")
23
24 def link_rooms(self, room, direction):
25 # links the provided room, in the provided direction
26 self.linked_rooms[direction.lower()] = room
27
28 def move(self, direction):
29 # returns the room linked in the given direction
30 if direction in self.linked_rooms.keys():
31 return self.linked_rooms[direction]
32 else:
33 print("You can't go that way")
34 return self
character.py¶
1# character.py
2
3class Character():
4
5 def __init__(self, name):
6 # initialises the character object
7 self.name = name
8 self.description = None
9 self.conversation = None
10
11 def describe(self):
12 # sends a description of the character to the terminal
13 print(f"{self.name} is here, {self.description}")
14
15 def talk(self):
16 # send converstation to the terminal
17 if self.conversation is not None:
18 print(f"{self.name}: {self.conversation}")
19 else:
20 print(f"{self.name} doesn't want to talk to you")
21
22 def hug(self):
23 # the character responds to a hug
24 print(f"{self.name} doesn't want to hug you")
25
26 def fight(self,item):
27 # the character response to a threat
28 print(f"{self.name} doesn't want to fight you")
29 return True
30
31
32class Friend(Character):
33
34 def __init__(self, name):
35 # initialise the Friend object by calling the character initialise
36 super().__init__(name)
37
38 def hug(self):
39 # the friend responds to a hug
40 print(f"{self.name} hugs you back.")
41
42
43class Enemy(Character):
44
45 num_of_enemy = 0
46
47 def __init__(self,name):
48 # initialise the Enemy object by calling the character initialise
49 super().__init__(name)
50 self.weakness = None
51 Enemy.num_of_enemy += 1
52
53 def fight(self, item):
54 # fights enemy with provided item and returns if player survives
55 if item == self.weakness:
56 print(f"You strike {self.name} down with {item}.")
57 Enemy.num_of_enemy -= 1
58 return True
59 else:
60 print(f"{self.name} crushes you. Puny adventurer")
61 return False
62
63 def get_num_of_enemy():
64 return Enemy.num_of_enemy
item.py¶
1# item.py
2class Item():
3
4 def __init__(self,name):
5 # initialise the Item object
6 self.name = name.lower()
7 self.description = None
8
9 def describe(self):
10 # prints description of item to the terminal
11 print(f"You see {self.name} in the room. It is {self.description}.")
Final Make¶
The tutorials are finished. Now it’s your turn to make the dungeon your own by adding new features. You can check the Extension Ideas page for inspiration.
There are a few logic mistakes in the code, but you’ll need to test the game to find them and work out how to fix them. Make sure you use your debugger to help.
Good luck on your adventure.