Tuesday, 8 December 2015

Python: Draw anti aliased lines

‘pygame.draw.aaline()’ method is used to draw anti aliased lines.

Syntax
aaline(Surface, color, startpos, endpos, blend=1)


Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.

#import pygame
import pygame

#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()

while(dead==False):
 for event in pygame.event.get(): 
  if event.type == pygame.QUIT:
   dead = True
  
  screen.fill(WHITE)
  pygame.draw.aaline(screen, RED, [0,0], [499, 499], 4)
  pygame.draw.aaline(screen, GREEN, [499,0], [0, 499], 4)
  pygame.draw.aaline(screen, BLUE, [249,0], [249, 499], 4)
  pygame.draw.aaline(screen, BLACK, [0,249], [499, 249], 4)
  pygame.display.flip()
  clock.tick(60)
   
#Shutdown display module
pygame.display.quit()

Run above program, you will get following output.





Previous                                                 Next                                                 Home

No comments:

Post a Comment