# drawHousesWithFunctions.py Draw some houses on the screen (using functions) # P. Conrad for CS5nm, 10/06/2008 # stuff we need to import in order to use PyGame import pygame from pygame.locals import * from sys import exit # a function to draw a house with PyGame # consumes: # x, y (numbers: lower left corner of the house) # width, height (numbers: width and height of house) # screen (the screen where we should draw this house) # color (a tuple of (r, g, b) representing a color) # produces # nothing # side effect # draws a house on the screen of the size and color given, # at the location given # Note: defining the function does NOT draw ANYTHING # It is just a "recipe" or a "blueprint" for drawig a house # The house only gets drawn when we "call" the function with "arguments". def drawHouse(x, y, width, height, screen, color): points = [(x,y- ((2/3.0) * height)), (x,y), (x+width,y), (x+width,y-(2/3.0) * height), (x,y- ((2/3.0) * height)), (x + width/2.0,y-height), (x+width,y-(2/3.0)*height)] lineThickness = 2 pygame.draw.lines(screen, color, False, points, lineThickness) # define some points for the window winLeft = x + 0.5 * width winRight = x + 0.9 * width winTop = y - 0.4 * height winBot = y - 0.2 * height winMidVert = x + 0.7 * width # vertical midpoint line passes through here winMidHoriz = y - 0.3 * height # third parameter to pygame.draw.lines is True, meaning here that # means we connect the last point back to the first winOutsidePoints = [ (winLeft, winBot), (winRight, winBot), (winRight, winTop), (winLeft, winTop)] pygame.draw.lines(screen, color, True, winOutsidePoints, lineThickness) # False here for third parameter means DON'T connect last point back to first. winVertLine = [ (winMidVert, winTop), (winMidVert, winBot) ] pygame.draw.lines(screen, color, False, winVertLine, lineThickness) winHorizLine = [ (winLeft, winMidHoriz), (winRight, winMidHoriz) ] pygame.draw.lines(screen, color, False, winHorizLine, lineThickness) # set up a window (a.k.a. a screen) size = width, height = 640,480 pygame.init() screen = pygame.display.set_mode(size) # set up variables for colors red = (255, 0, 0 ) # an RGB 3-tuple representing red green = (0, 255, 0) white = (255, 255, 255) blue = (0, 0, 255) # main loop while True: # loop forever (or at least until someone generates a QUIT event) for event in pygame.event.get(): if event.type == QUIT: pygame.quit(); exit() screen.fill(white); # Make the whole screen white # Now, draw three houses drawHouse(100,200,120,150,screen,red) drawHouse(400,200,120,150,screen,green) drawHouse(400,400,120,150,screen,blue) # But, we don't actually SEE the houses until we call "update". pygame.display.update()