Drawing Objects and Shapes in PyGame

You can easily draw basic shapes in pygame using the draw method of pygame. 

Drawing Rectangle shape: 

To draw a rectangle in your pygame project you can use draw.rect() function.

Syntax: pygame.draw.rect(surface, color, rect, width)

Parameters:

  • surface :- Here we can pass the surface on which we want to draw our rectangle. In the above example, we created a surface object named ‘window’.
  • color :- Here we can pass the color for our rectangle. We are using blue color in our example.
  • rect :- Here we can pass the rectangle, position, and dimensions.
  • width :- Here we can pass the line thickness. we can also create a solid rectangle by changing the value of this width parameter. So let’s look at that.

First, import the required module and initialize pygame. Now, Create the surface object of a specific dimension using the display.set_mode() method of pygame. Fill the background of the surface object with white color using the fill() function of pygame. Create a rectangle using the draw.rect() method of pygame. Update the Surface object.

Example 1: Drawing outlined rectangle using pygame.

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the outlined rectangle
pygame.draw.rect(window, (0, 0, 255),
                 [100, 100, 400, 100], 2)
 
# Draws the surface object to the screen.
pygame.display.update()


Output :

We can create a solid rectangle by setting the width parameter equal to 0 and the rest of the approach remains the same.

Example 2: Drawing a solid rectangle.

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the solid rectangle
pygame.draw.rect(window, (0,   0, 255),
                 [100, 100, 400, 100], 0)
 
# Draws the surface object to the screen.
pygame.display.update()


Output :

Drawing Circle Shape:

To draw a circle in your pygame project you can use draw.circle() function. The entire approach is the same as above only the function and parameters are changed accordingly.

Syntax : pygame.draw.circle(surface, color, center, radius, width)

Parameters :

  • surface :- Here we can pass the surface on which we want to draw our circle. In the above example, we created a surface object named ‘window’.
  • color :- Here we can pass the color for our circle. We are using green color in our example.
  • center :- Here we can pass the ( x, y ) coordinates for the center of the circle.
  • radius :- Here we can pass the radius of our circle.
  • width :- Here we can pass the line thickness. we can also create a solid circle by changing the value of this width parameter. So let’s look at that.

Example 1: Drawing a hollow circle.

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 170, 3)
 
# Draws the surface object to the screen.
pygame.display.update()


Output :

We can create a solid circle by setting the width parameter equal to 0. 

Example 2: drawing a solid circle

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the solid circle
pygame.draw.circle(window, (0, 255, 0),
                   [300, 300], 170, 0)
 
# Draws the surface object to the screen.
pygame.display.update()


Output :

Similarly, you can draw other basic shapes using the draw module of pygame. 

Drawing Polygon Shape:

The desired polygon can be drawn using polygon() function. 

Syntax: polygon(surface, color, points, width)

Again the approach remains the same only the function and the parameters change.

Example 1: drawing a solid polygon

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0),
                    [[300, 300], [100, 400],
                     [100, 300]])
 
# Draws the surface object to the screen.
pygame.display.update()


Output :

Example 2: Drawing a hollow polygon

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the outlined polygon
pygame.draw.polygon(window, (255, 0, 0),
                    [[300, 300], [100, 400],
                     [100, 300]], 5)
 
# Draws the surface object to the screen.
pygame.display.update()


Output:

Drawing Line Shape:

A line is the most basic drawing entity and can be drawn in pygame using line() function.

Syntax : pygame.draw.line(surface, color, start_pos, end_pos, width)

Example 1: drawing a line

Python3




# Importing pygame module
import pygame
from pygame.locals import *
 
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# create the display surface object
# of specific dimension.
window = pygame.display.set_mode((600, 600))
 
# Fill the scree with white color
window.fill((255, 255, 255))
 
# Using draw.rect module of
# pygame to draw the line
pygame.draw.line(window, (0, 0, 0),
                 [100, 300],
                 [500, 300], 5)
 
# Draws the surface object to the screen.
pygame.display.update()


Output:

Pygame – Drawing Objects and Shapes

In this article, we are going to see how to draw an object using Pygame. There can be two versions for drawing any shape, it can be a solid one or just an outline of it.

Similar Reads

Drawing Objects and Shapes in PyGame

You can easily draw basic shapes in pygame using the draw method of pygame....

Draw Multiple Shapes:

...

Writing your own drawing functions:

...

Drawing shapes with the mouse:

...