Stage 8 - Useability¶
Introduction¶
Our dungeon is complete. We have a fully functioning game, but before we sign-off we need to improve the useability of our program and tidy up our code.
To do this we will:
Create a help command
Improve the user experience and user interface
Add a farewell message
Add some in-code comments
Remove unused code
Standardise our white-space
Help command¶
We know all the commands we can use because we wrote the code. Someone else might not know what they can say. In addition, if they don’t enter one of the commands, the program only says I don't understand.
, which is far from helpful.
To make life easier for new players, we should create a help command which lists all the commands. But how will the player know about the help command, without using the help command? Simple, we change the catch-all event handler (under the else:
) to inform the user about the help command.
To implement this, add the highlighted code below 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 current_room.character = None
91 if Enemy.get_num_of_enemy() == 0:
92 print("You have slain all the enemies. You are victorious!")
93 running = False
94 else:
95 running = False
96 else:
97 print(f"You don't have {weapon}")
98 print(f"{current_room.character.name} strikes you down.")
99 running = False
100 else:
101 print("There is no one here to fight")
102 elif command == "take":
103 if current_room.item is not None:
104 backpack.append(current_room.item)
105 print(f"You put {current_room.item.name} into your backpack")
106 current_room.item = None
107 else:
108 print("There is nothing here to take")
109 elif command == "backpack":
110 if backpack == []:
111 print("It is empty")
112 else:
113 print("You have:")
114 for item in backpack:
115 print(f"- {item.name.capitalize()}")
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 elif command == "quit":
125 running = False
126 else:
127 print("Enter 'help' to list the copmmands.")
By now this code should be familiar and makes sense to you.
Improving the UI and UX¶
Although our program is visually very simple, the user stills interacts with it, which means we need to consider the UI and the UX.
UI and UX
UI stands for User Interface, which is basically what you see on your screen when you use an app or a website. This includes things like buttons, menus, icons, and colors. UI design focuses on making things look good and easy to use.
UX stands for User Experience, which is how you feel when you use an app or a website. It’s about how easy it is to use, how it makes you feel, and whether it helps you achieve what you’re trying to do. UX design focuses on making things easy and enjoyable to use.
We have already addressed some UI and UX problems by adding the help command. Play the game and see if you can identify anything else.
Did you notice that after entering a command, the game responds and then quickly describes the room again. It’s really easy to loose the command response in this quick action. Let’s change that by writing the response and then asking the user to press a key to proceed.
I implement 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 current_room.character = None
91 if Enemy.get_num_of_enemy() == 0:
92 print("You have slain all the enemies. You are victorious!")
93 running = False
94 else:
95 running = False
96 else:
97 print(f"You don't have {weapon}")
98 print(f"{current_room.character.name} strikes you down.")
99 running = False
100 else:
101 print("There is no one here to fight")
102 elif command == "take":
103 if current_room.item is not None:
104 backpack.append(current_room.item)
105 print(f"You put {current_room.item.name} into your backpack")
106 current_room.item = None
107 else:
108 print("There is nothing here to take")
109 elif command == "backpack":
110 if backpack == []:
111 print("It is empty")
112 else:
113 print("You have:")
114 for item in backpack:
115 print(f"- {item.name.capitalize()}")
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 elif command == "quit":
125 running = False
126 else:
127 print("Enter 'help' to list the copmmands.")
128 input("\nPress <Enter> key to continue")
Save the file, predict and then run the code.
How does that work? Let’s investigate:
input("\nPress <Enter> key to continue")
→ this is a hack. That is we are usinginput
in an unconventional way to achieve our desired outcome.the normal operation of
input
is to wait for the user response → addresses the pausing of the gamenormally the user enters their response by pressing the Enter key → stops the pausing
the value the user enters is not assigned to a variable, so it just disappears
Farewell message¶
When the game ends, it just stops. Whether the player won or lost it just exits the the Shell prompt. To make things a little more polite, we should add an farewell message.
To include a farewell message 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 current_room.character = None
91 if Enemy.get_num_of_enemy() == 0:
92 print("You have slain all the enemies. You are victorious!")
93 running = False
94 else:
95 running = False
96 else:
97 print(f"You don't have {weapon}")
98 print(f"{current_room.character.name} strikes you down.")
99 running = False
100 else:
101 print("There is no one here to fight")
102 elif command == "take":
103 if current_room.item is not None:
104 backpack.append(current_room.item)
105 print(f"You put {current_room.item.name} into your backpack")
106 current_room.item = None
107 else:
108 print("There is nothing here to take")
109 elif command == "backpack":
110 if backpack == []:
111 print("It is empty")
112 else:
113 print("You have:")
114 for item in backpack:
115 print(f"- {item.name.capitalize()}")
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 elif command == "quit":
125 running = False
126 else:
127 print("Enter 'help' to list the copmmands.")
128 input("\nPress <Enter> key to continue")
129print("Thank you for playing Deepest Dungeon")
In-code comments¶
We already have some in-code comment that helps structure our code, but we have missed much of the main loop. We should add some comment to the main loop to make our code more readable and therefore maintainable.
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 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")
When you create classes your should really have a comment that explains what each method does. If you look at the classes in our room.py, item.py and character.py files you will see that we have already done this.
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¶
White space is the blank lines between our code. You can use this to help structure your code.
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 now over. It is time to make this dungeon yours by adding new features. The Extensions Ideas page has some suggestions.
The current code does have a couple of logical errors, but you will have to test it to find out what they are and then try and solve them. Don’t forget to use your debugger to help you with this.
Good luck. May your adventure be long and fruitful.