pygame.draw

Functions that draw rectangles, circles, ellipses, lines, polygons and arcs onto any Surface, with fills, outlines and rounded corners.

2 min readUpdated
On this page

Overview#

pygame.draw draws simple shapes directly onto a Surface. Every function takes the target Surface and a colour first, then the shape's geometry, and returns a Rect covering the changed pixels. Colours can be (r, g, b) tuples, pygame.Color objects or names such as "red".

Syntax and parameters#

Python
pygame.draw.rect(surface, color, rect, width=0, border_radius=-1,
                 border_top_left_radius=-1, border_top_right_radius=-1,
                 border_bottom_left_radius=-1, border_bottom_right_radius=-1)
pygame.draw.circle(surface, color, center, radius, width=0,
                   draw_top_right=False, draw_top_left=False,
                   draw_bottom_left=False, draw_bottom_right=False)
pygame.draw.ellipse(surface, color, rect, width=0)
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1)
pygame.draw.line(surface, color, start_pos, end_pos, width=1)
pygame.draw.lines(surface, color, closed, points, width=1)
pygame.draw.aaline(surface, color, start_pos, end_pos)
pygame.draw.aalines(surface, color, closed, points)
pygame.draw.polygon(surface, color, points, width=0)
ParameterMeaning
surfaceWhere to draw: the screen or any other Surface.
colorColour of the shape.
rect(x, y, width, height) or a Rect. For ellipse and arc it is the bounding box.
width0 fills the shape; a positive value draws an outline of that thickness. For lines it is the line thickness.
border_radiusRounds all corners of a rectangle; the per-corner arguments override it.
center, radiusCircle centre and radius in pixels.
draw_top_right etc.Draw only the selected quarters of a circle.
start_angle, stop_angleArc angles in radians, counter-clockwise from the right.
closedFor lines/aalines: join the last point back to the first.
pointsA sequence of (x, y) points (three or more for polygons).

Return value#

Each function returns a Rect bounding the area it drew. You can pass it to pygame.display.update() to refresh only that area, or use it for debugging.

Practical example#

draw_example.pyDownload
"""pygame.draw: every drawing function with its most useful options."""
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 400))
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("white")
    pygame.draw.rect(screen, "royalblue", (20, 20, 120, 80))                          # filled
    pygame.draw.rect(screen, "black", (160, 20, 120, 80), width=3)                     # outline
    pygame.draw.rect(screen, "orange", (300, 20, 120, 80), border_radius=20)           # rounded
    pygame.draw.rect(screen, "purple", (440, 20, 120, 80), border_top_left_radius=40)  # one corner
    pygame.draw.circle(screen, "gold", (80, 170), 40)
    pygame.draw.circle(screen, "gold", (220, 170), 40, width=6)
    pygame.draw.circle(screen, "crimson", (360, 170), 40, draw_top_right=True, draw_bottom_left=True)
    pygame.draw.ellipse(screen, "seagreen", (440, 130, 160, 80))
    pygame.draw.line(screen, "black", (20, 240), (300, 260), 5)
    pygame.draw.aaline(screen, "black", (20, 270), (300, 290))                          # anti-aliased
    pygame.draw.lines(screen, "teal", False, [(330, 240), (380, 290), (430, 240), (480, 290)], 3)
    pygame.draw.polygon(screen, "tomato", [(520, 240), (600, 300), (500, 300)])
    pygame.draw.arc(screen, "navy", (40, 300, 120, 80), 0, 3.14, 4)                     # radians
    changed = pygame.draw.rect(screen, "grey", (200, 320, 60, 40))
    pygame.draw.rect(screen, "red", changed.inflate(8, 8), 1)                           # draw functions return a Rect
    pygame.display.flip()
    clock.tick(30)

pygame.quit()

Tips#

  • Transparency: drawing with an RGBA colour on the display doesn't blend. Draw on a Surface created with pygame.SRCALPHA and blit it.
  • Smooth edges: aaline and aalines draw anti-aliased lines. pygame-ce adds aacircle for smooth circles.
  • Thick lines have square ends and can look jagged at angles; for very thick lines draw a polygon instead.
  • Speed: drawing many shapes each frame is fine for simple games. For static backgrounds, draw once on a Surface and blit it every frame.

Common errors#

  • TypeError: invalid rect: pass the rectangle as one argument, (x, y, w, h), not four.
  • TypeError: points must be number pairs: polygon points must be (x, y) pairs.
  • Arcs look wrong: angles are in radians. Use math.radians(degrees).
  • Nothing appears: you drew after pygame.display.flip(), or a later fill() covered it.

pygame-ce notes#

pygame-ce includes pygame.draw.aacircle (anti-aliased circles), which the original pygame lacks. The other functions above work the same way in both.

Official documentation#

↑ ↓ to moveEnter to openEsc to close