Extension - Player Class¶
In this extensions tutorial, we will refactor our code to create a Player class. This will give the game a player object which we can use to add features associated with the player (health, inventory, gear, weapons etc.). The first feature we will add is the player inventory, known in our game as the backpack.
Class Diagram¶
Currently the code dealing with the backpack is held in main.py
. Before we plan our new class, we need to look at this code an identify all relevant feature we need to incorporate. So let’s look at the main.py
from the end of the standard tutorials.
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")
You will notice that there are four places that main.py
interacts with the player’s backpack.
line 54 → defines the backpack variable as an empty list
lines 82 - 85 → checks if chosen weapon is in the backpack
lines 101 - 106 → adds item to backpack
lines 109-114 → displays the contents of the backpack
If we were to move these features to a Player class, we need to consider the nature of the four features:
the backpack describes part of the player → attribute
checking for weapon in backpack is an action → method
adding an item to backpack is an action → method
displaying the contents of the backpack is an actions → method
Therefore the class diagram would look like this:
Now that we have a plan. Lets implement it in our code.