Home  >  Article  >  Backend Development  >  How to use Pygame's Draw drawing method in Python

How to use Pygame's Draw drawing method in Python

WBOY
WBOYforward
2023-04-19 16:46:062310browse

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)

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)

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)

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)

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)

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)

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)

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)

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()

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:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete