# ex09DrawSnowManWithTests.py Draw Some Snowmen # P. Conrad for CS5nm, 10/14/2008 # NOTE: When you run this file, it does NOT draw anything until you type: # draw() # at the Python Shell prompt. # stuff we need to import in order to use PyGame import pygame from pygame.locals import * from sys import exit # A function for running test cases # Note that \ character at the end of a line means that the line continues # on the next line def check_expect(test,check,expect): if (check == expect): print "Test " + test + " passed." else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) # makeSnowManPoints make a tuple of points for a snowman # The snowman consists of three circles, stacked on top # of each other. The bottom circle has radius 2 units, # The middle circle has radius 1.5 units, and the top # circle has radius 1 unit. The entire height is 7 units. # consumes: # x, y (numbers: center of bottom of snowman) # height (number: height of snowman) # produces # a tuple consisting of # (botX,botY,botR, midX, midY, midR, topX, topY, topR) # These are the center and radius of each of the three circles def makeSnowmanPoints(x,y,height): unit = height/9.0; # the entire snowman is five units high botX = midX = topX = x; # all the X coordinates are the same # assign the radiuses (radii?) botR = 2.0 * unit; midR = 1.5 * unit; topR = unit; # work our way up, defining the Y values. Do you see where these come from? botY = y - botR; midY = botY - botR - midR; topY = midY - topR; return (botX,botY,botR,midX,midY,midR,topX,topY,topR) check_expect("makeSnowmanPoints test1", makeSnowmanPoints(100,100,90), (100,80,20,100,45,15,100,20,10)) # @@@ ADD ONE MORE TEST HERE # a function to draw a snowman with PyGame # consumes: # x, y (numbers: bottom middle point of the snowman) # height (numbers: height of the entire snowman) # 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 def drawSnowman(x,y,height,screen,color): lineThickness = 3 # calculate all the points, and unpack them from the tuple into individual variables (botX,botY,botR,midX,midY,midR,topX,topY,topR) = makeSnowmanPoints(x,y,height) # Now draw the circles. We put int() around the points, because # pygame expects int arguments, not float. The extra parentheses around the x and y # coordinates are because the third argument to pygame.draw.circle should be an (x,y) tuple pygame.draw.circle(screen,color, (int(botX),int(botY)), int(botR), lineThickness); pygame.draw.circle(screen,color, (int(midX),int(midY)), int(midR), lineThickness); pygame.draw.circle(screen,color, (int(topX),int(topY)), int(topR), lineThickness); # You coud add code here to add eyes or a carrot for a nose # draw() a function to actually do the drawing # consumes and produces nothing # side effect: we see a window with our drawing # to call this function, type draw() in the Python Shell after you do "Run Module" in IDLE def draw(): # 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 snowmen drawSnowman(100,200,90,screen,red) drawSnowman(400,200,90,screen,green) drawSnowman(400,400,150,screen,blue) # But, we don't actually SEE anything until we call "update". pygame.display.update()