Andrew Hansen Help

Classes source code

This is a complete dump of the classes module:

from random import randint class Item: # Class names begin with upper case ''' Class to define all simple objects within the game. Attributes: name: str desc: str collectable: bool (overloaded) Methods: get_desc(): Used to return description. This may be a complicated process in some functions ''' def __init__(self, name, desc): # This is a constructor self.name = name self.desc = desc self.collectable = True def get_desc(self): # This is a getter return self.desc class Location: ''' Class to define all location objects within the game. Attributes: name: str open_desc: str closed_desc: str (overloaded) items: list open: bool. False if the location is locked or closed. exits: list Methods: get_desc(): Used to return description. This may be a complicated process in some functions ''' def __init__(self, name, open_desc, exits, closed_desc = ''): self.name = name self.open_desc = open_desc self.closed_desc = closed_desc # Overloaded self.items = [] self.open = True self.exits = exits def get_desc(self): ''' Complex description function. :return: Closed desc only if self.open is false. Open desc + list of items if self.open is true. ''' if self.open is True: desc_str = self.open_desc if len(self.items) != 0: for item in self.items: desc_str += 'There is a ' + item.name + ' here.\n' else: desc_str = self.closed_desc return desc_str 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 class Complex(): ''' Class to define all Complex objects within the game. Complex object are those that can be manipulated or have to be opened Attributes: name: str open_desc: str closed_desc: str trigger: str contents: list open: bool (overloaded) Methods: get_desc(): Used to return description. This may be a complicated process in some functions ''' def __init__(self, name, open_desc, closed_desc, trigger): self.name = name self.open_desc = open_desc # Desc when closed. self.closed_desc = closed_desc # Desc when opened self.trigger = trigger # Something to test for when user tries to open self.contents = [] # Starts empty. We can put items into it. self.open = False # Starts off closed def get_desc(self): if self.open == False: # If container is closed return self.closed_desc # return the closed desc else: ret_str = self.open_desc + '\n' # Start a return string, note CR if len(self.contents) == 0: # If container is empty ret_str += 'It is empty.' # Say "it's empty" else: for item in self.contents: # If there is stuff in here, ret_str += 'There is a ' + item.name + ' here.\n' # List each item in turn. return ret_str class World: ''' Class to define the World within the game. This class is exceedingly simple having only one attribute: grid, and one method: add_location() which is only being used for simplicity. Attributes: grid: list Methods: add_location(): ''' def __init__(self, rows, cols): self.grid = [] # create an empty grid for r in range(rows): # for each row ... row = [] # create an empty list for c in range(cols): # for each column row.append([]) # add an empty list to row self.grid.append(row) # Stick the whole row in the grid def add_location(self, row, col, location): self.grid[row][col] = location class Player(): ''' Class to define the Player object within the game. Attributes: row: int col: int items: list Methods: do_move() # Used to change the players location. get_inventory() # Used to list the objects the player is carrying. get_location() # Simplifies the process of getting the players row and col ''' def __init__(self, init_row, init_col): self.row = init_row self.col = init_col self.items = [] def do_move(self, direction): if direction == 'north': self.row = self.row + 1 if direction == 'south': self.row = self.row - 1 if direction == 'east': self.col = self.col + 1 if direction == 'west': self.col = self.col - 1 def get_inventory(self): for item in self.items: print(f'You have a {item.name}.\n') def get_location(self): return [self.row, self.col] if __name__ == '__main__': pass
Last modified: 07 April 2024