# drawAPolygon.py Draw a polygon with the mouse # P. Conrad for CS5nm, 10/06/2008 # Based on listing 4.10 from "Beginning Game Development with Python and Pygame" # Copyright 2007 by Will McGugan, http://www.apress.com import pygame from pygame.locals import * from sys import exit # set up a window 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) # an RGB 3-tuple representing green white = (255, 255, 255) # an RGB 3-tuple representing black color = red # set up an empty list of points points = [] # main loop while True: # loop forever (or at least until someone generates a QUIT event) # Handle events for event in pygame.event.get(): if event.type == QUIT: pygame.quit(); exit() if event.type == MOUSEBUTTONDOWN: points.append(event.pos) # add this point to the list if event.type == KEYDOWN and event.key == K_SPACE: # on the space bar, clear all the points points = [] if event.type == KEYDOWN and event.key == K_g: # pressed a g, make color green color = green screen.fill(white) print "points: " + str(points) if len(points) >= 3: pygame.draw.polygon(screen, color, points) pygame.display.update()