Pygame has a module, pygame.draw
, that contains functions which can draw shapes directly to a Surface.
Function | Description |
---|---|
pygame.draw.rect | draw a rectangle shape |
pygame.draw.polygon | draw a shape with any number of sides |
pygame.draw.circle | draw a circle around a point |
pygame.draw.ellipse | draw a round shape inside a rectangle |
pygame.draw.arc | draw a partial section of an ellipse |
pygame.draw.line | draw a straight line segment |
pygame.draw.lines | draw multiple contiguous line segments |
pygame.draw.aaline | draw fine antialiased lines |
pygame.draw.aalines | draw a connected sequence of antialiased lines |
To use the module you first need to import and initialize pygame correctly and set a mode for the display. It's convenient to define color constants in advance, making your code more readable and more beautiful. All functions takes a Surface to draw on, a color and a position argument that's either a pygame Rect or a 2-element integer/float sequence (the pygame.draw.circle
will only take integers because of undefined reasons).
The code below will showcase all the different functions, how they are used and how they look. We'll initialize pygame and define some constants before the examples.
import pygame
from math import pi
pygame.init()
screen = pygame.display.set_mode((100, 100))
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
The black color is the Surface default color and represents the part of the Surface that hasn't been drawn onto. The parameters of each function is explained down below at Parameters .
size = (50, 50)
rect_border = pygame.Surface(size) # Create a Surface to draw on.
pygame.draw.rect(rect_border, RED, rect_border.get_rect(), 10) # Draw on it.
rect_filled = pygame.Surface(size)
pygame.draw.rect(rect_filled, RED, rect_filled.get_rect())
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)] # The corner points of the polygon.
polygon = pygame.Surface(size)
pygame.draw.polygon(polygon, RED, points, 10)
polygon_filled = pygame.Surface(size)
pygame.draw.polygon(polygon_filled, RED, points)
size = (50, 50)
radius = 25
circle = pygame.Surface(size)
pygame.draw.circle(circle, RED, (radius, radius), radius, 10) # Position is the center of the circle.
circle_filled = pygame.Surface(size)
pygame.draw.circle(circle_filled, RED, (radius, radius), radius)
The holes is a unfortunate consequence of pygame's drawing algorithm.
size = (50, 25) # Minimize it's height so it doesn't look like a circle.
ellipse = pygame.Surface(size)
pygame.draw.ellipse(ellipse, RED, ellipse.get_rect(), 5)
ellipse_filled = pygame.Surface(size)
pygame.draw.ellipse(ellipse_filled, RED, ellipse.get_rect())
The holes is a unfortunate consequence of pygame's drawing algorithm.
size = (50, 50)
arc = pygame.Surface(size)
pygame.draw.arc(arc, RED, arc.get_rect(), 0, pi) # 0 to pi is 180° creating a half circle.
size = (50, 50)
line = pygame.Surface(size)
pygame.draw.line(line, RED, (0, 0), (50, 50)) # Start at topleft and ends at bottomright.
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)]
lines = pygame.Surface(size)
pygame.draw.lines(lines, RED, False, points)
lines_closed = pygame.Surface(size)
pygame.draw.lines(lines_closed, RED, True, points)
size = (50, 50)
antialiased_line = pygame.Surface(size)
pygame.draw.aaline(antialiased_line, RED, (0, 0), (50, 50))
size = (50, 50)
points = [(25, 0), (50, 25), (25, 50), (0, 25)]
antialiased_lines = pygame.Surface(size)
pygame.draw.aalines(antialiased_lines, RED, False, points)
antialiased_lines_closed = pygame.Surface(size)
pygame.draw.aalines(antialiased_lines_closed, RED, True, points)
To try it out for yourself: copy one of the code snippets above and the code below in an empty file, change the name image to the name of the Surface you want to blit and experiment.
import pygame
from math import pi
pygame.init()
screen = pygame.display.set_mode((100, 100))
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
# But code snippet here
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.blit(image, (25, 25))
pygame.display.update()