'''A class for representing a simple graphical target. Jeff Ondich, 1/22/07 This class depends on the "graphics.py" library written by John Zelle for his textbook "Python Programming: An Introduction to Computer Science." ''' from graphics import * class Target: def __init__(self, center, radius, nRings): ''' Initializes the Target object's center (a Point), radius (an integer), and number of rings (an integer). Sets the ring colors to default values of red and white. ''' self.center = center self.radius = radius self.numberOfRings = nRings self.outerColor = color_rgb(200, 0, 0) self.innerColor = color_rgb(255, 255, 255) def setCenter(self, center): self.center = center def getCenter(self): return self.center def setRadius(self, radius): self.radius = radius def getRadius(self): return self.radius def setNumberOfRings(self, nRings): self.numberOfRings = nRings def getNumberOfRings(self): return self.numberOfRings def setOuterColor(self, color): self.outerColor = color def getOuterColor(self): return self.outerColor def setInnerColor(self, color): self.innerColor = color def getInnerColor(self): return self.innerColor def draw(self, window): ringSeparation = self.radius / self.numberOfRings currentRadius = self.radius currentColor = self.outerColor for k in range(self.numberOfRings): circle = Circle(self.center, currentRadius) circle.setFill(currentColor) circle.draw(window) currentRadius = currentRadius - ringSeparation if currentColor == self.outerColor: currentColor = self.innerColor else: currentColor = self.outerColor if __name__ == '__main__': window = GraphWin('Target demo', 800, 400) window.setBackground(color_rgb(255, 255, 255)) # Using the default colors, create and draw a target with 5 rings. target = Target(Point(150, 200), 100, 5) target.draw(window) # Change all the features of the target and draw it again. target.setOuterColor(color_rgb(0, 200, 50)) target.setInnerColor(color_rgb(0, 50, 200)) target.setNumberOfRings(7) target.setRadius(50) target.setCenter(Point(400, 200)) target.draw(window) # Change the center and one of the colors, and draw it yet again. target.setCenter(Point(600, 200)) target.setOuterColor(color_rgb(0, 0, 0)) target.draw(window) s = raw_input('Hit Enter to quit')