'''building.py Started in class on 5/1/09, Jeff Ondich & friends Eventually, objects of the Building class will represent buildings for a 2-dimensional city-drawing program. ''' from graphics import * import time import random class Building: def __init__(self, x, y, width, height, color): lowerLeft = Point(x, y) upperRight = Point(lowerLeft.x + width, lowerLeft.y - height) self.rect = Rectangle(lowerLeft, upperRight) self.rect.setFill(color) # make one window windowGap = width / 10 windowWidth = 2 * windowGap windowHeight = 3 * windowGap if windowHeight > height - 2 * windowGap: windowHeight = height - 2 * windowGap if windowHeight < 0: windowHeight = 0 lowerLeft = Point(x + windowGap, y - height + windowGap + windowHeight) upperRight = Point(x + windowGap + windowWidth, y - height + windowGap) self.window = Rectangle(lowerLeft, upperRight) self.window.setFill(color_rgb(0, 0, 0)) def draw(self, window): self.rect.draw(window) self.window.draw(window) # some code to draw windows on the building # some code to draw an antenna def setColor(self, color): self.rect.setFill(color) # Here, we instantiate and draw a couple buildings with # random x-coordinates, heights and widths. # How would you go about making the colors random? window = GraphWin('Building test', 500, 350) x = random.randint(50, 450) height = random.randint(100, 300) width = random.randint(50, 150) b = Building(x, 300, width, height, color_rgb(0,0,255)) b.draw(window) x = random.randint(50, 450) height = random.randint(100, 300) width = random.randint(50, 150) b2 = Building(x, 300, width, height, color_rgb(255,0,255)) b2.draw(window) #time.sleep(1.5) #b2.setColor(color_rgb(0, 0, 0)) #b2.draw(window) s = raw_input('Hit Enter to quit ')