Table of Contents
Pygame's Draw
Drawing a rectangle
Draw a polygon
Draw a circle
Drawing an ellipse
Drawing arc curve
Draw a straight line
Draw multiple straight lines
Home Backend Development Python Tutorial How to use Pygame's Draw drawing method in Python

How to use Pygame's Draw drawing method in Python

Apr 19, 2023 pm 04:46 PM
python pygame draw

Pygame's Draw

Pygame provides a draw module for drawing some simple graphics shapes, such as rectangles, polygons, circles, straight lines, arcs, etc.

The common methods of the pygame.draw module are shown in the following table:

Name Description
pygame.draw.rect() Draw a rectangle
pygame.draw.polygon () Draw a polygon
pygame.draw.circle() Draw a circle based on the center and radius Shape
pygame.draw.ellipse() Draw an ellipse
pygame.draw.arc() Draw an arc (waving part of the ellipse)
pygame.draw.line() Draw line segments (straight lines)
pygame.draw.lines() Draw multiple continuous line segments
pygame.draw.aaline() Draw a smooth line segment (anti-aliasing)
pygame.draw.aalines() Draw multiple continuous line segments

The methods of using the functions in the table are similar. They can all draw some simple shapes on the Surface object, and the return value is a Rect object, representing the rectangular area in which the graphics is actually drawn. The above drawing functions all provide a color parameter. We can pass the color parameter value in the following three ways:

  • pygame.color Object

  • RGB Triplets

  • RGBA Quadruples

The following is a detailed description of the parameters of some of the above methods:

Drawing a rectangle

The syntax format for drawing a rectangle is as follows:

pygame.draw.rect(surface, color, rect, width)
Copy after login

The parameter description is as follows:

  • surface: refers to the main game window. Unless there are special circumstances, it will generally be drawn on the main screen;

  • color: This parameter is used for coloring the graphic;

  • rect: The position and size of the drawn graphic;

  • width: Optional parameter, specifies the width of the border, the default is 0, which means filling the rectangular area.

Note that when width > 0, it indicates the width of the wireframe; when width < 0, no graphics will be drawn at this time.

Draw a polygon

pygame.draw.polygon(surface, color, points, width)
Copy after login

The parameters are described as follows

  • points: A list parameter that represents the vertices that make up the polygon 3 or more (x,y) coordinates, representing these polygon vertices through tuples or lists.

  • The remaining parameters are the same as the above function.

Draw a circle

pygame.circle(surface, color, pos, radius, width=0)
Copy after login

The parameter description is as follows

  • pos: This parameter is used to specify The center position of the circle;

  • radius: used to specify the radius of the circle;

  • The remaining parameters are the same as the above functions.

Drawing an ellipse

pygame.draw.ellipse(surface, color, Rect, width=0)
Copy after login

The process of drawing an ellipse is actually to draw an inscribed ellipse inside the rectangular area (Rect)

Parameters The description is as follows

  • The remaining parameters are the same as the above function.

Drawing arc curve

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Copy after login

The process of drawing an ellipse is actually to draw an inscribed ellipse inside the rectangular area (Rect)
The parameters are explained as follows

  • start_angle : is the starting angle of the arc;

  • stop_angle : is the termination angle;

  • The remaining parameters are the same as the above function.

Draw a straight line

pygame.draw.line(surface, color, start_pos, end_pos, width=1)
Copy after login

The parameter description is as follows

  • start_pos : is the starting position of the line segment ;

  • end_pos : is the end position of the line segment;;

  • The remaining parameters are the same as the above function.

If you are drawing a smooth line to eliminate aliasing, use the blend = 1 parameter at this time, as shown below:

pygame.aaline(surface, color, startpos, endpos, blend=1)
Copy after login

The blend parameter indicates that by drawing the blended background Shadows to implement anti-aliasing.

Draw multiple straight lines

The parameter description is as follows

  • pointlist : The parameter value is a list, including a series of point coordinates List of ;

  • closed : Boolean parameter, if set to True, it means that the first endpoint of the straight line and the last endpoint of the straight line should be connected end to end;;

  • The remaining parameters are the same as the above function.

If you draw an anti-aliased straight line, use the following method:

pygame.draw.aalines(surface, color, closed, pointlist, blend=1)
Copy after login

Except that blend = 1 is specified, the meaning of the remaining parameters is the same as the above function.

The above drawing method is demonstrated through a set of simple examples:

import pygame
from math import pi

# 初始化
pygame.init()
# 设置主屏幕大小
size = (500, 450)
screen = pygame.display.set_mode(size)
# 设置标题
pygame.display.set_caption("Python自学网")
# 设置一个控制主循环的变量
done = False
# 创建时钟对象
clock = pygame.time.Clock()
while not done:
    # 设置游戏的fps
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True  # 若检测到关闭窗口,则将done置为True
    # 绘制一条宽度为 3 的红色对角线
    pygame.draw.line(screen, (0, 255, 0), [0, 0], (500, 450), 3)
    # 绘制多条蓝色的直线(连续直线,非抗锯齿),False 表示首尾不相连
    pygame.draw.lines(screen, (0, 0, 255), False, [[0, 80], [50, 90], [200, 80], [220, 30]], 1)
    # 绘制一个灰色的矩形区域,以灰色填充区域
    pygame.draw.rect(screen, (155, 155, 155), (75, 10, 50, 20), 0)
    # 绘制一个线框宽度为2的矩形区域
    pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20], 2)
    # 绘制一个椭圆形,其线宽为2
    pygame.draw.ellipse(screen, (255, 0, 0), (225, 10, 50, 20), 2)
    # 绘制一个实心的红色椭圆形
    pygame.draw.ellipse(screen, (255, 0, 0), (300, 10, 50, 20))
    # 绘制一个绿色边框(宽度为2)三角形
    pygame.draw.polygon(screen, (100, 200, 45), [[100, 100], [0, 200], [200, 200]], 2)
    # 绘制一个蓝色实心的圆形,其中[60,250]表示圆心的位置,40为半径,width默认为0
    pygame.draw.circle(screen, (0, 0, 255), [60, 250], 40)
    # 绘制一个圆弧,其中0表示弧线的开始位置,pi/2表示弧线的结束位置,2表示线宽
    pygame.draw.arc(screen, (255, 10, 0), (210, 75, 150, 125), 0, pi / 2, 2)
    # 刷新显示屏幕
    pygame.display.flip()
# 点击关闭,退出pygame程序
pygame.quit()
Copy after login

The above is the detailed content of How to use Pygame's Draw drawing method in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles