Menu

pygame.draw: Rectangles, Circles, Lines and Polygons

pygame.draw draws shapes on any Surface: rect, circle, line, lines, polygon, ellipse and arc. See every function's arguments, outlines with width, rounded corners with border_radius, and a scene built from shapes.

This page includes runnable editors. Edit, run, and see output instantly.

pygame.draw draws shapes onto a Surface, usually the screen. Every function takes the surface first, then a color, then where to draw:

The functions

FunctionArgumentsDraws
rectsurface, color, rect, width=0, border_radius=0A rectangle; rect is (x, y, w, h) or a pygame.Rect
circlesurface, color, center, radius, width=0A circle around center
ellipsesurface, color, rect, width=0An oval that fits inside rect
linesurface, color, start, end, width=1A straight line
linessurface, color, closed, points, width=1Lines through a list of points
polygonsurface, color, points, width=0A filled shape through the points
arcsurface, color, rect, start_angle, stop_angle, width=1Part of an ellipse; angles in radians
aalinesurface, color, start, endA smooth (anti-aliased) one pixel line

width: filled or outline

For rect, circle, ellipse and polygon, width=0 (the default) fills the shape. Any other value draws only an outline that many pixels thick. Lines always use width as their thickness.

Rectangles

A rectangle is placed by its top-left corner plus its width and height. You can pass a tuple, but games usually keep a pygame.Rect, which also moves and checks collisions (see Pygame Rect):

player = pygame.Rect(100, 80, 40, 60)
pygame.draw.rect(screen, "orange", player)

border_radius rounds all four corners. Each corner can also differ: border_top_left_radius, border_top_right_radius, border_bottom_left_radius and border_bottom_right_radius.

Circles

A circle is placed by its center, not its corner. That is the most common surprise: pygame.draw.circle(screen, color, (0, 0), 30) shows only a quarter circle in the window's corner.

Drawing order

Later drawings cover earlier ones. Draw the background first and the things in front last, and call screen.fill() at the start of the frame, never after drawing. Every pygame.draw function returns the Rect it changed, which is handy for collisions and for partial screen updates.

A scene from shapes

Shapes plus a loop are already enough for a moving picture. This one draws a house, a tree and a car that drives past:

Shapes are quick to draw and need no files, which makes them good for prototypes. When you want art, load images and blit them instead.

Frequently Asked Questions

How do I draw a rectangle in pygame?

pygame.draw.rect(screen, color, (x, y, width, height)). Add width=2 to draw only a 2 pixel outline, and border_radius=10 for rounded corners.

How do I draw a circle in pygame?

pygame.draw.circle(screen, color, (center_x, center_y), radius). The position is the center, not the top-left corner. Add a width for a ring.

Why is my shape not showing?

Usually one of three things: it is drawn before screen.fill() in the same frame, so the fill covers it; pygame.display.flip() is never called; or the coordinates are outside the window.

How do I draw a smooth line in pygame?

pygame.draw.aaline(screen, color, start, end) draws an anti-aliased one pixel line, which looks smoother than pygame.draw.line.

Coddy programming languages illustration

Learn to code with Coddy

Get started