# drawBoxes.py An example exam question for Midterm Exam 2 # P. Conrad for CS5nm, Fall 2008, UC Santa Barbara # draw a specific figure using PyGame # For details, see http://www.cs.ucsb.edu/~pconrad/cs5nm/08F/exam/E02/E02_study_guide.html import pygame; pygame.init() screen = pygame.display.set_mode((640,480)) # 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) def drawBoxes(screen, color, x, y, width, height): pygame.draw.lines(screen, color, False, makeBoxesPoints(x,y,width,height), 1) def makeBoxesPoints(x,y,width,height): points = [] points.append( (x+width/2.0, y - height) ) # point 1 from diagram # Fill in the rest of this function... # @@@ check_expect("makeBoxesPoints 1", makeBoxesPoints(50,100,100,50), [(100,50),(100,100),(150,100),(150,50),(50,50), (50,100),(100,100)]) check_expect("makeBoxesPoints 2", makeBoxesPoints(50,250,200,100), [(150,150),(150,250),(250,250),(250,150),(50,150), (50,250),(150,250)]) black = (0, 0, 0) white = (255, 255, 255) while (True): # check for quit events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); # erase the screen screen.fill(white) # draw the updated picture drawBoxes(screen, black, 50, 100, 100, 50) drawBoxes(screen, black, 50, 250, 200, 100) # redraw the points # update the screen pygame.display.update()