Andrew Hansen Help

The Location 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 Location class is only slightly more complex than the Item class. In addition to the name and description it only needs a list of items and the ability to add and remove them. This is the class diagram:

Location class

  • name

  • open_desc

  • closed_desc

  • state

  • exits

  • get_desc()

The Python code for the Location class is:

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

Note that only the open description has access to the items. The expectation is that locations such as this have their items on the inside. Locations such as the quad will start in the open state.

Start with a basic test of the descriptions for locked and unlocked.

if __name__ == '__main__': admin = Location('Admin', 'You are standing in the Admin office.\nThere is a small safe on the shelves behind the desk.\nThere is a photo of Kurt Cobain on the wall with the letters RIP under it.\nFrom here you can go south to the quad.\n', ['south'], 'You are standing outside the Admin office but it is locked.\nFrom here you can go south to the quad.\n', ) admin.open = False # Close the location print(admin.get_desc()) # You are standing outside the Admin office but it is locked. # From here you can go south to the quad. admin.open = True # Open the location print(admin.get_desc()) # You are standing in the Admin office. # There is a small safe on the shelves behind the desk. # There is a photo of Kurt Cobain on the wall with the letters RIP under it. # From here you can go south to the quad.

To test the items feature we need to create an item and add it to the location.

if __name__ == '__main__': coin = Item('Coin', 'No description req.') admin = Location('Admin', 'You are standing in the Admin office.\nThere is a small safe on the shelves behind the desk.\nThere is a photo of Kurt Cobain on the wall with the letters RIP under it.\nFrom here you can go south to the quad.\n', ['south'], 'You are standing outside the Admin office but it is locked.\nFrom here you can go south to the quad.\n', ) admin.open = False # Close the location admin.items.append(coin) # Put the coin in the Admin office print(admin.get_desc()) # You are standing outside the Admin office but it is locked. # From here you can go south to the quad. admin.open = True # Open the location print(admin.get_desc()) # You are standing in the Admin office. # There is a small safe on the shelves behind the desk. # There is a photo of Kurt Cobain on the wall with the letters RIP under it. # From here you can go south to the quad. # There is a Coin here.
Last modified: 07 April 2024