Sometimes we would like our characters to change their speech depending of factors throughout the game. This could be introduction text, interaction text, post-event text and so on. For this functionality we need to:
change conversation to a list of conservations
change the converation setter to append
change the talk method
create a way for the control attribute to be changed
1. change conversation attribute to a list and a index
We need a place to store the conversations and change the setter to adding the .
In charcter.py
class Character():
# Create a character
def __init__(self, char_name, char_description):
self.name = char_name
self.description = char_description
# change the conversation attribute to an empty list and add a controlling atttribute
self.conversation = None []
self.conversation_index = 0
.
.
.
2. change the conversation setter to append into the new attribute
.
.
.
# Set what this character will say when talked to
# change the setting of the conversation to an appending to a list
def set_conversation(self, conversation):
self.conversation = conversation
"""sets the conversation to a list of interactions
# arguments: conversation - list of entries ex [introText, secondConversation, thirdConversation]
for entry in conversation:
self.conversation.append(conversation)
# Talk to this character
def talk(self):
.
.
.
3. change the talk method to speak the text in the list's index
In character.py
.
.
.
def talk(self):
if self.conversation is not None:
print("[" + self.name + " says]: " + self.conversation[self.conversation_index])
else:
print(self.name + " doesn't want to talk to you")
.
.
.
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...")
.
.
.