The Actor class
The object classes will all be kept in a single file called game_classes.py. Each class will be added to the bottom of the file and tested / demonstrated in the "if name == 'main':" block.
The Actor class will need some special methods to make the game more engaging. The actor will need to have a few different responses to make them interesting even if the ultimate game play is still linear. The class could be extended so that different interactions produce different results.
The class diagram is:
The Python code for the actor is:
class Actor():
'''
Class to define all Actor objects within the game.
Note, this class requires the random library to be imported.
Attributes:
name: str
desc: str
vocab: list
trigger: str
satisfied: bool (overloaded)
Methods:
get_desc(): Used to return description. This may be a complicated process in some functions
'''
def __init__(self, name, desc, vocab, trigger):
self.name = name
self.desc = desc
self.vocab = vocab
self.trigger = trigger
self.satisfied = False
def get_desc(self):
return self.desc
def get_response(self):
'''
Function to return a response when actor is spoken to.
The entire vocab is stored in the vocab list. There can be any number of "unsatisfied" responses
which will be selected at random to make the actor more engaging.
The final response in the list is the response for when the actor is "satisfied".
:return: A response from vocab[0] to len(vocab)-2 if unsatisfied, vocab[len(vocab)-1] if satisfied.
'''
if not self.satisfied:
if len(self.vocab) == 1:
return self.vocab[0]
else:
resp_index = randint(0, len(self.vocab) - 2) # The last vocab item is the triggered response
return self.vocab[resp_index]
else:
return self.vocab[-1] # The last one
Note that the get_response() method requires a random number so add
from random import randint
to the top of the classes file.
The get_response() method is written with the idea that the actor will have a number of responses prior to getting what they want but only one response once they are happy. The satisfied response is the last one in the list and has the index len(vocab)-2. It would be a good idea if the actor then left the game so we don't have to plan any further discussions after the actor has done their job.
Here is the test for the Actor class:
if __name__ == '__main__':
joe = Actor('Joe the cleaner',
'Joe is a cleaner. He is dressed in overalls and carrying a mop.',
['I will lend you my key if you give me a picnic bar.',
'You will need to get me a picnic bar if you want to borrow my key.',
'Thank you for the picnic bar. Here is my key.'],
'picnic'
)
print(joe.get_desc()) # Joe is a cleaner. He is dressed in overalls and carrying a mop.
print(joe.get_response()) # You will need to get me a picnic bar if you want to borrow my key.
print(joe.get_response()) # I will lend you my key if you give me a picnic bar.
print(joe.get_response()) # I will lend you my key if you give me a picnic bar.
print(joe.get_response()) # You will need to get me a picnic bar if you want to borrow my key.
if joe.trigger == 'picnic': # The "picnic" will be taken from the user input during the game
joe.satisfied = True
print(joe.get_response()) # Thank you for the picnic bar. Here is my key.
print(joe.get_response()) # Thank you for the picnic bar. Here is my key.
Note that actual outputs from the get_response() method prior to triggering will vary depending on how many responses are provided and what the randint generates. Having the two print statements after Joe is triggered is to confirm that only one response is provided and that is the last one in the list.
Last modified: 07 April 2024