Monday, 7 December 2015

Python: Drawing Ellipses

‘pygame.draw.ellipse()’ method draws an ellipse on surface. It is used to draw an ellipse inside a rectangle.

Syntax
ellipse(Surface, color, Rect, width=0)

Example
pygame.draw.ellipse(screen, RED, [25,35,250,100], 3)


Last argument specifies the width in 3 pixels. If width is zero then the ellipse will be filled.
#import pygame
import pygame

#initialize game engine
pygame.init()

#Open a window
size = (600, 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.rect(screen,GREEN,[25,35,250,100],3)
  pygame.draw.ellipse(screen, RED, [25,35,250,100], 0)
  
  pygame.draw.rect(screen,RED,[125,135,250,100],0)
  pygame.draw.ellipse(screen, BLUE, [125,135,250,100], 3)
  
  pygame.draw.rect(screen,BLUE,[225,235,250,100],3)
  pygame.draw.ellipse(screen, BLACK, [225,235,250,100], 0)
  
  pygame.draw.rect(screen,BLACK,[325,335,250,100],0)
  pygame.draw.ellipse(screen, GREEN, [325,335,250,100], 3)
  
  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