# CSE 143 Python session #2 (objects) # Author: David Mailhot # The TreasureManager class runs a treasure-hunting game with the help of the # DrawingPanel and TreasureMain classes. It keeps track of a list of # Treasure objects, as well as locations that the user has 'dug' for with # Hole objects. # 'Digging' by the user gradually reveals parts of the game panel. # Any treasure revealed by digging are displayed. # The user interacts with the game panel: # A left-click 'digs' a hole and reveals any buried treasures in the area. # A right-click (or a Ctrl-left-click for Mac people) on a found treasure # displays the contents of that treasure. from treasure import * from hole import * DIG_RADIUS = 30 class TreasureManager(): # list buried of treasures unrevealed # list found of treasures revealed # list digs of holes dug def __init__(self): self.buried = [] self.found = [] self.digs = [] # adds the specified treasure object def add_treasure(self, chest): self.buried.append(chest) # draws all holes dug and only revealed treasures def draw_all(self, panel): for hole in self.digs: hole.draw(panel) for chest in self.found: chest.draw(panel) # 'digs' a hole at the specified x/y coordinates, revealing any treasures # that fall within the DIG_RADIUS def dig(self, x, y): self.digs.append( Hole(x,y,DIG_RADIUS) ) for chest in self.buried: if self.within_click(chest, x, y, DIG_RADIUS, DIG_RADIUS): self.found.append(chest) self.buried.remove(chest) # displays the contents of any treasure that the specified x/y coordinates touch def open_treasure(self, x, y): success = False for chest in self.found: if self.within_click(chest, x, y, chest.width, chest.height): print("In this treasure be "+str(chest)+"\n") success = True if not success: print("Arrr, no treasure be thereā€¦") # helper to determine whether the specified treasure is near the specified # x/y coordinates, using the distances specified of w width and h height def within_click(self, chest, x, y, w, h): distance = chest.distance(x,y) return (distance < w or distance < h)