Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 1 secondGo back to your Room class. Locate the attributes self.name self.description, and underneath them, add a new attribute called linked rooms, which for now is a blank dictionary.

Skip to 0 minutes and 15 secondsNow let's add another method to allow us to link rooms together. This method takes three arguments-- the object itself, which we can ignore, the Room object to link to, and the direction this object is in.

Skip to 0 minutes and 34 secondsHere is a diagram of how I would like my rooms to be laid out. As you can see, the kitchen is to the north of the dining hall, and the ballroom is to the west. Change to your main.py file. Create the dining room object and give it a description.

Skip to 1 minute and 1 secondThe dining hall is to the south of the kitchen, so I'm going to use the link room method on the kitchen object, like this.

Skip to 1 minute and 11 secondsTo show you how the dictionary looks, I'm going to go back to the Room class and add a line of code inside the link room method to display the contents of the dictionary.

Skip to 1 minute and 26 secondsRun in the main.py code and you will see something similar to this. South is the key in the dictionary, and the room object is the part which says room.roomobject.

Skip to 1 minute and 40 secondsThis code is not necessary for the game. I'm just using it to show you how the dictionary gets built up. You can remove it once you get the idea.

Linking rooms

In our game we would like to have lots of rooms, and so we need to add some attributes and methods to handle linking multiple room objects together.

We will add a dictionary of all of the rooms which are linked to a Room object. 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, we can ask the dictionary for a specific element by name. This will be useful in our adventure game, because we can ask for the room in a particular direction. For example, here is how we would refer to the room to the east:

self.linked_rooms["east"]

Go back to your Room class, locate the attributes self.name and self.description and below them add a new attribute called linked_rooms.

self.linked_rooms = {}

The linked_rooms = {} code creates an empty dictionary; its empty because at the moment the room isn’t linked to any other rooms.

Now let’s add a method to allow us to link rooms together.

New methods are added below the other methods:

add-code screenshot

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 we can ignore when we use the method), the room object to link to, and the relative direction of this object.

Here is a diagram of how I would like my rooms to be laid out:

blueprint

Challenge

challengeicon

  • Go back to your main.py code. Underneath your existing code, create two more objects to represent the dining_hall and the ballroom
  • Set names and 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")

add-second-code screenshot

See inside the dictionary

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 the contents of the dictionary:

print( self.name + " linked rooms :" + repr(self.linked_rooms) )

add-third-code screenshot

Run the main.py code and you will see something similar to this:

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. Comment it out by putting a # at the start of the line when you have seen how it works.

Challenge

challengeicon

  • Use the link_room method three more times in main.py to link the rooms together to match the diagram above. Don’t forget that links only go 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!

The room we are linking from is the object we call the method on, and the room we are linking to is the object we pass into the method.

If you would like some help, you can see all of the code so far as a Trinket here.

Share this video:

This video is from the free online course:

Object-oriented Programming in Python: Create Your Own Adventure Game

Raspberry Pi Foundation

Contact FutureLearn for Support