【pygame】draw模块

用于绘制简单图形的模块

pygame.draw.rect() 在给定的Surface上绘制矩形。

属性:

rect(surface, color, rect) -> Rect
rect(surface, color, rect, width=0, border_radius=0, border_radius=-1, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1) -> Rect
pygame.Surface.fill() 方法同样适用于绘制填充矩形,并且可以在某些平台上通过软件和硬件显示模式进行硬件加速。

参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
rect (Rect) – 要绘制的矩形【位置和尺寸】
width (int) – (可选)用于指示线条粗细或要填充矩形(不要与 rect 参数的 width 值混淆)
若 width == 0, (默认) 填充矩形

若 width > 0, 用于指示线条粗细

若 width < 0, 不会绘制任何内容

注意:

当使用 width > 1时,边缘线将增长到矩形的原始边界之外。详细信息参阅pygame.draw.line()函数的 width 参数笔记。
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)一个包围改变像素的矩形。如果没有画出任何东西,则包围矩形的位置将是给定 rect 参数的位置,其宽度和高度将为0

Changed in pygame 2.0.0:添加了对关键字参数的支持。

Changed in pygame 2.0.0.dev8:添加了对边界半径的支持。

pygame.draw.polygon() 在给定的曲面上绘制多边形。

polygon(surface, color, points) -> Rect
polygon(surface, color, points, width=0) -> Rect
参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
points (tuple(coordinate) or list(coordinate)) – 构成多边形顶点的3个或更多(x, y)坐标序列,序列中的每个坐标必须是tuple/list/pygame.math.Vector2 格式的2个整数/浮点数,例如: [(x1, y1), (x2, y2), (x3, y3)]
width (int) – (可选)用于指示线条粗细或要填充矩形(不要与 rect 参数的 width 值混淆)
若 width == 0, (默认) 填充多边形

若 width > 0, 用于指示线条粗细

若 width < 0, 不会绘制任何内容

注意:

当使用 width > 1时,边缘线将增长到多边形的原始边界之外。详细信息参阅pygame.draw.line()函数的 width 参数笔记。
返回值:(Rect)一个包围更改像素的矩形,如果没有绘制任何内容,则包围矩形的位置将是 points 参数中第一个点的位置(浮点值将被截断),其宽度和高度将为0

提示:

ValueError --若 len(points) < 3 (必须至少有3个点)
TypeError – 若 points 不是序列 or points 不包含数字对
注意:对于aa多边形,使用 aalines()【closed=True】。

Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.circle() 在给定的Surface上绘制圆。

circle(surface, color, center, radius) -> Rect
circle(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) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时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个int/float的序列表示的圆的中心点,例如(x, y)
radius (int or float) – 从 center 参数测量的圆的半径,如果 radius 小于1,则不绘制任何内容
width (int) – (可选)用于指示线条粗细或要填充矩形(不要与 rect 参数的 width 值混淆)
若 width == 0, (默认) 填充圆形
若 width > 0, 用于指示线条粗细
若 width < 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 值的所有圆部分,否则将绘制整个圆。

返回值:(Rect)a rect bounding the changed pixels, if nothing is drawn the bounding rect’s position will be the center parameter value (float values will be truncated) and its width and height will be 0

提示:

TypeError – 若 center 不是一个序列或两个数字
TypeError – 若 radius 不是一个数字
Changed in pygame 2.0.0.dev8:添加了对绘制圆形的象限的支持。Changed in pygame 2.0.0:添加了对关键字参数的支持。半径为0时不绘制任何内容(半径等于0时,在 center 坐标处绘制的一个像素点)。 center 参数接受浮点和Vector2 。对绘图算法进行了改进,使其看起来更像一个圆。

pygame.draw.ellipse() 在给定的曲面上绘制椭圆。

ellipse(surface, color, rect) -> Rect
ellipse(surface, color, rect, width=0) -> Rect
参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
rect (Rect) – 矩形表示椭圆的位置和尺寸,椭圆将在矩形内居中并以其为边界
width (int) – (可选)用于指示线条粗细或要填充矩形(不要与 rect 参数的 width 值混淆)
若 width == 0, (默认) 填充椭圆形
若 width > 0, 用于指示线条粗细
若 width < 0, 不会绘制任何内容
注意:

当使用 width > 1时,边缘线将仅从 rect 参数的原始边界向内增长。

返回值:(Rect)一个包围改变像素的矩形。如果没有画出任何东西,则包围矩形的位置将是给定 rect 参数的位置,其宽度和高度将为0

Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.arc() 在给定的曲面上绘制椭圆弧。

arc(surface, color, rect, start_angle, stop_angle) -> Rect
arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
两个角度参数以弧度表示,并指示弧的开始和停止位置。从 start_angle 到 stop_angle,弧是逆时针方向绘制的。

参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
rect (Rect) – 矩形表示椭圆的位置和尺寸,该椭圆将以矩形为中心
start_angle (float) – 弧的起始角(弧度表示)
stop_angle (float) --弧的终止角(弧度表示)
若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 参数的 width 值混淆)
若 width == 0, 不会绘制任何内容
若 width > 0, 用于指示线条粗细
若 width < 0, 不会绘制任何内容

注意:

当使用 width > 1时,边缘线将仅从 rect 参数的原始边界向内增长。

返回值:(Rect)一个包围改变像素的矩形。如果没有画出任何东西,则包围矩形的位置将是给定 rect 参数的位置,其宽度和高度将为0

Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.line() 画一条直线

line(surface, color, start_pos, end_pos, width) -> Rect
line(surface, color, start_pos, end_pos, width=1) -> Rect
在给定的Surface上画一条直线。没有封尾。粗线条的末端是方形的。

参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时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) – (可选)用于指示线条粗细
若 width >= 1, 则用于线条粗细 (默认值是1)
若 width < 1, 不绘制任何内容

注意:当使用 width > 1时,线条将按如下方式增长:

对于奇数 width 值,每一条线的宽度度随着原始行位于中心而增大。

对于偶数 width 值,每条线的宽度随着原始线从中心偏移而增大(因为没有绘制精确的中心线)。因此,斜率<1(horizontal-ish)的线将比原始线(y方向)多1个像素厚。斜率>=1(vertical-ish)的线在原始线的右侧(x方向)将多出1个厚度像素。

返回值:(Rect)一个包围更改像素的矩形,如果没有绘制任何内容,则包围矩形的位置将是 start_pos 参数值(浮点值将被截断),其宽度和高度将为0

提示:TypeError – 如果 start_pos or end_pos 不是两个数字的序列

Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.lines() 绘制多个连续直线段

lines(surface, color, closed, points) -> Rect
lines(surface, color, closed, points, width=1) -> Rect
在给定的曲面上绘制连续的直线序列。没有封尾端或斜角连接。粗线条的末端是方形的。用粗线条画锐角可能会产生不期望的效果。

参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
closed (bool) – 若为True ,则在 points 序列的第一个点和最后一个点之间绘制一条附加线段
points (tuple(coordinate) or list(coordinate)) – 一个含有2个或更多 (x, y) 坐标的序列,序列中的每个坐标必须是2个整数/浮点数格式的tuple/list/pygame.math.Vector2,同时相邻坐标将通过线段连接。【例如,对于点[(x1, y1), (x2, y2), (x3, y3)]线段将从(x1, y1)绘制到(x2, y2)并从(x2, y2) 绘制到(x3, y3),此外,如果闭合参数为真,则另一线段将从(x3, y3)绘制到(x1, y1)】
width (int) – (可选)用于指示线条粗细
若 width >= 1, 则用于线条粗细 (默认值是1)

若 width < 1, 不绘制任何内容
注意:

当使用width > 1时,请参阅line()的宽度注释,以了解粗线如何增长的详细信息。

返回值:(Rect)一个包围更改像素的矩形,如果没有绘制任何内容,则包围矩形的位置 points 参数中第一个点的位置(浮点值将被截断),其宽度和高度将为0

提示:

ValueError – 若 len(points) < 2 (必须至少有2个点)
TypeError – 若 points 不是序列或 points 不包含数字对
Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.aaline() 在给定Surface上绘制一条抗锯齿直线。

aaline(surface, color, start_pos, end_pos) -> Rect
aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
rect (Rect) – 矩形表示椭圆的位置和尺寸,该椭圆将以矩形为中心
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) – (可选)如果非零(默认值),线条将与Surface的现有像素阴影混合,否则将覆盖它们
返回值:(Rect)一个包围更改像素的矩形,如果没有绘制任何内容,则包围矩形的位置将是 start_pos 参数值(浮点值将被截断),其宽度和高度将为0

提示:TypeError – 如果 start_pos or end_pos 不是两个数字的序列

Changed in pygame 2.0.0:添加了对关键字参数的支持。

pygame.draw.aalines() 在给定的Surface上绘制连续的抗锯齿直线序列。

aalines(surface, color, closed, points) -> Rect
aalines(surface, color, closed, points, blend=1) -> Rect
参数:

surface (Surface) – 要绘制的surface
color (Color or int or tuple(int, int, int, [int])) – 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A])
closed (bool) – 若为True ,则在 points 序列的第一个点和最后一个点之间绘制一条附加线段
points (tuple(coordinate) or list(coordinate)) – 一个含有2个或更多 (x, y) 坐标的序列,序列中的每个坐标必须是2个整数/浮点数格式的tuple/list/pygame.math.Vector2,同时相邻坐标将通过线段连接。【例如,对于点[(x1, y1), (x2, y2), (x3, y3)]线段将从(x1, y1)绘制到(x2, y2)并从(x2, y2) 绘制到(x3, y3),此外,如果闭合参数为真,则另一线段将从(x3, y3)绘制到(x1, y1)】
blend (int) – (可选)如果非零(默认值),线条将与Surface的现有像素阴影混合,否则将覆盖它们
返回值:(Rect)一个包围更改像素的矩形,如果没有绘制任何内容,则包围矩形的位置 points 参数中第一个点的位置(浮点值将被截断),其宽度和高度将为0

提示:

ValueError – 若 len(points) < 2 (必须至少有2个点)
TypeError – 若 points 不是序列或 points 不包含数字对
Changed in pygame 2.0.0:添加了对关键字参数的支持。

猜你喜欢

转载自blog.csdn.net/apythonlearner/article/details/130749632