Python

# Pygame Draw Objects and Shapes

By ayed_amira , on 10/14/2020 , updated on 10/14/2020 - 6 minutes to read

Pygame Draw : In this tutorial, we will see how to create simple geometric figures with the Draw function of the Pygame module.

## Introduction

Pygame is a free cross-platform library that facilitates the development of real-time video games using the Python programming language. It allows you to program the multimedia part (graphics, sound and keyboard, mouse or joystick inputs).

Pygame, in addition to adapting SDL to Python, also provides a small number of functions specific to game development. One of the most commonly used functions is the draw function. The draw function allows you to create a multitude of simple and complex geometric figures.

Here are the different shapes that we will go through in this tutorial :

If you need to install the pygame module, I recommend you to check my previous tutorial :

## Pygame Draw : Getting Started

### Create a new window

Before we can use the draw function, we must import and initialize the pygame module. Here is the code to do this:

```#import et initialize pygame module

import pygame

pygame.init()

```

To open a new window, you can use the following code:

```# Open new window

screen = pygame.display.set_mode((650, 450))
```

The function pygame.display.set_mode will open a new window of size 650px by 450px. We will store this window in an object called screen which will be useful later to draw our different shapes. You can set any screen size, it depends on your need.

If you have set the screen size to 650×450, this means that the window can contain 650×450 = 292,500 points.

• The point (0,0) is in the upper left corner of the screen.
• The point (650,0) is in the upper right corner of the screen.
• The point (0,450) is in the lower left corner of the screen.
• The point (650,450) is in the lower right corner of the screen.

To summarize, the x-coordinates increase from left to right and the y-coordinates increase from top to bottom.

### Update Window

To update the window, we must use the function
pygame.display.update(). Without this function, it will be impossible to see the display changes on our window.

Here is an example that allows you to change the background color of the window and update it:

```import pygame

pygame.init()

screen = pygame.display.set_mode((650, 450))

# Define Colors

red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
darkBlue = pygame.Color(0, 0, 128)
white = pygame.Color(255, 255, 255)
black = pygame.Color(0, 0, 0)
pink = pygame.Color(255, 200, 200)
yellow = pygame.Color(255, 255, 0)

while True:
# exit the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.display.update()
```

Result :

The function screen.fill(color) allows to fill the whole screen with the color passed in parameter. To update the screen, the pygame.display.update() function must be used.

## Pygame Draw

### Draw a rectangle

pygame.draw.rect(surface, color, pygame.Rect(left, top, width, height))

The first shape we will draw is a rectangle. To do this we will use the function rect().

This function can take several parameters, the most important of which are :

• surface: surface to draw on
• color: color to draw with
• rect (Rect): rectangle to draw, position, and dimensions
• width (int,optional): used for line thickness or to indicate that the rectangle must be filled (not to be confused with the width value of the rest parameter)

Here is the code to draw a filled or unfilled rectangle:

```import pygame

pygame.init()

screen = pygame.display.set_mode((650, 450))
red = pygame.Color(255, 0, 0)
darkBlue = pygame.Color(0, 0, 128)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.rect(screen, red, (200, 180, 60, 100))
pygame.draw.rect(screen, red, (400, 180, 60, 100), 2)
pygame.display.update()
```

### Draw a line

pygame.draw.line (surface, color, (startX, startY), (endX, endY), width)

```import pygame

pygame.init()

screen = pygame.display.set_mode((650, 450))
green = pygame.Color(0, 255, 0)
darkBlue = pygame.Color(0, 0, 128)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.line(screen, green, (140, 220), (400, 250), 2)
pygame.display.update()
```

### Draw multiple contiguous lines

pygame.draw.lines(screen, color, closed , points, width)

```import pygame

pygame.init()

screen = pygame.display.set_mode((650, 450))

darkBlue = pygame.Color(0, 0, 128)
yellow = pygame.Color(255, 255, 0)

while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)

points = [(325, 0), (650, 225), (325, 450), (0, 225)]
pygame.draw.lines(screen, yellow, True, points, 5)

pygame.display.update()
```

### Draw a Circle

```import pygame

pygame.init()

screen = pygame.display.set_mode((650, 450))
darkBlue = pygame.Color(0, 0, 128)
white = pygame.Color(255, 255, 255)

while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.circle(screen, white, (325, 225), 100)
pygame.display.update()
```

### Draw an Arc

pygame.draw.arc(surface, color, rect, start_angle, stop_angle,width)

```import pygame
import math
pygame.init()

screen = pygame.display.set_mode((650, 450))

green = pygame.Color(0, 255, 0)
darkBlue = pygame.Color(0, 0, 128)

while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.arc(screen, green, [200, 200, 50, 50], math.pi / 2, math.pi, 5)

pygame.display.update()
```

### Draw a polygon

pygame.draw.polygon(surface, color, point_list)

```import pygame
pygame.init()

screen = pygame.display.set_mode((650, 450))
red = pygame.Color(255, 0, 0)
darkBlue = pygame.Color(0, 0, 128)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.polygon(screen, red, ((252, 75), (76, 125), (250, 375), (400, 25), (60, 340)))
pygame.display.update()
```

### Draw an ellipse

pygame.draw.ellipse(surface, color, rect, width=0)

```import pygame
pygame.init()

screen = pygame.display.set_mode((650, 450))
darkBlue = pygame.Color(0, 0, 128)
yellow = pygame.Color(255, 255, 0)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

screen.fill(darkBlue)
pygame.draw.ellipse(screen, yellow, (100, 100, 300, 150))
pygame.display.update()
```

## Conclusion

We have seen in this tutorial how to create simple shapes with Pygame. It is possible to create more complex shapes by playing with the parameters of the different functions we have seen. I hope this article has helped you better understand the draw function of the Pygame module.

In any case, don’t hesitate to tell me in comments if you have any problems using one of these functions, I would be happy to answer.

See you soon!

Back to the python section