ipygame.mask
pygame-compatible mask module.
from_surface
Section titled “from_surface”def from_surface(surface: Surface, threshold: int = 127) -> "Mask"Create a Mask from a Surface’s alpha channel.
from_threshold
Section titled “from_threshold”def from_threshold(surface: Surface, color, threshold=(0, 0, 0, 255), other_surface: Surface | None = None, palette_colors: int = 1) -> "Mask"Create a Mask from a Surface using a color threshold.
Mask Objects
Section titled “Mask Objects”class Mask()2D bit mask for pixel-perfect collision detection.
__init__
Section titled “__init__”def __init__(size: tuple[int, int], fill: bool = False)get_size
Section titled “get_size”def get_size() -> tuple[int, int]get_rect
Section titled “get_rect”def get_rect(**kwargs) -> Rectget_at
Section titled “get_at”def get_at(pos) -> intset_at
Section titled “set_at”def set_at(pos, value: int = 1) -> Nonedef fill() -> Nonedef clear() -> Noneinvert
Section titled “invert”def invert() -> Nonedef count() -> intoverlap
Section titled “overlap”def overlap(other: "Mask", offset: tuple[int, int]) -> tuple[int, int] | NoneReturn the first overlapping bit position, or None.
overlap_area
Section titled “overlap_area”def overlap_area(other: "Mask", offset: tuple[int, int]) -> intCount overlapping set bits.
overlap_mask
Section titled “overlap_mask”def overlap_mask(other: "Mask", offset: tuple[int, int]) -> "Mask"Return a Mask of overlapping bits.
def draw(other: "Mask", offset: tuple[int, int]) -> NoneOR another mask onto this mask.
def erase(other: "Mask", offset: tuple[int, int]) -> NoneClear bits where other has bits set.
centroid
Section titled “centroid”def centroid() -> tuple[int, int]Return the centroid of set bits.
def angle() -> floatReturn the orientation angle of set bits in degrees.
outline
Section titled “outline”def outline(every: int = 1) -> list[tuple[int, int]]Return a list of points on the outline of the mask.
def scale(size: tuple[int, int]) -> "Mask"Return a scaled copy of the mask.
connected_component
Section titled “connected_component”def connected_component(pos=None) -> "Mask"Return the connected component containing pos (or the largest).
connected_components
Section titled “connected_components”def connected_components(minimum: int = 0) -> list["Mask"]Return all connected components.
get_bounding_rects
Section titled “get_bounding_rects”def get_bounding_rects() -> list[Rect]Return bounding rects for each connected component.
convolve
Section titled “convolve”def convolve( other: "Mask", output: "Mask | None" = None, offset: tuple[int, int] = (0, 0)) -> "Mask"Convolve this mask with another.
to_surface
Section titled “to_surface”def to_surface( surface: Surface | None = None, setsurface: Surface | None = None, unsetsurface: Surface | None = None, setcolor=(255, 255, 255, 255), unsetcolor=(0, 0, 0, 255), dest=(0, 0)) -> SurfaceRender the mask onto a Surface.
def copy() -> "Mask"