Drawing Different Shapes in pygame Using Python

In this module, we are going to learn how to draw various shapes like polygon, rectangle, circle, ellipse, etc. in pygame using Python programming.

How to draw Shapes using pygame.draw in Python

Here, we are going to implement pygame draw function whose syntax is as follows.

pygame.draw.shape_name(parameters)

Drawing a Rectangle

First, let us implement the most basic shape that helps build different games the syntax is as follows.

pygame.draw.rect(surface_object,color,tuple_of_position_width_height)

Here, tuple includes the position of the rectangle for the first two values and width, height for the next two values. The following code describes drawing a rectangle.

import pygame
import sys
pygame.init()
disp_surf=pygame.display.set_mode((350,250))
pygame.display.set_caption('Shapes Demo')
disp_surf.fill((255,255,255))
pygame.draw.rect(disp_surf,(255,0,0),(32,25,52,42))
while True:
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

Output:

How to draw Shapes using pygame.draw in Python

We get a rectangle of red in color of width, height as (52,42) and it is placed at position (32,25).

Drawing a Polygon

The basic syntax for drawing a polygon is as follows.

pygame.draw.polygon(disp_surf,color,tuple)

The tuple consists of two value tuples which indicates drawing of the line at given tuple. For the pentagon, we take 5 tuples and for hexagon, we take 6 tuples. The following code implements a polygon called a pentagon.

import pygame
import sys
pygame.init()
disp_surf=pygame.display.set_mode((350,350))
pygame.display.set_caption('Shapes Demo')
disp_surf.fill((255,255,255))
pygame.draw.polygon(disp_surf,(0,0,255),((146,0),(291,106),(236,277),(56,277),(0,106)))
while True:
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

Output:

Drawing a Polygon

We get output as a pentagon blue in color tuple values indicate the position of lines drawn to form a polygon.

Drawing a Circle

To draw a circle the syntax follows.

pygame.draw.circle(disp_surf,color,center_point,radius)

Here, center_point indicates the center of circle nothing but from that pixel as center, it draws a circle of mentioned radius. The following code describes the drawing of a circle.

import pygame
import sys
pygame.init()
disp_surf=pygame.display.set_mode((250,250))
pygame.display.set_caption('Shapes Demo')
disp_surf.fill((255,255,255))
pygame.draw.circle(disp_surf,(0,255,0),(125,125),32)
while True:
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

Output:

Drawing a Circle in Pygame

We get output as a circle at center i.e, position (125,125) with a radius of 32 and the color of the circle is green.

Drawing an Ellipse

The syntax for drawing an ellipse is as follows.

pygame.draw.ellipse(sur_obj,color,tuple)

Here, tuple indicates 4 integer tuple which is called the bounding rectangle in which the ellipse will be placed. since we are using rectangle tuples there is no need to specify the center of the ellipse. the following code describes drawing an ellipse.

import pygame
import sys
pygame.init()
disp_surf=pygame.display.set_mode((350,250))
pygame.display.set_caption('Shapes Demo')
disp_surf.fill((255,255,255))
pygame.draw.ellipse(disp_surf,(0,255,0),(165,125,100,50))
while True:
    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

Output:

Drawing Different Shapes in pygame Using Python

We get output as an ellipse green in color at position (165,125) of width and height as(100,50).

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *