-
pygame.draw
- pygame module for drawing shapes
— draw a rectangle — draw a polygon — draw a circle — draw an ellipse — draw an elliptical arc — draw a straight line — draw multiple contiguous straight line segments — draw a straight antialiased line — draw multiple contiguous straight antialiased line segments 在一个表面上画几个简单的形状。这些函数将用于渲染到任何格式的表面。
大多数函数使用宽度参数来表示形状边缘周围的笔划(粗细)的大小。如果传递的宽度为0,则形状将被填充(实心)。
所有绘图函数都与曲面的剪裁区域相关,并将被约束到该区域。这些函数返回一个矩形,表示更改的像素的边界区域。此边界矩形是包围受影响区域的“最小”边界框。
所有绘图函数都接受可以是以下格式之一的颜色参数:
一个
(RGB)
三元组(元组/列表)一个
(RGBA)
四元组(元组/列表)已映射到表面像素格式的整数值(请参见
pygame.Surface.map_rgb()
convert a color into a mapped color value 和pygame.Surface.unmap_rgb()
convert a mapped integer color value into a Color )
颜色的Alpha值将直接写入曲面(如果曲面包含像素Alpha),但绘制函数不会透明绘制。
这些功能会暂时锁定它们正在操作的曲面。通过锁定和解锁绘制调用周围的表面对象,可以加快许多顺序绘制调用的速度(请参见
pygame.Surface.lock()
lock the Surface memory for pixel access 和pygame.Surface.unlock()
unlock the Surface memory from pixel access )。备注
请参阅
pygame.gfxdraw
pygame module for drawing shapes 用于替代绘制方法的模块。- pygame.draw.rect()¶
- draw a rectanglerect(surface, color, rect) -> Rectrect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1) -> Rect
在给定曲面上绘制一个矩形。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
rect (Rect) -- 要绘制的矩形、位置和尺寸
width (int) -- (可选)用于线条粗细或指示要填充矩形(不要与
rect
参数)|IFwidth == 0
,(默认)填充矩形|Ifwidth > 0
,用于线条粗细|IFwidth < 0
,将不会抽出任何东西|..VersionChanged::2.1.1带宽度的绘制矩形现在可以在矩形区域内正确绘制宽度,而不是使用内部调用Draw.Line(),后者的宽度有一半溢出到矩形区域之外。border_radius (int) -- (可选)用于绘制圆角矩形。支持的范围为 [0, min(height, width) / 2] ,0表示没有圆角的矩形。
border_top_left_radius (int) -- (可选)用于设置左上边框的值。如果不设置此值,它将使用BORDER_RADIUS值。
border_top_right_radius (int) -- (可选)用于设置右上边框的值。如果不设置此值,它将使用BORDER_RADIUS值。
border_bottom_left_radius (int) -- (可选)用于设置左下边框的值。如果不设置此值,它将使用BORDER_RADIUS值。
border_bottom_right_radius (int) -- (可选)用于设置右下边框的值。如果不设置此值,它将使用BORDER_RADIUS值。|如果
border_radius < 1
它将绘制不带圆角的矩形|如果任何边框半径具有< 0
它将使用BORDER_RADIUS的值|如果矩形同一侧的半径总和大于矩形大小,则将对半径|进行缩放
- 返回
矩形边框,如果未绘制任何内容,则边框矩形的位置将是给定的
rect
参数,其宽度和高度将为0- 返回类型
备注
这个
pygame.Surface.fill()
fill Surface with a solid color 方法同样适用于绘制填充矩形,并且可以在某些平台上进行硬件加速。Changed in pygame 2.0.0: 添加了对关键字参数的支持。
Changed in pygame 2.0.0.dev8: 添加了对边界半径的支持。
- pygame.draw.polygon()¶
- draw a polygonpolygon(surface, color, points) -> Rectpolygon(surface, color, points, width=0) -> Rect
在给定曲面上绘制一个多边形。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
points (tuple(coordinate) or list(coordinate)) -- 组成多边形顶点的3个或更多(x,y)坐标的序列,每个顶点 坐标 序列中必须是一个元组/列表/:类:pygame.math.Vector2,共2个整型/浮点型,例如
[(x1, y1), (x2, y2), (x3, y3)]
width (int) -- (可选)用于线条粗细或指示要填充多边形|如果Width==0,(默认)填充该多边形|如果Width>0,用于线条粗细|如果Width<0,则不绘制任何内容|.注::使用时
width
值> 1
,则边线将在多边形的原始边界之外增长。有关边线的厚度如何增长的更多详细信息,请参阅width
《纽约时报》的pygame.draw.line()
draw a straight line 功能。
- 返回
边界更改的像素的矩形,如果未绘制任何内容,则边界矩形的位置将是
points
参数(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
ValueError -- 如果
len(points) < 3
(必须至少有3分)TypeError -- 如果
points
不是一个序列或points
不包含数字对
备注
对于aapolgon,请使用
aalines()
使用closed=True
。Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.circle()¶
- draw a circlecircle(surface, color, center, radius) -> Rectcircle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None) -> Rect
在给定曲面上绘制圆。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
center (tuple(int or float, int or float) or list(int or float, int or float) or Vector2(int or float, int or float)) -- 圆的中心点为2个整型/浮点型的序列,例如
(x, y)
radius (int or float) -- 圆的半径,从
center
参数,则不会绘制任何内容,如果radius
小于1width (int) -- (可选)用于线条粗细或指示要填充圆|IF
width == 0
,(默认)填充圆圈|Ifwidth > 0
,用于线条粗细|IFwidth < 0
,将不会抽出任何东西|..注::使用时width
值> 1
,边线只会向内增长。draw_top_right (bool) -- (可选)如果设置为True,则将绘制圆的右上角
draw_top_left (bool) -- (可选)如果设置为True,则将绘制圆的左上角
draw_bottom_left (bool) -- (可选)如果设置为True,则将绘制圆的左下角
draw_bottom_right (bool) -- (可选)如果设置为True,则将绘制圆的右下角|如果Draw_Circle_Part中的任何一个为True,则将绘制具有True|值的所有圆部分,否则将绘制整个圆。
- 返回
一个矩形边框,如果未绘制任何内容,则边框矩形的位置将为
center
参数值(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
TypeError -- 如果
center
不是由两个数字组成的序列TypeError -- 如果
radius
不是一个数字
Changed in pygame 2.0.0: 添加了对关键字参数的支持。当半径为0时不绘制任何内容(
center
坐标通常在半径等于0时绘制)。浮点数和向量2被接受为center
帕拉姆。绘制算法进行了改进,使其看起来更像一个圆。Changed in pygame 2.0.0.dev8: 添加了对绘制圆形象限的支持。
- pygame.draw.ellipse()¶
- draw an ellipseellipse(surface, color, rect) -> Rectellipse(surface, color, rect, width=0) -> Rect
在给定曲面上绘制椭圆。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
rect (Rect) -- 矩形以指示椭圆的位置和尺寸,椭圆将在矩形内居中并以其为边界
width (int) -- (可选)用于线条粗细或指示要填充椭圆(不要与
rect
参数)|IFwidth == 0
,(默认)填充省略号|Ifwidth > 0
,用于线条粗细|IFwidth < 0
,将不会抽出任何东西|..注::使用时width
值> 1
的原始边界向内生长,则边线只会从rect
参数。
- 返回
矩形边框,如果未绘制任何内容,则边框矩形的位置将是给定的
rect
参数,其宽度和高度将为0- 返回类型
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.arc()¶
- draw an elliptical arcarc(surface, color, rect, start_angle, stop_angle) -> Rectarc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
在给定曲面上绘制一条椭圆弧。
这两个角度参数以弧度表示,表示圆弧的开始和结束位置。圆弧是以逆时针方向从
start_angle
发送到stop_angle
。- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
rect (Rect) -- 矩形以指示圆弧将基于的椭圆的位置和尺寸,椭圆将在矩形内居中
start_angle (float) -- 以弧度为单位的圆弧起点角度
stop_angle (float) -- 以弧度为单位的圆弧停止角|IF
start_angle < stop_angle
,则以逆时针方向从start_angle
发送到stop_angle
|如果start_angle > stop_angle
,tau(tau==2*pi)将被添加到stop_angle
如果生成的停止角值大于start_angle
以上内容start_angle < stop_angle
大小写适用,否则将不会绘制任何内容|如果start_angle == stop_angle
,不会抽出任何东西width (int) -- (可选)用于线条粗细(不要与
rect
参数)|IFwidth == 0
,将不会绘制任何内容|如果width > 0
,(默认为1)用于线条粗细|IFwidth < 0
,与width == 0
。。注::使用时width
值> 1
的原始边界向内生长,则边线只会从rect
参数。
- 返回
矩形边框,如果未绘制任何内容,则边框矩形的位置将是给定的
rect
参数,其宽度和高度将为0- 返回类型
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.line()¶
- draw a straight lineline(surface, color, start_pos, end_pos) -> Rectline(surface, color, start_pos, end_pos, width=1) -> Rect
在给定曲面上绘制一条直线。没有封口。对于粗线,两端是平方的。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
start_pos (tuple(int or float, int or float) or list(int or float, int or float) or Vector2(int or float, int or float)) -- 直线的起始位置,(x,y)
end_pos (tuple(int or float, int or float) or list(int or float, int or float) or Vector2(int or float, int or float)) -- 行的结束位置,(x,y)
width (int) -- (可选)用于线条粗细|如果宽度>=1,则用于线条粗细(默认为1)|如果宽度小于1,则不绘制任何内容|.注::使用时
width
值> 1
,线路将按如下方式增长。对于奇怪的人width
值时,每条线的粗细都会随着原始线条居中而增大。即使是这样width
值时,每条线的粗细随着原始线距中心的偏移而增加(因为没有绘制精确的中心线)。因此,斜率<1(水平方向)的线将比原始线(沿y方向)多1个像素的厚度。斜率大于等于1(垂直方向)的线在原始线的右侧(在x方向上)的厚度将增加1个像素。
- 返回
一个矩形边框,如果未绘制任何内容,则边框矩形的位置将为
start_pos
参数值(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
TypeError -- 如果
start_pos
或end_pos
不是由两个数字组成的序列
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.lines()¶
- draw multiple contiguous straight line segmentslines(surface, color, closed, points) -> Rectlines(surface, color, closed, points, width=1) -> Rect
在给定曲面上绘制一系列连续的直线。没有收头或斜接。对于粗线,两端是平方的。绘制带有锐角的粗线可能会产生不希望看到的效果。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
closed (bool) -- 如果
True
中的第一个点和最后一个点之间绘制一条附加的线段points
序列points (tuple(coordinate) or list(coordinate)) -- 2个或更多(x,y)坐标的序列,其中每个 坐标 序列中必须是元组/列表/:类:pygame.math.Vector2,其中包含2个整型/浮点型,相邻的坐标将由线段连接,例如用于点
[(x1, y1), (x2, y2), (x3, y3)]
将从以下位置绘制线段(x1, y1)
至(x2, y2)
以及来自(x2, y2)
至(x3, y3)
此外,如果closed
参数为True
将从以下位置绘制另一个线段(x3, y3)
至(x1, y1)
width (int) -- (可选)用于线条粗细|如果宽度>=1,则用于线条粗细(默认为1)|如果宽度小于1,则不绘制任何内容|.注::使用时
width
值> 1
请参阅width
备注:line()
有关粗线如何增长的详细信息,请参阅。
- 返回
边界更改的像素的矩形,如果未绘制任何内容,则边界矩形的位置将是
points
参数(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
ValueError -- 如果
len(points) < 2
(必须至少得2分)TypeError -- 如果
points
不是一个序列或points
不包含数字对
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.aaline()¶
- draw a straight antialiased lineaaline(surface, color, start_pos, end_pos) -> Rectaaline(surface, color, start_pos, end_pos, blend=1) -> Rect
在给定曲面上绘制一条直的抗锯齿线。
线条的粗细为1个像素,端点的高度和宽度各为1个像素。
- 直线及其端点的绘制方式:
如果两个端点相等,则仅绘制单个像素(四舍五入后浮点为最接近的整数)。
否则,如果直线不陡峭(即,如果沿x轴的长度大于沿y轴的高度):
对于每个端点:
如果
x
端点的x坐标是一个整数,找出它将覆盖哪些像素并绘制它们。否则:
当直线延伸超过端点时,使用整数作为x坐标来计算最近点的位置。
找出哪些像素将被覆盖,以及该点覆盖了多少像素。
如果端点是左侧端点,请将Coverage乘以(1-的小数部分
x
)。否则,将覆盖率乘以小数部分
x
。然后画出这些像素。
- 例如:
- 直线的左端点
((1, 1.3), (5, 3))
会覆盖70%的像素(1, 1)
和30%的像素(1, 2)
而右边的将覆盖100%的像素(5, 3)
。直线的左端点((1.2, 1.4), (4.6, 3.1))
将覆盖56% (即0.8 70%) * of the pixel(1, 1)
and 24% * (即0.8 30%) 像素数的(1, 2)
而右边的将覆盖42% (即0.6 70%) * of the pixel(5, 3)
and 18% * (即0.6 30%) 像素数的(5, 4)
而右翼
然后,对于x坐标为整数的直线上的端点之间的每个点:
找出哪些像素将被覆盖,以及该点覆盖了多少像素并绘制它们。
- 例如:
- 沿线的点
((1, 1), (4, 2.5))
会是(2, 1.5)
和(3, 2)
将覆盖50%的像素(2, 1)
,50%的像素(2, 2)
以及100%的像素(3, 2)
。沿线的点((1.2, 1.4), (4.6, 3.1))
会是(2, 1.8)
(覆盖20%的像素(2, 1)
和80%的像素(2, 2)
),(3, 2.3)
(覆盖70%的像素(3, 2)
和30%的像素(3, 3)
)和(4, 2.8)
(覆盖20%的像素(2, 1)
和80%的像素(2, 2)
)
否则,对陡线执行与非陡线相同的操作,但沿y轴而不是x轴(使用
y
而不是x
,上而不是左,下而不是右)。
备注
关于坐标的浮点值,具有由两个整数组成的坐标的点被认为正好在所述像素的中心(因此具有1个像素的高度和宽度将完全覆盖它),而具有一个(或两个)具有非零小数部分的坐标的点将部分覆盖两个(或四个如果两个数字都具有小数部分)相邻像素, e.g. 重点是
(1.4, 2)
覆盖60%的像素(1, 2)
和40%的像素(2,2)
。- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
start_pos (tuple(int or float, int or float) or list(int or float, int or float) or Vector2(int or float, int or float)) -- 直线的起始位置,(x,y)
end_pos (tuple(int or float, int or float) or list(int or float, int or float) or Vector2(int or float, int or float)) -- 行的结束位置,(x,y)
blend (int) -- (可选)如果非零(默认),则线将与表面的现有像素阴影混合,否则将覆盖它们
- 返回
一个矩形边框,如果未绘制任何内容,则边框矩形的位置将为
start_pos
参数值(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
TypeError -- 如果
start_pos
或end_pos
不是由两个数字组成的序列
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
- pygame.draw.aalines()¶
- draw multiple contiguous straight antialiased line segmentsaalines(surface, color, closed, points) -> Rectaalines(surface, color, closed, points, blend=1) -> Rect
在给定曲面上绘制一系列连续的直线抗锯齿线。
- 参数
surface (Surface) -- 要在其上绘制的曲面
color (Color or int or tuple(int, int, int, [int])) -- 绘制所用的颜色,如果使用元组,则alpha值是可选的
(RGB[A])
closed (bool) -- 如果
True
中的第一个点和最后一个点之间绘制一条附加的线段points
序列points (tuple(coordinate) or list(coordinate)) -- 2个或更多(x,y)坐标的序列,其中每个 坐标 序列中必须是元组/列表/:类:pygame.math.Vector2,其中包含2个整型/浮点型,相邻的坐标将由线段连接,例如用于点
[(x1, y1), (x2, y2), (x3, y3)]
将从以下位置绘制线段(x1, y1)
至(x2, y2)
以及来自(x2, y2)
至(x3, y3)
此外,如果closed
参数为True
将从以下位置绘制另一个线段(x3, y3)
至(x1, y1)
blend (int) -- (可选)如果非零(默认),则每条线将与表面的现有像素阴影混合,否则像素将被覆盖
- 返回
边界更改的像素的矩形,如果未绘制任何内容,则边界矩形的位置将是
points
参数(浮点值将被截断),其宽度和高度将为0- 返回类型
- 引发
ValueError -- 如果
len(points) < 2
(必须至少得2分)TypeError -- 如果
points
不是一个序列或points
不包含数字对
Changed in pygame 2.0.0: 添加了对关键字参数的支持。
绘制模块的示例代码。¶
import pygame from math import pi # Initialize pygame pygame.init() # Set the height and width of the screen size = [400, 300] screen = pygame.display.set_mode(size) pygame.display.set_caption("Example code for the draw module") # Loop until the user clicks the close button. done = False clock = pygame.time.Clock() while not done: # This limits the while loop to a max of 60 times per second. # Leave this out and we will use all CPU we can. clock.tick(60) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop # Clear the screen and set the screen background screen.fill("white") # Draw on the screen a green line from (0, 0) to (50, 30) # 5 pixels wide. Uses (r, g, b) color - medium sea green. pygame.draw.line(screen, (60, 179, 113), [0, 0], [50, 30], 5) # Draw on the screen a green line from (0, 50) to (50, 80) # Because it is an antialiased line, it is 1 pixel wide. # Uses (r, g, b) color - medium sea green. pygame.draw.aaline(screen, (60, 179, 113), [0, 50], [50, 80], True) # Draw on the screen 3 black lines, each 5 pixels wide. # The 'False' means the first and last points are not connected. pygame.draw.lines( screen, "black", False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5 ) # Draw a rectangle outline pygame.draw.rect(screen, "black", [75, 10, 50, 20], 2) # Draw a solid rectangle. Same color as "black" above, specified in a new way pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20]) # Draw a rectangle with rounded corners pygame.draw.rect(screen, "green", [115, 210, 70, 40], 10, border_radius=15) pygame.draw.rect( screen, "red", [135, 260, 50, 30], 0, border_radius=10, border_top_left_radius=0, border_bottom_right_radius=15, ) # Draw an ellipse outline, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, "red", [225, 10, 50, 20], 2) # Draw an solid ellipse, using a rectangle as the outside boundaries pygame.draw.ellipse(screen, "red", [300, 10, 50, 20]) # This draws a triangle using the polygon command pygame.draw.polygon(screen, "black", [[100, 100], [0, 200], [200, 200]], 5) # Draw an arc as part of an ellipse. # Use radians to determine what angle to draw. pygame.draw.arc(screen, "black", [210, 75, 150, 125], 0, pi / 2, 2) pygame.draw.arc(screen, "green", [210, 75, 150, 125], pi / 2, pi, 2) pygame.draw.arc(screen, "blue", [210, 75, 150, 125], pi, 3 * pi / 2, 2) pygame.draw.arc(screen, "red", [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2) # Draw a circle pygame.draw.circle(screen, "blue", [60, 250], 40) # Draw only one circle quadrant pygame.draw.circle(screen, "blue", [250, 250], 40, 0, draw_top_right=True) pygame.draw.circle(screen, "red", [250, 250], 40, 30, draw_top_left=True) pygame.draw.circle(screen, "green", [250, 250], 40, 20, draw_bottom_left=True) pygame.draw.circle(screen, "black", [250, 250], 40, 10, draw_bottom_right=True) # Go ahead and update the screen with what we've drawn. # This MUST happen after all the other drawing commands. pygame.display.flip() # Be IDLE friendly pygame.quit()
Edit on GitHub