import pygame # Define some colors BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) RED = ( 255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) YELLOW = ( 255, 255, 0) pygame.init() # Set the width and height of the screen [width, height] size = (700, 700) screen = pygame.display.set_mode(size) pygame.display.set_caption("Dynamisches System von n-Eclen") # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() #posx,posy = 400,400 plist = [] reset = False padd, plast,go,mag,small,center,mgo = False,False,False,False,False,False,False finished = False np = 0 #background_image = pygame.image.load("bluetrees.jpg").convert() def scale(s,pt): return ( s*pt[0]-(s-1)/2*size[0],s*pt[1]-(s-1)/2*size[1] ) def midp(pt,qt): pq = ( pt[0]+qt[0],pt[1]+qt[1] ) return ( pq[0]/2.0,pq[1]/2.0 ) def iterate(plist,np): if np>1: pplist = range(np) pplist[0] = midp(plist[np-1],plist[0]) for i in range(np-1): pplist[i+1] = midp(plist[i],plist[i+1]) return (pplist) else: return(plist) # -------- Print menue print "a add a point" print "b add last point" print "g each vertex replaced by midpoint of neighbors" print "f fast repeat of g" print "m magnify 10%" print "s shrink 10%" print "c center" print "SPC clear n-gon" print "q quit" # -------- Main Program Loop ----------- while not done: # --- Main event loop for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: padd = True if event.key == pygame.K_b: plast = True if event.key == pygame.K_g: go = True if event.key == pygame.K_m: mag = True if event.key == pygame.K_s: small = True if event.key == pygame.K_c: center = True if event.key == pygame.K_SPACE: reset = True if event.key == pygame.K_q: done = True if event.key == pygame.K_f: mgo = True if event.type == pygame.KEYUP: if event.key == pygame.K_f: mgo = False # --- Game logic should go here if padd: padd = False if not finished: np = np+1 new = pygame.mouse.get_pos() plist = plist + [new] if plast: plast = False if not finished: np = np+1 new = pygame.mouse.get_pos() plist = plist + [new] print "The number of vertices is",np finished = True if go: go = False if finished: plist = iterate(plist,np) if mgo: if finished: plist = iterate(plist,np) plist = iterate(plist,np) if mag: mag = False if finished: for i in range(np): plist[i] = scale(1.1,plist[i]) if small: small = False if finished: for i in range(np): plist[i] = scale(0.9,plist[i]) if center: center = False if finished: nn = float(np) nsum = (0,0) for i in range(np): nsum = ( nsum[0]+plist[i][0],nsum[1]+plist[i][1] ) cor = ( nsum[0]/nn-size[0]/2.0,nsum[1]/nn-size[1]/2.0 ) for i in range(np): plist[i] = ( plist[i][0]-cor[0],plist[i][1]-cor[1] ) if reset: plist = [] reset = False padd, plast,go,mag,small,center,mgo = False,False,False,False,False,False,False finished = False np = 0 # --- Prepare for drawing: convert to integers pl = plist for i in range(np): pl[i] = ( int(plist[i][0]),int(plist[i][1]) ) # --- Drawing code should go here # First, clear the screen to white. Don't put other drawing commands # above this, or they will be erased with this command. screen.fill(BLACK) #screen.blit(background_image, [0, 0]) for i in range(np-1): pygame.draw.circle(screen,RED,pl[i],10) pygame.draw.line(screen, WHITE, pl[i], pl[i+1], 4) if np>0: pygame.draw.circle(screen,RED,pl[np-1],10) if finished: pygame.draw.line(screen, WHITE, pl[0], pl[np-1], 4) # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(60) # Close the window and quit. # If you forget this line, the program will 'hang' # on exit if running from IDLE. pygame.quit()