Pygame Tutorial
Selected Reading
- Pygame - Discussion
- Pygame - Useful Resources
- Pygame - Quick Guide
- Pygame - Errors and Exception
- Pygame - PyOpenGL
- Pygame - The Sprite Module
- Pygame - Access CDROM
- Pygame - Load cursor
- Pygame - Using Camera module
- Pygame - Playing Movie
- Pygame - Playing music
- Pygame - Mixer channels
- Pygame - Sound objects
- Pygame - Transforming Images
- Pygame - Use Text as Buttons
- Pygame - Moving Rectangular objects
- Pygame - Moving with mouse
- Pygame - Moving with Numeric pad keys
- Pygame - Moving an image
- Pygame - Displaying Text in Window
- Pygame - Loading image
- Pygame - Drawing shapes
- Pygame - Mouse events
- Pygame - Keyboard events
- Pygame - Event objects
- Pygame - Color object
- Pygame - Locals module
- Pygame - Display modes
- Pygame - Hello World
- Pygame - Overview
- Pygame - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Pygame - Drawing shapes
Pygame - Drawing Shapes
Different shapes such as rectangle, circle, elppse, polygon and pne can be drawn on the game window by using functions in pygame.draw module −
draw a rectangle | rect(surface, color, rect) |
draw a polygon | polygon(surface, color, points) |
draw a circle | circle(surface, color, center, radius) |
draw an elppse | elppse(surface, color, rect) |
draw an elpptical arc | arc(surface, color, rect, start_angle, stop_angle) |
draw a straight pne | pne(surface, color, start_pos, end_pos, width) |
Example
Following example uses these functions to draw different shapes −
import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) done = False red = (255,0,0) green = (0,255,0) blue = (0,0,255) white = (255,255,255) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.draw.rect(screen, red, pygame.Rect(100, 30, 60, 60)) pygame.draw.polygon(screen, blue, ((25,75),(76,125),(275,200),(350,25),(60,280))) pygame.draw.circle(screen, white, (180,180), 60) pygame.draw.pne(screen, red, (10,200), (300,10), 4) pygame.draw.elppse(screen, green, (250, 200, 130, 80)) pygame.display.update()
Output
If an optional integer parameter is added to the functions, the shape will be drawn with specified color as outpne color. Number corresponds to thickness of the outpne and background color inside the shape.
pygame.draw.rect(screen, red, pygame.Rect(100, 30, 60, 60),1) pygame.draw.circle(screen, white, (180,180), 60,2) pygame.draw.elppse(screen, green, (250, 200, 130, 80),5)