# David Mailhot, CSE143 python lecture #2 (objects) # A Treasure object is used in a treasure hunt and is searched for on # a drawingpanel. # It contains a treasure string (duh!) as well as information of it's # x/y position in a drawingpanel. from math import * class Treasure: # constructor with specified arguments. x & y represent the center # of this treasure object. booty is the contents of this treasure. def __init__(self, x, y, width, height, color, booty): self.x = x self.y = y self.width = width self.height = height self.color = color self.booty = booty # use the specified drawingpanel to draw itself def draw(self, panel): topleft_x = self.x - self.width/2 topleft_y = self.y - self.height/2 panel.canvas.create_rectangle(topleft_x, topleft_y, topleft_x + self.width, topleft_y + self.height, fill=self.color) def distance(self, x, y): dx = self.x - x dy = self.y - y return sqrt(dx * dx + dy * dy) # return the contents of this treasure def __str__(self): return str(self.booty)