Displaying rooms
This was our original example from step 2.2 showing how a room description might look in the game:
The Dining Hall
-------------------
A large room with ornate golden decorations on every wall
The Kitchen is north
The Ballroom is west
Let’s add a new method to the Room
class to report the room name, description, and the directions of all the rooms connected to it. Go back to the room.py file and, below the link_room
method, add a new method which will display all of the rooms linked to the current room object. Don’t forget to make sure the new method is indented, just like all the other methods.
def get_details(self):
for direction in self.linked_rooms:
room = self.linked_rooms[direction]
print( "The " + room.get_name() + " is " + direction)
This method loops through the dictionary self.linked_rooms
and, for every defined direction, prints out that direction and the name of the room in that direction.
Go back to the main.py file and, at the bottom of your script, call this method on the dining hall object, then run the code to see the two rooms linked to the dining hall.
dining_hall.get_details()
Challenge
-
Add some code to the
get_details()
method so that it also prints out the name and description of the current room, as in our original example. Remember that we can refer to the current room asself
inside the method. -
Check that your
get_details()
method works for any room object by calling it on the kitchen and ballroom as well.
© CC BY-SA 4.0