Drawing Objects and Shapes in Pygame

One useful feature of PyGame is drawing objects, certain shapes you should try.

Drawing Objects and Shapes in Pygame Picture 1

Module pygame.draw

In PyGame, drawing objects and shapes is a straightforward process. You can use the pygame.draw module to draw shapes like rectangles, circles, lines and more.

Before you start, make sure you have pip installed on your device, then use this command to install the PyGame module:

pip install pygame

Draw basic shapes

The pygame.draw module provides several functions that allow you to draw basic shapes. Each function uses a set of parameters to determine its shape and position.

  1. rect() : You can draw a rectangle using this function. This is one of the most common shapes in the game. You can create bases, walls, and other objects using rectangles. This function calculates 4 parameters: surface (rectangular drawing surface), color (color), rect (rect object) and width (line thickness).

 

pygame.draw.rect(surface, color, rect, width)
  1. circle() : Draws a circle. It calculates 4 parameters: surface, color, pos (center of the circle) and radius.
pygame.draw.circle(surface, color, pos, radius)
  1. polygon() : Draw a polygon. It calculates 4 parameters: surface, color, points (list of polygonal shape points) and width.
pygame.draw.polygon(surface, color, points, width)
  1. line() : Draws a straight line. It calculates 4 parameters: surface, color, start_pos and end_pos.
pygame.draw.line(surface, color, start_pos, end_pos)

Draw complex shapes

PyGame also allows you to draw complex shapes like arcs, ellipses, and aalines.

  1. arc() : This function draws an arc on the surface. An arc is a part of a circle. It calculates 6 parameters: surface, color, rect, start_angle, end_angle, width. Define angles in degrees, where 0 degrees are to the right and increments clockwise. The width parameter defines the thickness of the arc.
pygame.draw.arc(surface, color, rect, start_angle, end_angle, width)
  1. ellipse() : Draws an ellipse on the surface. An ellipse is an elongated circle. It calculates 4 parameters: surface, color, rect, width.
pygame.draw.ellipse(surface, color, rect, width)
  1. aaline() : Used to draw an anti-aliased line on the surface. Anti-aliasing is a technique used to smooth the edges of a shape so that it looks more natural. It takes 4 parameters that reflect the line() function: surface, color, start_pos and end_pos.
pygame.draw.aaline(surface, color, start_pos, end_pos)

Creating nested shapes in PyGame

Another cool feature of PyGame is drawing nested shapes. You can do this by combining the basic shape drawing functions in PyGame.

To draw a shape inside another, first draw the inner shape, then the outer shape above it. The inner shape can be a basic shape such as a rectangle or a circle.

For example, to draw a rectangle inside a circle, you can use the pygame.draw.circle() function to draw a circle, and then draw a rectangle on top of it.

 

Here is an example code that demonstrates how to draw a rectangle inside a circle:

import pygame # Chạy pygame pygame.init() # Tạo một cửa sổ window = pygame.display.set_mode((800, 600)) # Tạo một hình tròn circle_rect = pygame.draw.circle(window, (255, 255, 255), (400, 300), 100) # Tạo một bề mặt cho hình chữ nhật rectangle_surface = pygame.Surface((50, 50)) rectangle_surface.set_colorkey((0, 0, 0)) # Vẽ hình chữ nhật trên bề mặt pygame.draw.rect(rectangle_surface, (255, 0, 0), (0, 0, 50, 50)) # Làm mờ bề mặt trên hình tròn window.blit(rectangle_surface, (375, 275)) # Vòng lặp chính while True:     # Thoát game     for event in pygame.event.get():         if event.type == pygame.QUIT:             pygame.quit()             exit()     # Update màn hình     pygame.display.update()

The result is a rectangle inside the circle. You can use this technique to create more complex shapes and game elements by combining multiple shapes.

Drawing Objects and Shapes in Pygame Picture 2

Draw pictures

PyGame also allows you to draw pictures. You can use the pygame.image.load() function to load an image from a file and pygame.image.blit() to draw an image on the screen.

The syntax for pygame.image.load() would look like this:

pygame.image.load(filename)

Here is the syntax for pygame.image.blit():

pygame.image.blit(image, rect)

In addition to loading and drawing images, PyGame also provides a number of other image-related functions. These functions allow you to manipulate images in a variety of ways, such as rotating, flipping, cropping, and scaling.

Advanced Extra Features

PyGame also provides some advanced features for drawing objects and shapes. For example, you can use the pygame.mouse.get_pos() function to get the mouse position and the pygame.mouse.get_pressed() function to get the mouse button state. With these functions, you can create a click-to-draw feature that allows you to draw shapes by clicking the mouse.

import pygame # Chạy pygame pygame.init() # Tạo cửa sổ window = pygame.display.set_mode((800, 600)) # Loop chính while True:     # Lấy vị trí chuột     mouse_position = pygame.mouse.get_pos()     # Nhận trạng thái nút chuột     mouse_pressed = pygame.mouse.get_pressed()     # Thoát game     for event in pygame.event.get():         if event.type == pygame.QUIT:             pygame.quit()             exit()     # Vẽ hình tròn khi nhấn chuột     if mouse_pressed[0] == 1:         pygame.draw.circle(window, (255, 0, 0), mouse_position, 20)     # Update màn hình     pygame.display.update()

 

Result:

Drawing Objects and Shapes in Pygame Picture 3

Above is how to draw objects and shapes in PyGame . Hope the article is useful to you.

3.5 ★ | 2 Vote

May be interested

  • How to draw basic shapes in Adobe IllustratorHow to draw basic shapes in Adobe Illustrator
    how to draw basic shapes in adobe illustrator. to get started with graphic design skills with adobe illustrator, you first need to master the basics of drawing basic shapes on vector, which will be the basis for your creativity. the following, thu thu
  • How to group in Word, group multiple shapes into 1 in WordHow to group in Word, group multiple shapes into 1 in Word
    how to group in word, group multiple shapes into 1 in word. the group feature (group) in word helps you group shapes into a unified block, making moving a group of objects easier without fear of affecting the arrangement in the
  • How to Create Power-Ups and Collections in PygameHow to Create Power-Ups and Collections in Pygame
    make pygame projects more fun by integrating power-ups and collections. here are detailed instructions.
  • Instructions for drawing straight lines in WordInstructions for drawing straight lines in Word
    shapes are diagram creation tools in word, excel or powerpoint. and we can draw lines with shapes on word.
  • Before AutoCAD, the drawings created were complex and elaborate like thisBefore AutoCAD, the drawings created were complex and elaborate like this
    when there are no software to support technical drawing and especially autocad, the drawings are designed by designers on large blank pages. all stages must be done manually with a lot of complicated items such as high-grade pencils, compasses, erasers, drawing tables, rulers with many different shapes ...
  • How to fix the insertion block in ExcelHow to fix the insertion block in Excel
    inserting shape shapes into excel is very simple. so how to fix the inserted shapes into excel?
  • Coreldraw - Lesson 9: Road drawing toolsCoreldraw - Lesson 9: Road drawing tools
    line drawing tools in coreldraw 12 allow creating lines or curves through points. objects created with line drawing tools can change some properties such as: thickness, color ...
  • The easiest way to Draw with PowerPointThe easiest way to Draw with PowerPoint
    powerpoint has a basic set of tools for drawing shapes and lines into slides. to use the hands-free pen and drawing tool, you need to click on the 'review' tab and then select 'start inking' (the same tool can show up in the 'draw' tab on office 365). you can also use the contour tool and draw a basic shape located to the right of the 'home' tab. if you're using powerpoint instead of ms paint or another drawing program, you can export your slides into several types of image files when you save them.
  • Tutorial for Word 2016 (Part 18): How to add Shape shapesTutorial for Word 2016 (Part 18): How to add Shape shapes
    when working in word, you can insert multiple shapes such as arrows, captions, squares, stars, etc. adding shapes (shapes) into word is not new but with instructions we provide below, you will get more beautiful and interesting documents.
  • How to draw an object in GIMPHow to draw an object in GIMP
    sometimes users often want to doodle something in any graphic program. today's article will help readers get started and explain a general technique on how to draw an object in gimp.