Pygame Drawing Objects and Shapes

In Pygame, as well as adjusting SDL to Python, additionally gives few capacities explicit to game turn of events. Perhaps the most ordinarily utilized function is the draw function methods. The methods available in pygame permits you to make a large number of basic and complex mathematical above figures.

Function name

Description of the function

draw.rect()

drawing a rectangle shape

draw.line()

drawing a straight line

draw.lines()

drawing multiple contiguous line

draw.simple circle

drawing a simple circle

draw.arc()

drawing an arc

draw.polygon()

drawing a shape with a multitude of sides

pygame.draw.ellipse()

drawing a round shape inside a rectangle

Drawing Rectangle shape in Pygame:

To attract a square shape your pygame project you can utilize draw.rect() work.

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

The concerned parameters of function explained:

  1. rect: Here we can pass the square shape, position, and aspects.
  2. shading: Here we can pass the shading for our square shape. We are utilizing blue tone in our example.
  3. surface: to draw our square shape, pass surface, which we need to the function. In the above example, we made a surface item named 'window1'.
  4. width: The line thickness can be passed. we can likewise make a strong square shape by changing the worth of this width boundary. So, we should check out that.

To start with, import the necessary module and introduce pygame. Presently, Creating the surface object of a particular aspect utilizing the display.set_mode() strategy for pygame. Filling the foundation of the surface item with white shading utilizing the filling() capacity of pygame. Make a square shape utilizing the draw.rect() technique for pygame.

Example 1: Using pygame drawing outlined rectangle

Instructions to proceed with the code:

  1. pygame module import statement
  2. give permission and initiating the pygame
  3. using the pygame's functionality for creating the display surface object
  4. Filling the console screen with white colour
  5. pygame to draw the outlined rectangle
  6. Draws the surface object to the console screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window11 = pygame.display.set_mode((600, 600))
window11.filling((256, 256, 256))
pygame.draw.rect(window11,(0, 0, 256),
                                     [100, 200, 400, 100 ], 2)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Example 2: Drawing a solid rectangle.

Instructions to proceed with the code:

  1. Importing pygame module
  2. initiating pygame and give permission
  3. using pygame's functionality.
  4. Fillinging the console screen with white colour
  5. Using draw.rect fuction module
  6. pygame methods to draw the solid shape rectangle
  7. Drawing the surface object to the console screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window11 = pygame.display.set_mode ((700, 700))
window11.filling((256, 256, 256))
pygame.draw.rect(window11, (0, 0, 256),
         [101, 101, 401, 101 ], 0)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Drawing Simple circle Shape in Pygame:

To attract a simple circle your pygame project you can utilize draw.simple circle() work. The whole methodology is as old as just the capacity and boundaries are changed appropriately.

Syntax: pygame.draw.simple circle(surface, shading, focus, sweep, width)

The parameters of function explained:

  1. range: Here we can pass the span of our simple circle.
  2. width: The line thickness can be passed. we can likewise make a strong simple circle by changing the worth of this width boundary. So how about we check out that.
  3. surface: to draw our square circle, pass surface, which we need to the function. In the above example, we made a surface item named 'window1'.
  4. shading: Here we can pass the shading for our simple circle. We are utilizing green tone in our example.
  5. focus: Here we can pass the(x, y) arranges for the focal point of the simple circle.

Example 1: Drawing a hollow simple circle in Pygame

Instructions to proceed with the code:

  1. Step: Importing pygame module
  2. Step: initiating pygame and give permission
  3. Step: using pygame's functionality.
  4. Step: Filling the console screen with white colour
  5. Step: Using draw.rect module of pygame, drawing the solid simple circle
  6. Step: Drawing surface objects to the console screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window11 = pygame.display.set_mode((600, 600))
window11.filling((256, 256, 256))
pygame.draw.simple circle(window11,(0, 256, 0) ,  [300, 300 ], 170, 3)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

We are now creating a solid simple circle by setting the width parameter equal to 0.

Example 2: drawing a solid simple circle in Pygame

Instructions to proceed with the code:

  1. Step: Bringing in pygame module
  2. Step: starting pygame and allow to utilize pygame's usefulness.
  3. Step: Filling the control center screen with white tone
  4. Step: Utilizing draw.rect module of
  5. Step: pygame to draw the strong straightforward circle
  6. Step: Attracts the surface item to the control center screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window11 = pygame.display.set_mode((600, 600))
window11.filling((256, 256, 256))
pygame.draw.simple circle(window11,(0, 256, 0),
[300, 300 ], 170, 0)
pygame.display.update()
Pygame Drawing Objects And Shapes

Drawing Polygon Shape in Pygame:

In pygame we can use polygon () function and draw desired polygon.

Syntax: polygon (surfacename, colour name, points, width)

Instructions to proceed with the code:

  1. Step: Bringing in pygame module
  2. Step: starting pygame and allow to utilize pygame's usefulness.
  3. Step: making the presentation surface object of explicit aspect.
  4. Step: Filling the control center screen with white tone
  5. Step: Utilizing draw.rect module of
  6. Step: pygame to draw the laid out polygon
  7. Step: Attracts the surface item to the control console screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window1 = pygame.display.set_mode((600, 600))
window1.filling((256, 256, 256))
pygame.draw.polygon(window1,(256, 0, 0),  [ [300, 300 ],  [100, 400 ], [100, 300 ] ])
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Example 2.

Instructions to proceed with the code:

  1. Step: Bringing in pygame module
  2. Step: starting pygame and allow to utilize pygame's function.
  3. Step: making the presentation surface object of explicit aspect.
  4. Step: Filling the control center screen with white tone
  5. Step: Utilizing draw.rect module of
  6. Step: pygame to draw the laid out the polygon shape.
  7. Step: Here filling code is given as 5 that is to fill the polygon with color.
  8. Step: Attracts the surface item to the control console screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window1 = pygame.display.set_mode((600, 600))
window1.filling((256, 256, 256))
pygame.draw.polygon(window1,(256, 0, 0), [ [300, 300 ],  [100, 400 ],  [100, 300 ] ], 5)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Drawing Line Shape in Pygame:

Line: Basic drawing entity in objects and shape chapter and in pygame it can be drawn using line() method.

Syntax: pygame.draw.line(surface name, colour name, start_pos no, end_pos no,

width size)

Instructions to proceed with the code:

  1. Step: importing in pygame module
  2. Step: starting pygame and allow to utilize pygame's usefulness.
  3. Step: making the presentation surface object of explicit aspect.
  4. Step: Filling the console’s screen with white tone
  5. Step: Utilizing draw.rect() module of pygame to take a stand
  6. Step: Attracts the surface item to the control center screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window1 = pygame.display.set_mode((600, 600))
window1.filling((256, 256, 256))
pygame.draw.line(window1,(0, 0, 0),
       [100, 300 ],
       [500, 300 ], 5)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Drawing multiple contiguous lines in Pygame

Syntax: pygame.draw.lines(console screen, colour, closed  points, points number, width number)

Instructions to proceed with the code:

  1. Step: Bringing in pygame module
  2. Step: starting pygame and allow to utilize pygame's function.
  3. Step: making the presentation surface object of explicit aspect.
  4. Step: Filling the control center screen with white tone
  5. Step: Utilizing draw.lines module of
  6. Step: pygame to draw the contiguous lines
  7. Step: Here filling code is given as 5 that is to fill background color.
  8. Step: Attracts the surface item to the control console screen.

Below is the source code:

import pygame
pygame.init()
console screen = pygame.display.set_mode((650, 450)) 
darkBlue = pygame.Colour(0, 0, 128)
yellow = pygame.Colour(256, 256, 0)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    console screenn.filling(darkBlue)
    points =  [(325, 0),(650, 225),(325, 450),(0, 225) ]
    pygame.draw.lines(console screen, yellow, True, points, 5)
    pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Drawing Multiple Shapes in Pygame

You can draw different shapes on a comparable console. For that, the essential required modules are imported and pygame is presented. As of now, making the surface object of a specific perspective using the display.set_mode() system for pygame. Filling the establishment of the surface thing with white concealing using the filling() limit of pygame. Making required shapes are portrayed beforehand. Update the Surface at last step

Example 1: Drawing a simple circle inside a rectangle.

Instructions to proceed with the code:

  1. Step: importing pygame module
  2. Step: starting pygame and give consent to utilize pygame's usefulness.
  3. Step: making the presentation surface itemof explicit aspect.
  4. Step: Filling the console screen with white tone
  5. Step: Utilizing draw.rect module of pygame to draw the square shape
  6. Step: Utilizing draw.rect module of
  7. Step: pygame to draw the straight forward circle inside the square shape
  8. Step: Attracts the surface item to the control center screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init ()
window1 = pygame.display.set_mode ((600, 600))
window1.filling ((256, 256, 256))
pygame.draw.rect(window1,(0, 0, 256),  [50, 200, 500, 200 ])
pygame.draw.simple circle (window1, (0, 256, 0), [300, 300 ], 100)
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Example 2: Drawing rectangles inside one another.

Instructions to proceed with the code:

  1. Step: importing pygame module
  2. Step: starting pygame and give authorization to utilize pygame's usefulness.
  3. Step: making the showcase surface item of explicit aspect.
  4. Step: Filling the screen with white tone
  5. Step: Making a rundown of various rects
  6. Step: Making rundown of various tones
  7. Step: Making a variable which we will utilize to repeat over the colour_list
  8. Step: Drawing the square shape utilizing the draw.rect() technique
  9. Step: Expanding the worth of colour_var by 1 later every cycle
  10. Step: Attracts the surface item to the control center screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window1 = pygame.display.set_mode ((600, 600))
window1.filling ((256, 256, 256))
rectangle_list = [pygame.Rect (50, 100, 500, 200), pygame.Rect (70, 120, 460, 160),
 pygame.Rect (90, 140, 420, 120), pygame.Rect (110, 160, 380, 80), pygame.Rect (130, 180, 340, 40)] colour_list =  [(0, 0, 0), (256, 256, 256), (0, 0, 256), (0, 256, 0), (256, 0, 0) ]
colour_var = 0
for rectangle in rectangle_list:
            pygame.draw.rect(window1, colour_list [colour_var ], rectangle)
            colour_var += 1
pygame.display.update()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Writing our own drawing functions in Pygame

You can without much of a stretch making your own specific attracting capacities pygame. This should be possible by following the given strategy. Making a drawing capacity that will take beginning position, width, and tallness as boundaries. Draw required shapes are portrayed previously. Call the drawfunction()

Instructions to proceed with the code:

  1. Step: importing pygame module
  2. Step: Making Drawing capacity
  3. Step: Making square shape utilizing the draw.rect() strategy
  4. Step: Computation the focal point of the straightforward circle
  5. Step: Computing the sweep of the straightforward circle
  6. Step: Drawing the straightforward circle starting pygame and give consent to utilize.
  7. Step: Filling the screen with white tone
  8. Step: Calling the drawing capacity
  9. Step: Attracts the surface item to the control centre screen.

Below is the source code:

import pygame
from pygame.locals import *
def drawingfunction(x, y, width1, height1):
            pygame.draw.rect(window1,(0, 0, 256),  [x, y, width1, height1 ])
            simple circle_x = width1/2 + x
            simple circle_y = height1/2 + y
            if height1 < width1:
                   radius = height1/2
            else:
                   radius = width1/2
           pygame.draw.simple circle(window1,(0, 256, 0),  [simple circle_x, simple circle_y ], radius)
pygame.init()
window1 = pygame.display.set_mode ((600, 600))
window1.filling ((256, 256, 256))
drawingfunction (50, 200, 500, 200)
pygame.display.update ()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output

Drawing shapes with the help of mouse in Pygame

Presently how about we perceive how we can making shapes at whatever point the client taps the mouse. We are going to making straightforward circles in the following example however you can making any shape you need.

Making a rundown to store the place of the shape to be drawn. Making a variable to store the shade of the shape. Making a variable available which we will utilize later to execute the loop “while loop” and create some time complex circle. Repeat over every one of the occasions got from pygame.event.get(). On the off chance that the kind of the occasion is stopped then setting the run variable to bogus. Assuming the sort of the occasion is MOUSEBUTTONDOWN(this occasion happens when a client presses the mouse button) then, at that point, getting the current situation in a variable then, at that point, attaching that situation in our basic circle_positions list. Repeat over every one of the situations in of the exhibit creatingd utilizing a for a circle. Continue to draw the shape. Update the surface item.

Instructions to proceed with the code:

  1. Step: Importing pygame module
  2. Step: starting pygame and allow to utilize pygame's usefulness. making the presentation surface object of explicit aspect
  3. Step: Filling the screen with white tone making list in which we will store the place of the basic circle range of the basic circle
  4. Step: Shade of the straightforward circle
  5. Step: Making a variable which we will utilize
  6. Step: Emphasizing over every one of the occasions got from
  7. Step: Assuming that the kind of the occasion is stopped then, at that point, setting the run variable to bogus
  8. Step: Utilizing for circle to repeat over the straightforward circle_positions list
  9. Step: Attracts the surface item to the control center screen.

Below is the source code:

import pygame
from pygame.locals import *
pygame.init()
window1 = pygame.display.set_mode ((600, 600))
window1.filling ((256, 256, 256))
simple circle_positions =  [ ]
simple circle_radius = 60
colour = (0, 0, 256)
run = True
while run:
            for event in pygame.event.get ():
                        if event.type == QUIT:
                                    run = False
                        elif event.type == MOUSEBUTTONDOWN:
                                    position = event.pos
                      simple circle_positions.append(position)
            for position in simple circle_positions:
               pygame.draw.simple circle (window1, colour, position, simple circle_radius)
            pygame.display.update ()

Output:

Pygame Drawing Objects And Shapes

Above figure: screenshot of obtained output