How to draw a rectangle with Pygame

There are two methods you can use to put a game character on the screen with Pygame. The first method which I have shown you before is to insert either a png or a jpeg image on the screen with the screen.blit method. The second method which I am about to show you now is to draw on the screen. Actually you can create a game without using any image by just drawing square or circle on the screen and I am going to show you how to draw a few different shapes on the screen with Pygame in this pygame tutorial series. In this post we will draw two rectangles, one will be filled with color and another one is a hollow rectangle.

In order to draw a rectangle on the screen we will use the

pygame.draw.rect

method which will take a few arguments as shown in the below example.

pygame.draw.rect(screen, (100, 100, 150), Rect((150, 150), (100, 100)), 3)

The first argument is the surface of the screen, the second one is the tuple which contains the RGB value of the color which will be used to fill the entire rectangle or to be used as the color of the line if that rectangle is a hollow one. The third argument is the rectangle object itself which takes two arguments (the tuple which contains the top left corner’s (x and y coordinate point) of that rectangle (on the screen) as well as the tuple which contains the width and height of that rectangle). The fourth argument is an optional line width argument which if you use it will draw line around the hollow rectangle according to the width you have specified. If you leave out that fourth argument then the rectangle will be filled up with the color which we have specified.

Before we start to draw on a surface we will also need to lock that surface first with the following method.

screen.lock()

Then we also need to unlock that same surface again once we have finished drawing on it with this method.

screen.unlock()

Below is the entire script which I use to draw two rectangles on the screen, one is the hollow rectangle and another one is the rectangle filled with color.

import pygame
from pygame.locals import *
from sys import exit
from random import *
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.lock()
    rectangle_pos = (150, 150)
    rectangle_pos2 = (250, 350)
    rectangle_size = (100, 100)
    screen.fill((255,255,255))
    pygame.draw.rect(screen, (100, 100, 150), Rect(rectangle_pos, rectangle_size), 3)
    pygame.draw.rect(screen, (100, 100, 150), Rect(rectangle_pos2, rectangle_size))
    screen.unlock()
    pygame.display.update()

If you run the above script you will get the following outcome.

Draw rectangle with Pygame script
Draw rectangle with Pygame script

There are more tutorials on Pygame coming everyday so make sure to bookmark this Pygame channel for the daily Pygame tutorial : http://gamingdirectional.com/blog/category/pygame/