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 around the edge of the shape. If a width of 0 is passed the function will actually solid fill the entire shape.
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.
Most of the arguments accept a color argument that is an RGB
triplet. These can also accept an RGBA
quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently. The color argument can also be an integer pixel value that is already mapped to the Surface's pixel format.
These functions must 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.
pygame.draw.rect(Surface, color, Rect, width=0) -> Rect
draw a rectangle shape
Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.
Keep in mind the Surface.fill()
method works just as well for drawing filled rectangles. In fact the Surface.fill()
can be hardware accelerated on some platforms with both software and hardware display modes.
pygame.draw.polygon(Surface, color, pointlist, width=0) -> Rect
draw a shape with any number of sides
Draws a polygonal shape on the Surface. The pointlist argument is the vertices of the polygon. The width argument is the thickness to draw the outer edge. If width is zero then the polygon will be filled.
For aapolygon, use aalines with the 'closed' parameter.
pygame.draw.circle(Surface, color, pos, radius, width=0) -> Rect
draw a circle around a point
Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.
pygame.draw.ellipse(Surface, color, Rect, width=0) -> Rect
draw a round shape inside a rectangle
Draws an elliptical shape on the Surface. The given rectangle is the area that the circle will fill. The width argument is the thickness to draw the outer edge. If width is zero then the ellipse will be filled.
pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1) -> Rect
draw a partial section of an ellipse
Draws an elliptical arc on the Surface. The rect argument is the area that the ellipse will fill. The two angle arguments are the initial and final angle in radians, with the zero on the right. The width argument is the thickness to draw the outer edge.
pygame.draw.line(Surface, color, start_pos, end_pos, width=1) -> Rect
draw a straight line segment
Draw a straight line segment on a Surface. There are no endcaps, the ends are squared off for thick lines.
pygame.draw.lines(Surface, color, closed, pointlist, width=1) -> Rect
draw multiple contiguous line segments
Draw a sequence of lines on a Surface. The pointlist argument is a series of points that are connected by a line. If the closed argument is true an additional line segment is drawn between the first and last points.
This does not draw any endcaps or miter joints. Lines with sharp corners and wide line widths can have improper looking corners.
pygame.draw.aaline(Surface, color, startpos, endpos, blend=1) -> Rect
draw fine antialiased lines
Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.
pygame.draw.aalines(Surface, color, closed, pointlist, blend=1) -> Rect
draw a connected sequence of antialiased lines
Draws a sequence on a surface. You must pass at least two points in the sequence of points. The closed argument is a simple Boolean and if true, a line will be draw between the first and last points. The Boolean blend argument set to true will blend the shades with existing shades instead of overwriting them. This function accepts floating point values for the end points.
# 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 Developpers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/draw.html