Write a program using an object
Start to create your object-oriented text-based adventure game
Extend other people’s classes
Create an enemy in the room
Recap Week 3
Extending your knowledge of OOP
Finish your game
End of the course
Your game needs to have lots of rooms, and so you need to add some attributes and methods to handle linking multiple room objects together.
In order to do this, you’ll add another attribute to a Room object, containing a dictionary of all of the rooms that it is linked to. You may not have encountered a dictionary data structure before. Dictionaries are similar to lists, but allow you to give each element a name. Here is an example of a dictionary that stores the winners of various medals:
winners = { "gold": "Alex", "silver": "Brian", "bronze": "Clare"}
print( winners["gold"] )
>>> Alex
As you can see, you can ask the dictionary for a specific element by name. This will be useful in your adventure game, because you can ask for the room in a particular direction. For example, here is how you would refer to the room to the east:
self.linked_rooms["east"]
Go back to your Room class, locate the __init__ constructor, and add a new attribute called linked_rooms.
def __init__(self, room_name):
...
self.linked_rooms = {}
The linked_rooms = {} code creates an empty dictionary; it’s empty because at the moment, the room isn’t linked to any other rooms.
Next, you need to add a method so that you can link rooms together. Add this new method below the other methods:
class Room():
...
def describe(self):
print( self.description )
# Add link_room method here
Add the link_room method:
def link_room(self, room_to_link, direction):
self.linked_rooms[direction] = room_to_link
This method takes three parameters: the object itself (which you can ignore when you use the method), the room object to link to, and the relative direction of this object.
Here is a diagram of how the rooms will be laid out:
Go back to your main.py code
Create the additional rooms shown in the diagram above
Set descriptions for all of your room objects
The dining hall is to the south of the kitchen, so I am going to use the link_room method on the kitchen object in my main.py file, like this:
kitchen.link_room(dining_hall, "south")
from room import Room
kitchen = Room("kitchen")
kitchen.set_description("A dank and dirty room buzzing with flies")
# you can remove this line now
kitchen.describe() # remove
dining_hall = Room("dining hall")
dining_hall.set_description("A large room with ornate golden decorations on each wall")
ballroom = Room("ballroom")
ballroom.set_description("A vast room with a shiny wooden floor; huge candlesticks guard the entrance")
# Add your link room code here
If you would like to see what’s inside the dictionary, add this line of code inside the link_room method in room.py to display its contents:
print( self.name + " linked rooms :" + repr(self.linked_rooms) )
class Room():
...
def link_room(self, room_to_link, direction):
self.linked_rooms[direction] = room_to_link
# Add the code here (make sure it is indented)
Run the main.py code and you will see something similar to this, which shows that there is a Room object in the specified direction:
Kitchen linked rooms :{'south': <room.Room object at 0x03A22770>}
This code is not necessary for the game; I am just using it to show you how the dictionary gets built up. Once you have seen how it works, comment that line out by putting a # at its start.
Your Room class should now look similar to this, with getter and setter methods for name, and methods to describe the room and link it to other rooms.
class Room():
def __init__(self, room_name):
self.name = room_name
self.description = None
self.linked_rooms = {}
def set_description(self, room_description):
self.description = room_description
def get_description(self):
return self.description
def set_name(self, room_name):
self.name = room_name1
def get_name(self):
return self.name
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
self.linked_rooms[direction] = room_to_link
# print( self.name + " linked rooms :" + repr(self.linked_rooms) )
Use the link_room method in main.py to link the rooms together in your game so that it matches the diagram above.
Don’t forget that links go only one way:
kitchen.link_room(dining_hall, "south")
This code links from the kitchen to the dining hall. However, at the moment there is no link back from the dining hall to the kitchen, so the player will be stuck there forever!
It is therefore important that there is a link in the opposite direction:
dining_hall.link_room(kitchen, "north")
The room you are linking from is the object you call the method on, and the room you are linking to is the object you pass into the method.