Skip to content

ipygame.mask

pygame-compatible mask module.

def from_surface(surface: Surface, threshold: int = 127) -> "Mask"

Create a Mask from a Surface’s alpha channel.

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.

class Mask()

2D bit mask for pixel-perfect collision detection.

def __init__(size: tuple[int, int], fill: bool = False)

def get_size() -> tuple[int, int]

def get_rect(**kwargs) -> Rect

def get_at(pos) -> int

def set_at(pos, value: int = 1) -> None

def fill() -> None

def clear() -> None

def invert() -> None

def count() -> int

def overlap(other: "Mask", offset: tuple[int, int]) -> tuple[int, int] | None

Return the first overlapping bit position, or None.

def overlap_area(other: "Mask", offset: tuple[int, int]) -> int

Count overlapping set bits.

def overlap_mask(other: "Mask", offset: tuple[int, int]) -> "Mask"

Return a Mask of overlapping bits.

def draw(other: "Mask", offset: tuple[int, int]) -> None

OR another mask onto this mask.

def erase(other: "Mask", offset: tuple[int, int]) -> None

Clear bits where other has bits set.

def centroid() -> tuple[int, int]

Return the centroid of set bits.

def angle() -> float

Return the orientation angle of set bits in degrees.

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.

def connected_component(pos=None) -> "Mask"

Return the connected component containing pos (or the largest).

def connected_components(minimum: int = 0) -> list["Mask"]

Return all connected components.

def get_bounding_rects() -> list[Rect]

Return bounding rects for each connected component.

def convolve(
other: "Mask",
output: "Mask | None" = None,
offset: tuple[int, int] = (0, 0)) -> "Mask"

Convolve this mask with another.

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)
) -> Surface

Render the mask onto a Surface.

def copy() -> "Mask"