When our user gets stuck and forgets what they can possibly do, we may want them to be able to type a command that lists the possible commands for them to explore with. For this functionality we need to:
create a dictionary of commands
create a help command
1. Create a dictionary of commands
In order to put instructions in, we need to have a list of commands that the user can use to interact with the game.
In main.py
.
.
.
current_room = kitchen
backpack = []
# add a dictionary of all the commands the user can input with their descriptions assigned as values
commands = {
"north": "move to the room north of where you are",
dead = False
while dead == False:
.
.
.
2. Change the link_room( ) method
The goal is to have the primary room add the destination room to the linked_rooms attribute, and then add the primary room to the to the destination room's linked_rooms attribute.
In room.py
.
.
.
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
compliment = {
'north': 'south',
'east': 'west',
'up': 'down',
'left': 'right'
}
# add the link for the other room direction
room_to_link.linked_rooms[compliment[direction]] = self
self.linked_rooms[direction] = room_to_link
def get_details(self):
print(self.name)
print("--------------------")
.
.
.
3. Change the link_room( ) instructions from main.py
Now that we have changed the method, we need to change how we call it in the main game.
In main.py
.
.
.
print("There are " + str(Room.number_of_rooms) + " rooms to explore.")
# only link the rooms that are north and east
kitchen.link_room(dining_hall, "south")
dining_hall.link_room(kitchen, "north")
dining_hall.link_room(ballroom, "west")
ballroom.link_room(dining_hall, "east")
dave = Enemy("Dave", "A smelly zombie")
dave.set_conversation("Brrlgrh... rgrhl... brains...")
.
.
.
Extra: Make oneway passages
If we want a link to only be one way, we now need to define a new method to remove a link that we have already done.
in room.py
.
.
.
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
compliment = {
'north': 'south',
'east': 'west',
'up': 'down',
'left': 'right'
}
room_to_link.linked_rooms[compliment[direction]] = self
self.linked_rooms[direction] = room_to_link
# add a method to remove a link that exists in a room
def remove_link(self, direction):
if direction in self.linked_rooms:
del self.linked_rooms[direction]
def get_details(self):
print(self.name)
print("--------------------")
.
.
.
then in main.py, remove the direction that you do not want to have linked.
.
.
.
print("There are " + str(Room.number_of_rooms) + " rooms to explore.")
dining_hall.link_room(kitchen, "north")
ballroom.link_room(dining_hall, "east")
# provide the instruction to remove the link from the room. In this case removing ballroom link from the dining_hall
dining_hall.remove_link("west")
dave = Enemy("Dave", "A smelly zombie")
dave.set_conversation("Brrlgrh... rgrhl... brains...")
.
.
.