Table of contents
1.
Introduction 
2.
Explanation of functions used
3.
Implementation
4.
Frequently Asked Questions
4.1.
How do you make a rectangle object in pygame?
4.2.
Which function in pygame is used to draw a rectangle?
4.3.
What does Pygame draw rect do?
4.4.
How to draw a rectangle in Python?
5.
Conclusion
Last Updated: Feb 5, 2025
Medium

How to draw a rectangle in Pygame?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Pygame is a video game development library written in Python. On top of the excellent SDL library, Pygame adds capabilities. This allows you to use Python to construct full-featured games and multimedia programmes.

How to draw a rectangle in Pygame?

Explanation of functions used

Functions Used:

  • pygame.display.set_mode(): This function sets up a display surface. The size of the display is a parameter for this function.
  • pygame.display.flip(): This function is used to update the content throughout the entire display surface of the full screen.
  • pygame.draw.rect(): To draw a rectangle, use this function. It draws a rectangle on the surface using the surface, colour, and pygame Rect object as input parameters.

Implementation

Here we draw just a simple rectangle

# Importing the library
import pygame
  
# Initializing Pygame
pygame.init()
  
# Initializing surface
surface = pygame.display.set_mode((400,300))
  
# Initialing Color
color = (255,0,0)
  
# Drawing Rectangle
pygame.draw.rect(surface, color, pygame.Rect(30, 30, 60, 60))
pygame.display.flip()

Output: 

output

Now we draw just a bordered rectangle. 

# Importing the library
import pygame
  
# Initializing Pygame
pygame.init()
  
# Initializing surface
surface = pygame.display.set_mode((400,300))
  
# Initialing Color
color = (255,0,0)
  
# Drawing Rectangle
pygame.draw.rect(surface, color, pygame.Rect(30, 30, 60, 60),  2)
pygame.display.flip()

Output:

output

Frequently Asked Questions

How do you make a rectangle object in pygame?

The most basic way to create a Rect object in Pygame is to pass two tuples into the Pygame. Rect() Class. The first tuple (left, top) represents the position of the rect on the window, whereas the second tuple (width, height) represents the dimensions of the Rect.

Which function in pygame is used to draw a rectangle?

To do this, you'll need to use the pygame. draw. rect module, and you'll need to use the DISPLAY constant to add it to the screen and the variable blue to make it blue (blue is a tuple of values equivalent to blue in RGB values and its coordinates).

What does Pygame draw rect do?

pygame.draw.rect() draws a rectangle on a Pygame surface using specified color, position, width, and height, with an optional border thickness.

How to draw a rectangle in Python?

In Python, use pygame.draw.rect(surface, color, rect, width) for Pygame or matplotlib.patches.Rectangle() for Matplotlib to create a rectangle.

Conclusion

Drawing a rectangle in Pygame is a simple yet essential task for game development and graphical applications. Using pygame.draw.rect(), you can easily create rectangles with different colors, sizes, and border thicknesses. This function is useful for rendering game objects, UI elements, and interactive components.

Live masterclass