Monday 7 December 2015

Pygame: Draw lines

‘pygame.draw.line()’ method is used to draw lines on a surface.

Syntax
line(Surface, color, start_pos, end_pos, width=1)

pygame_hello.py
#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.line(screen, RED, [0,0], [499, 499], 4)
  pygame.draw.line(screen, GREEN, [499,0], [0, 499], 4)
  pygame.draw.line(screen, BLUE, [249,0], [249, 499], 4)
  pygame.draw.line(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 screen.







Previous                                                 Next                                                 Home

No comments:

Post a Comment