Stage 5 - Item Creation¶
Introduction¶
Now our dungeon is starting to take shape. The user can move between multiple rooms in which they can interact with different characters. We have two different types of characters, with different user interactions.
In this stage we will continue to fill out our dungeon. Most Dungeon crawlers will contain different items that the player can interact with, so let’s create some items.
To achieve this we will:
Define an Item class
Create Item objects
Add the Item objects to the rooms
Include the Item objects in the room description.
Class Diagram¶
The new class diagram shows our new Items class, as well as the new item attribute in our Room class.
Define the Item class¶
In Thonny create a new file and enter the code below. Then save it as item.py in the same folder as main.py, character.py and room.py.
1# item.py
2
3class Item():
4
5 def __init__(self,name):
6 # initialise the Item object
7 self.name = name.lower()
8 self.description = None
In investigating the code everything should be familiar:
defining the
Item
classdefining our
__init__
methodassigning the
name
argument to theself.name
attributecreating a placeholder
self.description
attribute to be assigned a value later
Create Item objects¶
To create the Item objects, move to the main.py file and add the 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# 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'''
47# describe the rooms
48cavern.describe()
49armoury.describe()
50lab.describe()
51'''
52
53# initialise variables
54running = True
55current_room = cavern
56
57# ----- MAIN LOOP -----
58while running:
59 current_room.describe()
60
61 command = input("> ").lower()
62
63 if command in ["north", "south", "east", "west"]:
64 current_room = current_room.move(command)
65 elif command == "talk":
66 if current_room.character is not None:
67 current_room.character.talk()
68 else:
69 print("There is no one here to talk to")
70 elif command == "hug":
71 if current_room.character is not None:
72 current_room.character.hug()
73 else:
74 print("There is no one here to hug")
75 elif command== "fight":
76 if current_room.character is not None:
77 weapon = input("What will you fight with? > ").lower()
78 if current_room.character.fight(weapon):
79 current_room.character = None
80 else:
81 running = False
82 else:
83 print("There is no one here to fight")
84 elif command == "quit":
85 running = False
86 else:
87 print("I don't understand.")
Predict what you think will happen and run the code.
Again, in investigating the code this should all be familiar.
For each of the items, we have:
imported our new class (Item)
called
Item()
to create a Item object and then assigned it to a variableassigned a string to the Item object’s
description
attribute
Add the item object to the the rooms.¶
To add the Item objects to the rooms, we need to first adjust the Room
class, so go to room.py and add the code below.
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 for direction in self.linked_rooms.keys():
20 print(f"To the {direction} is the {self.linked_rooms[direction].name}")
21
22 def link_rooms(self, room_to_link, direction):
23 # links the provided room, in the provided direction
24 self.linked_rooms[direction.lower()] = room_to_link
25
26 def move(self, direction):
27 # returns the room linked in the given direction
28 if direction in self.linked_rooms.keys():
29 return self.linked_rooms[direction]
30 else:
31 print("You can't go that way")
32 return self
Save the room.py file and then return to the main.py file, add the highlighted code:
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'''
52# describe the rooms
53cavern.describe()
54armoury.describe()
55lab.describe()
56'''
57
58# initialise variables
59running = True
60current_room = cavern
61
62# ----- MAIN LOOP -----
63while running:
64 current_room.describe()
65
66 command = input("> ").lower()
67
68 if command in ["north", "south", "east", "west"]:
69 current_room = current_room.move(command)
70 elif command == "talk":
71 if current_room.character is not None:
72 current_room.character.talk()
73 else:
74 print("There is no one here to talk to")
75 elif command == "hug":
76 if current_room.character is not None:
77 current_room.character.hug()
78 else:
79 print("There is no one here to hug")
80 elif command== "fight":
81 if current_room.character is not None:
82 weapon = input("What will you fight with? > ").lower()
83 if current_room.character.fight(weapon):
84 current_room.character = None
85 else:
86 running = False
87 else:
88 print("There is no one here to fight")
89 elif command == "quit":
90 running = False
91 else:
92 print("I don't understand.")
Predict what you think will happen and then run the program.
Investigating this code, and it will all look familiar to you.
For each item we assigned it to the item
attribute of a Room object.
Include the Item objects in the room description¶
Despite writing all this code, your program should run the same as when we started. That’s because none of these changes have been outputted. For the Item objects we are going to follow the same process that we did for the Character objects:
create a
describe
method in theItem
classcall the Item
describe
method from the Roomdescribe
method
First we need to go to the item.py file and add the code below:
1# item.py
2
3class Item():
4
5 def __init__(self,name):
6 # initialise the Item object
7 self.name = name.lower()
8 self.description = None
9
10 def describe(self):
11 # prints description of item to the terminal
12 print(f"You see {self.name} in the room. It is {self.description}.")
In investing this code, you should already recognise all the elements:
defining method
method describing comment
display to terminal using attributes of the object.
Now head to room.py and add the following code:
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_to_link, direction):
25 # links the provided room, in the provided direction
26 self.linked_rooms[direction.lower()] = room_to_link
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
Predict and run the code.
Investigate the code and you will notice that it is very similar to the character description:
check if there is an item in the room
when an item is present, call it’s describe method.
Go a step further and test the code by going to each room and checking that the correct item is displayed.
Room |
Item expected |
Item described |
---|---|---|
Cavern |
chair |
|
Armoury |
elmo |
|
Lab |
cheese |
Stage 5 task¶
Now it is time for your to implement the Make phase.
You should:
make an additional item for each of your additional rooms
add your additional items to their room.