Sunday, July 19, 2015

Create basic shapes using PyGame

We create a basic window in Python and draw the basic geometrical shapes like Rectangle, Circle, Line, ellipse or polygon etc. You should have pygame installed else the code will not execute and throw an ImportError.





"""
Created on 16-Nov-2014
Modified on 19-July-2015
@author: Psycho_Coder
"""
import pygame
import sys
from pygame.locals import QUIT
pygame.init()
DISPSURF = pygame.display.set_mode((320, 250), 0, 32)
pygame.display.set_caption("Basic Shapes using PyGame")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (227, 27, 27)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RCB = (27, 27, 27)
# Fill the background with a color
DISPSURF.fill(RCB)
pygame.draw.polygon(DISPSURF, RED, ((20, 20), (52, 60), (171, 60), (200, 20)))
pygame.draw.line(DISPSURF, BLUE, (50, 60), (50, 160), 2)
pygame.draw.line(DISPSURF, BLUE, (170, 60), (170, 160), 2)
pygame.draw.line(DISPSURF, RED, (110, 60), (110, 160), 2)
pygame.draw.circle(DISPSURF, WHITE, (110, 110), 40, 1)
pygame.draw.rect(DISPSURF, GREEN, (52, 160, 120, 40))
pygame.draw.ellipse(DISPSURF, RED, (220, 100, 80, 40))
pygame.draw.ellipse(DISPSURF, BLUE, (240, 80, 40, 80))
while True:
for evt in pygame.event.get():
if evt.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

Related Posts:

Follow Me!

Followers

Visitor Map