pygame.mask
Pygame 中处理图形遮罩的模块。
pygame.mask.from_surface —— 从指定 Surface 对象中返回一个 Mask
pygame.mask.from_threshold —— 从给定阈值的 Surface 对象中创建一个 Mask
pygame.mask.Mask —— 用于表示 2d 位掩码(遮罩)的 Pygame 对象
用于快速实现完美的碰撞检测,Mask 可以精确到 1 个像素级别的判断。
Pygame 1.8 新增加的。
pygame.mask.from_surface()
↶
从指定 Surface 对象中返回一个 Mask。
from_surface(Surface, threshold = 127) -> Mask
Surface 对象中透明的部分设置为 1,不透明部分设置为 0。
检查每个像素的 alpha 值是否大于 threshold 参数指定的值。(alpha 通道使用 0 ~ 255 描述像素的透明度)
如果 Surface 对象是基于 colorkeys 实现的透明(而不是基于 pixel alphas),则忽略 threshold 参数。
注:关于 Surface 对象的透明实现,可参考:Surface | Pygame中文文档
搜索pygame.mask.from_surface的示例
pygame.mask.from_threshold()
↶
从给定阈值的 Surface 对象中创建一个 Mask。
from_threshold(Surface, color, threshold = (0,0,0,255), othersurface = None, palette_colors = 1) -> Mask
从 Surface 对象中获取 Mask,这个方法更有特色。如果只提供一个 Surface 对象,那么 Surface 对象中所有与 threshold 参数提供颜色匹配的像素均被选入 Mask 中。如果指定了 othersurface 可选参数,那么被选入 Mask 中的像素不仅需要与 threshold 参数提供颜色匹配,还需要包含在 othersurface 指定的 Surface 对象中。
搜索pygame.mask.from_threshold的示例
pygame.mask.Mask
↶
用于表示 2d 位掩码(遮罩)的 Pygame 对象。
Mask((width, height)) -> Mask
pygame.mask.Mask.get_size —— 返回 Mask 的大小
pygame.mask.Mask.get_at —— 如果像素 (x, y) 被设置,返回值是非 0
pygame.mask.Mask.set_at —— 设置 Mask 中给定位置的值
pygame.mask.Mask.overlap —— 返回两个 Mask 在指定偏移处的重叠坐标(如果没有返回 None)
pygame.mask.Mask.overlap_area —— 返回两个 Mask 重叠的像素数量
pygame.mask.Mask.overlap_mask —— 将两个 Mask 重叠的部分创建一个新的 Mask
pygame.mask.Mask.fill —— 将所有的位设置为 1
pygame.mask.Mask.clear —— 将所有的位设置为 0
pygame.mask.Mask.invert —— 翻转 Mask 中所有的位(0 变 1,1 变 0)
pygame.mask.Mask.scale —— 缩放 Mask 的尺寸
pygame.mask.Mask.draw —— 将 Mask 绘制到另一个 Mask 上边
pygame.mask.Mask.erase —— 用另一个 Mask 擦除 Mask
pygame.mask.Mask.count —— 返回 Mask 被设置(为 1)的像素的数量
pygame