EXPERIMENTAL!: This API may change or disappear in later pygame releases. If you use this, your code may break with the next pygame release.
The pygame package does not import gfxdraw automatically when loaded, so it must imported explicitly to be used.
import pygame import pygame.gfxdraw
For all functions the arguments are strictly positional and integers are accepted for coordinates and radii. The color argument can be one of the following formats:
a
pygame.Colorobjectan
(RGB)triplet (tuple/list)an
(RGBA)quadruplet (tuple/list)
The functions rectangle() and box() will accept any (x, y, w, h) sequence for their rect argument, though pygame.Rect instances are preferred.
To draw a filled antialiased shape, first use the antialiased (aa*) version of the function, and then use the filled (filled_*) version. For example:
col = (255, 0, 0) surf.fill((255, 255, 255)) pygame.gfxdraw.aacircle(surf, x, y, 30, col) pygame.gfxdraw.filled_circle(surf, x, y, 30, col)
Note
For threading, each of the functions releases the GIL during the C part of the call.
Note
See the pygame.draw module for alternative draw methods. The pygame.gfxdraw module differs from the pygame.draw module in the API it uses and the different draw functions available. pygame.gfxdraw wraps the primitives from the library called SDL_gfx, rather than using modified versions.
New in pygame 1.9.0.
Draws a single pixel, at position (x ,y), on the given surface.
Draws a straight horizontal line ((x1, y) to (x2, y)) on the given surface. There are no endcaps.
None
NoneType
Draws a straight vertical line ((x, y1) to (x, y2)) on the given surface. There are no endcaps.
None
NoneType
Draws a straight line ((x1, y1) to (x2, y2)) on the given surface. There are no endcaps.
surface (Surface) -- surface to draw on
x1 (int) -- x coordinate of one end of the line
y1 (int) -- y coordinate of one end of the line
x2 (int) -- x coordinate of the other end of the line
y2 (int) -- y coordinate of the other end of the line
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an unfilled rectangle on the given surface. For a filled rectangle use box().
None
NoneType
Note
The rect.bottom and rect.right attributes of a pygame.Rect always lie one pixel outside of its actual border. Therefore, these values will not be included as part of the drawing.
Draws a filled rectangle on the given surface. For an unfilled rectangle use rectangle().
None
NoneType
Note
The rect.bottom and rect.right attributes of a pygame.Rect always lie one pixel outside of its actual border. Therefore, these values will not be included as part of the drawing.
Note
The pygame.Surface.fill() method works just as well for drawing filled rectangles. In fact pygame.Surface.fill() can be hardware accelerated on some platforms with both software and hardware display modes.
Draws an unfilled circle on the given surface. For a filled circle use filled_circle().
None
NoneType
Draws an unfilled antialiased circle on the given surface.
None
NoneType
Draws a filled circle on the given surface. For an unfilled circle use circle().
None
NoneType
Draws an unfilled ellipse on the given surface. For a filled ellipse use filled_ellipse().
surface (Surface) -- surface to draw on
x (int) -- x coordinate of the center of the ellipse
y (int) -- y coordinate of the center of the ellipse
rx (int) -- horizontal radius of the ellipse
ry (int) -- vertical radius of the ellipse
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an unfilled antialiased ellipse on the given surface.
surface (Surface) -- surface to draw on
x (int) -- x coordinate of the center of the ellipse
y (int) -- y coordinate of the center of the ellipse
rx (int) -- horizontal radius of the ellipse
ry (int) -- vertical radius of the ellipse
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws a filled ellipse on the given surface. For an unfilled ellipse use ellipse().
surface (Surface) -- surface to draw on
x (int) -- x coordinate of the center of the ellipse
y (int) -- y coordinate of the center of the ellipse
rx (int) -- horizontal radius of the ellipse
ry (int) -- vertical radius of the ellipse
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an arc on the given surface. For an arc with its endpoints connected to its center use pie().
The two angle arguments are given in degrees and indicate the start and stop positions of the arc. The arc is drawn in a clockwise direction from the start_angle to the stop_angle. If start_angle == stop_angle, nothing will be drawn
surface (Surface) -- surface to draw on
x (int) -- x coordinate of the center of the arc
y (int) -- y coordinate of the center of the arc
r (int) -- radius of the arc
start_angle (int) -- start angle in degrees
stop_angle (int) -- stop angle in degrees
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Note
This function uses degrees while the pygame.draw.arc() function uses radians.
Draws an unfilled pie on the given surface. A pie is an arc() with its endpoints connected to its center.
The two angle arguments are given in degrees and indicate the start and stop positions of the pie. The pie is drawn in a clockwise direction from the start_angle to the stop_angle. If start_angle == stop_angle, a straight line will be drawn from the center position at the given angle, to a length of the radius.
surface (Surface) -- surface to draw on
x (int) -- x coordinate of the center of the pie
y (int) -- y coordinate of the center of the pie
r (int) -- radius of the pie
start_angle (int) -- start angle in degrees
stop_angle (int) -- stop angle in degrees
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an unfilled trigon (triangle) on the given surface. For a filled trigon use filled_trigon().
A trigon can also be drawn using polygon() e.g. polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)
surface (Surface) -- surface to draw on
x1 (int) -- x coordinate of the first corner of the trigon
y1 (int) -- y coordinate of the first corner of the trigon
x2 (int) -- x coordinate of the second corner of the trigon
y2 (int) -- y coordinate of the second corner of the trigon
x3 (int) -- x coordinate of the third corner of the trigon
y3 (int) -- y coordinate of the third corner of the trigon
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an unfilled antialiased trigon (triangle) on the given surface.
An aatrigon can also be drawn using aapolygon() e.g. aapolygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)
surface (Surface) -- surface to draw on
x1 (int) -- x coordinate of the first corner of the trigon
y1 (int) -- y coordinate of the first corner of the trigon
x2 (int) -- x coordinate of the second corner of the trigon
y2 (int) -- y coordinate of the second corner of the trigon
x3 (int) -- x coordinate of the third corner of the trigon
y3 (int) -- y coordinate of the third corner of the trigon
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws a filled trigon (triangle) on the given surface. For an unfilled trigon use trigon().
A filled_trigon can also be drawn using filled_polygon() e.g. filled_polygon(surface, ((x1, y1), (x2, y2), (x3, y3)), color)
surface (Surface) -- surface to draw on
x1 (int) -- x coordinate of the first corner of the trigon
y1 (int) -- y coordinate of the first corner of the trigon
x2 (int) -- x coordinate of the second corner of the trigon
y2 (int) -- y coordinate of the second corner of the trigon
x3 (int) -- x coordinate of the third corner of the trigon
y3 (int) -- y coordinate of the third corner of the trigon
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
Draws an unfilled polygon on the given surface. For a filled polygon use filled_polygon().
The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).
surface (Surface) -- surface to draw on
points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2 of 2 ints/floats (float values will be truncated)
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
ValueError -- if len(points) < 3 (must have at least 3 points)
IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)
Draws an unfilled antialiased polygon on the given surface.
The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).
surface (Surface) -- surface to draw on
points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2 of 2 ints/floats (float values will be truncated)
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
ValueError -- if len(points) < 3 (must have at least 3 points)
IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)
Draws a filled polygon on the given surface. For an unfilled polygon use polygon().
The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).
surface (Surface) -- surface to draw on
points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2 of 2 ints/floats (float values will be truncated)`
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
ValueError -- if len(points) < 3 (must have at least 3 points)
IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)
Draws a textured polygon on the given surface. For better performance, the surface and the texture should have the same format.
A per-pixel alpha texture blit to a per-pixel alpha surface will differ from a pygame.Surface.blit() blit. Also, a per-pixel alpha texture cannot be used with an 8-bit per pixel destination.
The adjacent coordinates in the points argument, as well as the first and last points, will be connected by line segments. e.g. For the points [(x1, y1), (x2, y2), (x3, y3)] a line segment will be drawn from (x1, y1) to (x2, y2), from (x2, y2) to (x3, y3), and from (x3, y3) to (x1, y1).
surface (Surface) -- surface to draw on
points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2 of 2 ints/floats (float values will be truncated)
texture (Surface) -- texture to draw on the polygon
tx (int) -- x offset of the texture
ty (int) -- y offset of the texture
None
NoneType
ValueError -- if len(points) < 3 (must have at least 3 points)
IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)
Draws a Bézier curve on the given surface.
surface (Surface) -- surface to draw on
points (tuple(coordinate) or list(coordinate)) -- a sequence of 3 or more (x, y) coordinates used to form a curve, where each coordinate in the sequence must be a tuple/list/pygame.math.Vector2 of 2 ints/floats (float values will be truncated)
steps (int) -- number of steps for the interpolation, the minimum is 2
color (Color or tuple(int, int, int, [int])) -- color to draw with, the alpha value is optional if using a tuple (RGB[A])
None
NoneType
ValueError -- if steps < 2
ValueError -- if len(points) < 3 (must have at least 3 points)
IndexError -- if len(coordinate) < 2 (each coordinate must have at least 2 items)
© Pygame Developers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/gfxdraw.html