Open In App

How to draw rectangle in Pygame?

Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language.

Functions  Used:



Example 1: This example draws a rectangle that is filled with red color.




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

Output:



Example 2: This example draws a rectangle with a red border and no color-filled inside.




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

Output:


Article Tags :