''' building.py Feb 10, 2012 Written by Jeff Ondich and friends to illustrate classes. ''' import random import time from graphics import * class Building: # For now, the bottom of every building will be y=400 def __init__(self, left, width, height, color): # Buildings consist of what? A rectangle, and some windows. # Here's the rectangle. upperLeft = Point(left, 400 - height) bottomRight = Point(left + width, 400) self.rectangle = Rectangle(upperLeft, bottomRight) self.rectangle.setFill(color) # Here are the building's windows. littleWindowWidth = 20 littleWindowHeight = 30 littleWindowGap = 10 windowsPerRow = (width - 10) / (littleWindowWidth + littleWindowGap) rowsOfWindows = 2 self.littleWindowList = [] topEdge = 400 - height + littleWindowGap for j in range(rowsOfWindows): leftEdge = left + littleWindowGap for k in range(windowsPerRow): upperLeft = Point(leftEdge, topEdge) bottomRight = Point(leftEdge + littleWindowWidth, topEdge + littleWindowHeight) littleWindow = Rectangle(upperLeft, bottomRight) littleWindow.setFill(color_rgb(0, 0, 0)) self.littleWindowList.append(littleWindow) leftEdge += littleWindowWidth + littleWindowGap topEdge += littleWindowHeight + littleWindowGap def draw(self, window): self.rectangle.draw(window) for littleWindow in self.littleWindowList: littleWindow.draw(window) # Initialize city window windowHeight = 500 windowWidth = 700 windowTitle = 'City' backgroundColor = color_rgb(255, 255, 255) window = GraphWin(windowTitle, windowWidth, windowHeight) window.setBackground(backgroundColor) # Create the buildings buildings = [] for k in range(20): randomLeft = random.randint(0, windowWidth) randomWidth = random.randint(25, 250) randomHeight = random.randint(80, windowHeight - 150) randomRed = random.randint(150, 255) randomBlue = random.randint(0, 50) randomGreen = random.randint(40, 150) color = color_rgb(randomRed, randomGreen, randomBlue) newBuilding = Building(randomLeft, randomWidth, randomHeight, color) buildings.append(newBuilding) # Draw the buildings for building in buildings: building.draw(window) s = raw_input('Hit Enter to quit') # Something with lights # for k in range(8): # time.sleep(0.5) # b = random.choice(buildings) # b.turnOnRandomLight()