pygame中draw模块方法详解

目录

pygame.draw.rect()

pygame.draw.polygon()

pygame.draw.circle()

pygame.draw.ellipse()

pygame.draw.arc()

pygame.draw.line()

pygame.draw.lines()

pygame.draw.aaline()

pygame.draw.aalines()

绘图模块的示例代码


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(intintint[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(intintint[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(intintint[int])) -- 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A]
  • center (tuple(int or floatint or float) or list(int or floatint or float) or Vector2(int or floatint 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(intintint[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(intintint[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(intintint[int])) -- 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A]
  • start_pos (tuple(int or floatint or float) or list(int or floatint or float) or Vector2(int or floatint or float)) -- 线的起始位置, (x, y)
  • end_pos (tuple(int or floatint or float) or list(int or floatint or float) or Vector2(int or floatint 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(intintint[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(intintint[int])) -- 要绘制的颜色,使用tuple表示时alpha值是可选的(RGB[A]
  • rect (Rect) -- 矩形表示椭圆的位置和尺寸,该椭圆将以矩形为中心
  • start_pos (tuple(int or floatint or float) or list(int or floatint or float) or Vector2(int or floatint or float)) -- 线的起始位置, (x, y)
  • end_pos (tuple(int or floatint or float) or list(int or floatint or float) or Vector2(int or floatint 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(intintint[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:添加了对关键字参数的支持。

绘图模块的示例代码

# 导入名为'pygame'的函数库 
import pygame
from math import pi
 
# 初始化游戏引擎
pygame.init()
 
# 以RGB格式定义将使用的颜色
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE =  (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =   (255,   0,   0)
 
# 设置屏幕的高度和宽度
size = [400, 300]
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("Example code for the draw module")
 
#L循环,直到用户单击“关闭”按钮。
done = False
clock = pygame.time.Clock()
 
while not done:
 
    # 将while循环限制为每秒最多10次
    # Leave this out and we will use all CPU we can.
    clock.tick(10)
     
    for event in pygame.event.get(): # 捕获用户动作
        if event.type == pygame.QUIT: # 若用户点击“关闭”
            done=True # 标记已完成动作,以便退出此循环
 
    # 所有绘图代码都发生在for循环之后,但是在 while done==False主循环中。
     
    # 清除屏幕并设置屏幕背景
    screen.fill(WHITE)
 
    # 在屏幕上画一条从(0, 0)到(50, 30)的绿线
    # 5像素宽
    pygame.draw.line(screen, GREEN, [0, 0], [50,30], 5)
 
    # 在屏幕上画三条黑线,每一条5像素宽
    # 'False' 表示第一个和最后一个点没有连接
    pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
    
    # 在屏幕上画一条从(0, 50)到(50, 80)的绿线
    # 因为它是一条抗锯齿线,所以宽度为1像素
    pygame.draw.aaline(screen, GREEN, [0, 50],[50, 80], True)

    # 绘制矩形轮廓
    pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
     
    # 绘制实心矩形
    pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])

    # 画一个圆角矩形
    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)

    # 使用矩形作为外边界绘制椭圆轮廓
    pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2) 

    # 使用矩形作为外边界绘制实心椭圆
    pygame.draw.ellipse(screen, RED, [300, 10, 50, 20]) 
 
    # 使用polygon命令绘制三角形
    pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
  
    # 画一条弧线作为椭圆的一部分
    # 使用弧度确定要绘制的角度
    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)
    
    # 画一个圆
    pygame.draw.circle(screen, BLUE, [60, 250], 40)

    # 绘制一个象限圆
    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)

    # 继续用画的东西更新屏幕。
    # MUST在所有其他绘图命令之后发生
    pygame.display.flip()
 
# Be IDLE friendly
pygame.quit()

 

 

 

整理自:https://www.pygame.org/docs/ref/draw.html

  • 5
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值