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
| Function | Arguments | Draws |
|---|---|---|
rect | surface, color, rect, width=0, border_radius=0 | A rectangle; rect is (x, y, w, h) or a pygame.Rect |
circle | surface, color, center, radius, width=0 | A circle around center |
ellipse | surface, color, rect, width=0 | An oval that fits inside rect |
line | surface, color, start, end, width=1 | A straight line |
lines | surface, color, closed, points, width=1 | Lines through a list of points |
polygon | surface, color, points, width=0 | A filled shape through the points |
arc | surface, color, rect, start_angle, stop_angle, width=1 | Part of an ellipse; angles in radians |
aaline | surface, color, start, end | A 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.