Cross-platform library for developing multimedia applications and video games in Python built on top of SDL
Primitive shape drawing functions for creating graphics. This module provides high-performance drawing operations for basic geometric shapes, lines, and filled areas with support for colors, line widths, and antialiasing.
Draw rectangles and rounded rectangles with various fill and border options.
def rect(surface: pygame.Surface, color, rect: pygame.Rect, width: int = 0, border_radius: int = 0, border_top_left_radius: int = -1, border_top_right_radius: int = -1, border_bottom_left_radius: int = -1, border_bottom_right_radius: int = -1) -> pygame.Rect:
"""
Draw a rectangle on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use (Color, tuple, or color name)
rect (pygame.Rect): Rectangle area to draw
width (int): Line thickness (0 for filled rectangle)
border_radius (int): Radius for all corners
border_*_radius (int): Radius for specific corners (-1 uses border_radius)
Returns:
pygame.Rect: Rectangle area that was drawn
"""Draw circles and ellipses with optional partial drawing and customizable borders.
def circle(surface: pygame.Surface, color, center: tuple[int, int], radius: int, width: int = 0, draw_top_right: bool = None, draw_top_left: bool = None, draw_bottom_left: bool = None, draw_bottom_right: bool = None) -> pygame.Rect:
"""
Draw a circle on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
center (tuple[int, int]): (x, y) center position
radius (int): Circle radius in pixels
width (int): Line thickness (0 for filled circle)
draw_*_* (bool): Draw specific quadrants (None draws all)
Returns:
pygame.Rect: Bounding rectangle of the drawn circle
"""
def ellipse(surface: pygame.Surface, color, rect: pygame.Rect, width: int = 0) -> pygame.Rect:
"""
Draw an ellipse on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
rect (pygame.Rect): Bounding rectangle for ellipse
width (int): Line thickness (0 for filled ellipse)
Returns:
pygame.Rect: Bounding rectangle that was drawn
"""Draw partial circles and ellipses with precise angle control.
def arc(surface: pygame.Surface, color, rect: pygame.Rect, start_angle: float, stop_angle: float, width: int = 1) -> pygame.Rect:
"""
Draw an arc on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
rect (pygame.Rect): Bounding rectangle for arc
start_angle (float): Start angle in radians
stop_angle (float): Stop angle in radians
width (int): Line thickness
Returns:
pygame.Rect: Bounding rectangle of the drawn arc
"""Draw single lines and connected line sequences with antialiasing options.
def line(surface: pygame.Surface, color, start_pos: tuple[int, int], end_pos: tuple[int, int], width: int = 1) -> pygame.Rect:
"""
Draw a straight line on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
start_pos (tuple[int, int]): (x, y) starting position
end_pos (tuple[int, int]): (x, y) ending position
width (int): Line thickness
Returns:
pygame.Rect: Bounding rectangle of the drawn line
"""
def lines(surface: pygame.Surface, color, closed: bool, points: list[tuple[int, int]], width: int = 1) -> pygame.Rect:
"""
Draw connected lines on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
closed (bool): If True, connect last point to first
points (list[tuple[int, int]]): List of (x, y) points
width (int): Line thickness
Returns:
pygame.Rect: Bounding rectangle of all drawn lines
"""
def aaline(surface: pygame.Surface, color, start_pos: tuple[int, int], end_pos: tuple[int, int], blend: int = 1) -> pygame.Rect:
"""
Draw an antialiased line on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
start_pos (tuple[int, int]): (x, y) starting position
end_pos (tuple[int, int]): (x, y) ending position
blend (int): Blending mode (1 for antialiased, 0 for solid)
Returns:
pygame.Rect: Bounding rectangle of the drawn line
"""
def aalines(surface: pygame.Surface, color, closed: bool, points: list[tuple[int, int]], blend: int = 1) -> pygame.Rect:
"""
Draw connected antialiased lines on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
closed (bool): If True, connect last point to first
points (list[tuple[int, int]]): List of (x, y) points
blend (int): Blending mode (1 for antialiased, 0 for solid)
Returns:
pygame.Rect: Bounding rectangle of all drawn lines
"""Draw complex shapes defined by multiple points.
def polygon(surface: pygame.Surface, color, points: list[tuple[int, int]], width: int = 0) -> pygame.Rect:
"""
Draw a polygon on a surface.
Args:
surface (pygame.Surface): Surface to draw on
color: Color to use
points (list[tuple[int, int]]): List of (x, y) vertices (minimum 3)
width (int): Line thickness (0 for filled polygon)
Returns:
pygame.Rect: Bounding rectangle of the drawn polygon
"""All drawing functions accept colors in multiple formats:
# Color formats accepted by all drawing functions:
color_name: str # "red", "blue", "white", etc.
rgb_tuple: tuple[int, int, int] # (255, 0, 0) for red
rgba_tuple: tuple[int, int, int, int] # (255, 0, 0, 128) for semi-transparent red
pygame_color: pygame.Color # Color object
hex_integer: int # 0xFF0000 for redimport pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Fill background
screen.fill((255, 255, 255)) # White background
# Draw filled rectangle
pygame.draw.rect(screen, (255, 0, 0), (100, 100, 200, 150))
# Draw rectangle outline
pygame.draw.rect(screen, (0, 255, 0), (350, 100, 200, 150), 3)
# Draw rounded rectangle
pygame.draw.rect(screen, (0, 0, 255), (100, 300, 200, 100), border_radius=20)
# Draw circle
pygame.draw.circle(screen, (255, 255, 0), (400, 400), 50)
# Draw circle outline
pygame.draw.circle(screen, (255, 0, 255), (600, 400), 50, 5)
pygame.display.flip()import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((0, 0, 0))
# Draw single line
pygame.draw.line(screen, (255, 255, 255), (0, 0), (800, 600), 2)
# Draw connected lines (triangle)
points = [(400, 100), (300, 300), (500, 300)]
pygame.draw.lines(screen, (0, 255, 0), True, points, 3)
# Draw antialiased lines for smoother appearance
start = (100, 500)
end = (700, 100)
pygame.draw.aaline(screen, (255, 0, 0), start, end)
pygame.display.flip()import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((0, 0, 0))
# Draw star polygon
center = (400, 300)
outer_radius = 100
inner_radius = 50
num_points = 5
star_points = []
for i in range(num_points * 2):
angle = i * math.pi / num_points
if i % 2 == 0:
radius = outer_radius
else:
radius = inner_radius
x = center[0] + radius * math.cos(angle)
y = center[1] + radius * math.sin(angle)
star_points.append((x, y))
pygame.draw.polygon(screen, (255, 255, 0), star_points)
# Draw partial circle (pac-man)
mouth_rect = pygame.Rect(100, 100, 100, 100)
pygame.draw.arc(screen, (255, 255, 0), mouth_rect, 0.5, 5.8, 3)
pygame.display.flip()import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
angle = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear screen
screen.fill((0, 0, 0))
# Draw rotating line
center = (400, 300)
length = 100
end_x = center[0] + length * math.cos(angle)
end_y = center[1] + length * math.sin(angle)
pygame.draw.line(screen, (255, 255, 255), center, (end_x, end_y), 3)
pygame.draw.circle(screen, (255, 0, 0), center, 5)
angle += 0.05
pygame.display.flip()
clock.tick(60)
pygame.quit()import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Use filled shapes when possible - they're faster than outlines
pygame.draw.rect(screen, (255, 0, 0), (10, 10, 100, 50)) # Faster
pygame.draw.rect(screen, (255, 0, 0), (10, 10, 100, 50), 1) # Slower
# Batch multiple draw operations
dirty_rects = []
dirty_rects.append(pygame.draw.circle(screen, (0, 255, 0), (100, 100), 20))
dirty_rects.append(pygame.draw.circle(screen, (0, 0, 255), (200, 100), 20))
# Update only the drawn areas
pygame.display.update(dirty_rects)