Drawing shapes with pygame.draw

Rectangles, circles, lines, polygons and arcs are enough to make complete games. Learn every pygame.draw function by building a small scene.

Beginner3 min readUpdated
On this page

What you'll learn

  • Every function in pygame.draw and its key arguments
  • Filled shapes versus outlines, and rounded rectangles
  • How draw order creates layers
  • How to wrap drawing code in reusable functions

Prerequisites: Getting started.

The pattern behind every draw function#

Every pygame.draw function takes the Surface to draw on, a colour, then the shape's geometry. Most accept an optional width: 0 (the default) fills the shape, a positive number draws an outline that many pixels thick.

Python
pygame.draw.rect(screen, (36, 87, 166), (40, 40, 160, 100))          # filled
pygame.draw.rect(screen, "black", (240, 40, 160, 100), width=4)      # outline
pygame.draw.circle(screen, "gold", (520, 90), 50)                     # centre, radius
pygame.draw.line(screen, "red", (40, 200), (600, 200), 3)             # start, end, width

Colours can be RGB tuples (r, g, b), RGBA tuples, pygame.Color objects or colour names such as "gold". The colour picker gives you the tuple for any colour.

The functions at a glance#

FunctionGeometry argumentsNotes
rectrect as (x, y, w, h) or a Rectborder_radius= rounds corners
circlecenter, radiusdraw_top_left=True etc. draw quarters
ellipsebounding rectthe ellipse fits inside the rectangle
linestart_pos, end_pos, widththick lines have square ends
linesclosed, list of pointsconnects points in order
aaline / aalineslike line / linesanti-aliased (smooth) 1-pixel lines
polygonlist of pointsthree or more points
arcbounding rect, start_angle, stop_angleangles in radians

Each function returns a Rect covering the pixels it changed, which is useful when you only want to update part of the screen.

Build a scene#

Shapes become recognisable objects when you combine them. This program draws a house, trees and an animated sun:

drawing_scene.pyDownload
"""A small scene built only from pygame.draw calls."""
import math
import pygame

WIDTH, HEIGHT = 640, 480

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()


def draw_house(surface, x, y):
    pygame.draw.rect(surface, (210, 120, 90), (x, y, 160, 120))                       # walls
    pygame.draw.polygon(surface, (150, 60, 50), [(x - 15, y), (x + 80, y - 70), (x + 175, y)])  # roof
    pygame.draw.rect(surface, (90, 60, 40), (x + 62, y + 60, 36, 60))                 # door
    pygame.draw.circle(surface, (244, 197, 49), (x + 90, y + 92), 3)                  # door knob
    for wx in (x + 15, x + 115):                                                      # windows
        pygame.draw.rect(surface, (200, 230, 255), (wx, y + 25, 30, 30))
        pygame.draw.rect(surface, (60, 40, 30), (wx, y + 25, 30, 30), width=3)
        pygame.draw.line(surface, (60, 40, 30), (wx + 15, y + 25), (wx + 15, y + 54), 2)


def draw_tree(surface, x, y):
    pygame.draw.rect(surface, (110, 75, 45), (x - 8, y, 16, 60))
    pygame.draw.circle(surface, (60, 150, 80), (x, y - 10), 38)
    pygame.draw.circle(surface, (80, 175, 95), (x - 18, y - 22), 22)


t = 0.0
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    t += clock.tick(60) / 1000

    screen.fill((170, 210, 245))                                        # sky
    sun_y = 90 + math.sin(t) * 10
    pygame.draw.circle(screen, (250, 210, 70), (520, sun_y), 45)       # sun
    for i in range(12):                                                 # sun rays
        angle = t * 0.5 + i * math.tau / 12
        start = (520 + math.cos(angle) * 55, sun_y + math.sin(angle) * 55)
        end = (520 + math.cos(angle) * 75, sun_y + math.sin(angle) * 75)
        pygame.draw.line(screen, (250, 210, 70), start, end, 4)
    pygame.draw.ellipse(screen, (255, 255, 255), (80, 60, 140, 50))     # cloud
    pygame.draw.rect(screen, (100, 170, 90), (0, 360, WIDTH, 120))      # grass
    draw_house(screen, 220, 240)
    draw_tree(screen, 110, 300)
    draw_tree(screen, 500, 310)
    pygame.draw.lines(screen, (120, 90, 60), False, [(0, 420), (160, 400), (300, 430), (640, 405)], 3)  # path
    pygame.display.flip()

pygame.quit()
A house, two trees, a cloud and a sun drawn with pygame.draw
drawing_scene.py

Things to notice:

  • Draw order is layer order. The sky is filled first, the grass next, then the house on top. Whatever you draw last appears in front.
  • Functions take a position. draw_house(surface, x, y) draws relative to (x, y), so you can place as many houses as you like.
  • Polygons are lists of points. The roof is a triangle: left eave, peak, right eave.
  • Angles for arc are radians, and the sun's rays use math.cos and math.sin to spread around a circle. math.tau is a full turn (2π).

Rounded rectangles and partial circles#

pygame.draw.rect accepts border_radius for all corners, or border_top_left_radius and friends for individual corners. Rounded rectangles are handy for buttons and health bars. pygame.draw.circle can draw only some quarters with draw_top_right=True and similar flags, which is useful for pie-chart style timers.

Transparency#

The draw functions write colours directly and ignore the alpha value when drawing on the display. For a see-through shape, draw it on a separate Surface created with pygame.SRCALPHA, then blit that Surface:

Python
overlay = pygame.Surface((200, 100), pygame.SRCALPHA)
pygame.draw.rect(overlay, (0, 0, 0, 128), overlay.get_rect(), border_radius=12)   # 50% black
screen.blit(overlay, (220, 190))

Smooth edges#

Filled shapes have jagged ("aliased") edges. For smooth 1-pixel lines use aaline. pygame-ce adds pygame.draw.aacircle for smooth circles; in the original pygame you can draw shapes larger on a separate Surface and shrink it with pygame.transform.smoothscale.

Common mistakes#

  • Passing four numbers instead of a rect: pygame.draw.rect(screen, color, 10, 10, 50, 50) fails. Wrap them: (10, 10, 50, 50).
  • Degrees in arc: arc(..., 0, 180) is 180 radians, many full turns. Use math.radians(180) or math.pi.
  • Outline width larger than the shape: a 30-pixel outline on a 20-pixel circle draws nothing useful.
  • Expecting alpha to work on screen: see the transparency section above.

Practice exercises#

  1. Add a chimney to the house and a second cloud that drifts across the sky.
  2. Write a draw_health_bar(surface, x, y, value, maximum) function using two rounded rectangles.
  3. Draw a clock face using arc for the rim and line for the hands, driven by pygame.time.get_ticks().
  4. Recreate the scene at a different window size by computing positions from WIDTH and HEIGHT.
↑ ↓ to moveEnter to openEsc to close