Tuesday 8 December 2015

Pygame: Drawing a polygon


‘pygame.draw.polygon()’ is used to draw polygon.

Syntax
polygon(Surface, color, pointlist, width=0)

If the width is 0, then the polygon will be filled.
#import pygame
import pygame
import math

#initialize game engine
pygame.init()

#Open a window
size = (500, 500)
screen = pygame.display.set_mode(size)

#Set title to the window
pygame.display.set_caption("Hello World")

dead=False

#Initialize values for color (RGB format)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0, 255, 0)
BLUE=(0,0,255)
BLACK=(0,0,0)

clock = pygame.time.Clock()
PI=math.pi

while(dead==False):
 for event in pygame.event.get(): 
  if event.type == pygame.QUIT:
   dead = True
  
  screen.fill(WHITE)
  pygame.draw.polygon(screen, RED, [[10,10], [100, 10], [55, 80]], 0)
  pygame.draw.polygon(screen, RED, [[110,10], [200, 10], [155, 80]], 0)
  pygame.draw.polygon(screen, RED, [[210,10], [300, 10], [255, 80]], 0)
  pygame.draw.polygon(screen, RED, [[310,10], [400, 10], [355, 80]], 0)
  pygame.draw.polygon(screen, RED, [[410,10], [500, 10], [455, 80]], 0)
  
  pygame.draw.polygon(screen, GREEN, [[10, 500], [100, 500], [55, 420]], 0)
  pygame.draw.polygon(screen, GREEN, [[110, 500], [200, 500], [155, 420]], 0)
  pygame.draw.polygon(screen, GREEN, [[210, 500], [300, 500], [255, 420]], 0)
  pygame.draw.polygon(screen, GREEN, [[310, 500], [400, 500], [355, 420]], 0)
  pygame.draw.polygon(screen, GREEN, [[410, 500], [500, 500], [455, 420]], 0)
  
  pygame.draw.polygon(screen, BLUE, [[55, 80], [55, 420], [155, 80], [155, 420], [255, 80], [255, 420], [355, 80], [355, 420], [455, 80],[455, 420] ], 2)
  
  pygame.draw.polygon(screen, BLACK, [[55, 80], [455, 80], [455, 420], [55, 420]], 2)

  pygame.display.flip()
  clock.tick(60)
   
#Shutdown display module
pygame.display.quit()

Above program displays following window.







Previous                                                 Next                                                 Home

No comments:

Post a Comment