pygame module for drawing shapes
Draw several simple shapes to a surface. These functions will work for rendering to any format of surface. Rendering to hardware surfaces will be slower than regular software surfaces.
Most of the functions take a width argument to represent the size of stroke (thickness) around the edge of the shape. If a width of 0 is passed the shape will be filled (solid).
All the drawing functions respect the clip area for the surface and will be constrained to that area. The functions return a rectangle representing the bounding area of changed pixels. This bounding rectangle is the 'minimum' bounding box that encloses the affected area.
All the drawing functions accept a color argument that can be one of the following formats:
- a
pygame.Color
object- an
(RGB)
triplet (tuple/list)- an
(RGBA)
quadruplet (tuple/list)- an integer value that has been mapped to the surface's pixel format (see
pygame.Surface.map_rgb()
andpygame.Surface.unmap_rgb()
)
A color's alpha value will be written directly into the surface (if the surface contains pixel alphas), but the draw function will not draw transparently.
These functions temporarily lock the surface they are operating on. Many sequential drawing calls can be sped up by locking and unlocking the surface object around the draw calls (see pygame.Surface.lock()
and pygame.Surface.unlock()
).
Note
See the pygame.gfxdraw
module for alternative draw methods.
pygame.draw.rect(surface, color, rect) -> Rect
pygame.draw.rect(surface, color, rect, width=0) -> Rect
draw a rectangle
Draws a rectangle on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the given |
Return type: |
Note
The pygame.Surface.fill()
method works just as well for drawing filled rectangles and can be hardware accelerated on some platforms with both software and hardware display modes.
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.polygon(surface, color, points) -> Rect
pygame.draw.polygon(surface, color, points, width=0) -> Rect
draw a polygon
Draws a polygon on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the first point in the |
Return type: | |
Raises: |
|
Note
For an aapolygon, use aalines()
with closed=True
.
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.circle(surface, color, center, radius) -> Rect
pygame.draw.circle(surface, color, center, radius, width=0) -> Rect
draw a circle
Draws a circle on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the |
Return type: | |
Raises: |
ValueError -- if |
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.ellipse(surface, color, rect) -> Rect
pygame.draw.ellipse(surface, color, rect, width=0) -> Rect
draw an ellipse
Draws an ellipse on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the given |
Return type: |
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.arc(surface, color, rect, start_angle, stop_angle) -> Rect
pygame.draw.arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
draw an elliptical arc
Draws an elliptical arc on the given surface.
The two angle arguments are given in radians and indicate the start and stop positions of the arc. The arc is drawn in a counterclockwise direction from the start_angle
to the stop_angle
.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the given |
Return type: |
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.line(surface, color, start_pos, end_pos, width) -> Rect
pygame.draw.line(surface, color, start_pos, end_pos, width=1) -> Rect
draw a straight line
Draws a straight line on the given surface. There are no endcaps. For thick lines the ends are squared off.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the |
Return type: | |
Raises: |
TypeError -- if |
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.lines(surface, color, closed, points) -> Rect
pygame.draw.lines(surface, color, closed, points, width=1) -> Rect
draw multiple contiguous straight line segments
Draws a sequence of contiguous straight lines on the given surface. There are no endcaps or miter joints. For thick lines the ends are squared off. Drawing thick lines with sharp corners can have undesired looking results.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the first point in the |
Return type: | |
Raises: |
|
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.aaline(surface, color, start_pos, end_pos) -> Rect
pygame.draw.aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
draw a straight antialiased line
Draws a straight antialiased line on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the |
Return type: | |
Raises: |
TypeError -- if |
Changed in pygame 2.0.0: Added support for keyword arguments.
pygame.draw.aalines(surface, color, closed, points) -> Rect
pygame.draw.aalines(surface, color, closed, points, blend=1) -> Rect
draw multiple contiguous straight antialiased line segments
Draws a sequence of contiguous straight antialiased lines on the given surface.
Parameters: |
|
---|---|
Returns: |
a rect bounding the changed pixels, if nothing is drawn the bounding rect's position will be the position of the first point in the |
Return type: | |
Raises: |
|
Changed in pygame 2.0.0: Added support for keyword arguments.
# Import a library of functions called 'pygame' import pygame from math import pi # Initialize the game engine pygame.init() # Define the colors we will use in RGB format BLACK = ( 0, 0, 0) WHITE = (255, 255, 255) BLUE = ( 0, 0, 255) GREEN = ( 0, 255, 0) RED = (255, 0, 0) # Set the height and width of the screen size = [400, 300] screen = pygame.display.set_mode(size) pygame.display.set_caption("Example code for the draw module") #Loop until the user clicks the close button. done = False clock = pygame.time.Clock() while not done: # This limits the while loop to a max of 10 times per second. # Leave this out and we will use all CPU we can. clock.tick(10) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done=True # Flag that we are done so we exit this loop # All drawing code happens after the for loop and but # inside the main while done==False loop. # Clear the screen and set the screen background screen.fill(WHITE) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.line(screen, GREEN, [0, 0], [50,30], 5) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5) # Draw on the screen a GREEN line from (0,0) to (50.75) # 5 pixels wide. pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80], True) # Draw a rectangle outline pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2) # Draw a solid rectangle pygame.draw.rect(screen, BLACK, [150, 10, 50, 20]) # Draw an ellipse outline, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) # Draw an solid ellipse, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) # This draws a triangle using the polygon command pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5) # Draw an arc as part of an ellipse. # Use radians to determine what angle to draw. pygame.draw.arc(screen, BLACK,[210, 75, 150, 125], 0, pi/2, 2) pygame.draw.arc(screen, GREEN,[210, 75, 150, 125], pi/2, pi, 2) pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi,3*pi/2, 2) pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3*pi/2, 2*pi, 2) # Draw a circle pygame.draw.circle(screen, BLUE, [60, 250], 40) # Go ahead and update the screen with what we've drawn. # This MUST happen after all the other drawing commands. pygame.display.flip() # Be IDLE friendly pygame.quit()
© Pygame Developers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/draw.html