Mask
A 2D binary mask for pixel-perfect collision detection and pixel manipulation.
Constructor
-
Mask() -
Mask(size: Vec2, filled: bool = False) -
Mask(pixel_array: PixelArray, threshold: int = 1)
Parameters
size: The size of the mask as (width, height).filled: Whether to fill the mask with solid pixels. Defaults to False.pixel_array: The source pixel array to create the mask from.threshold: Alpha threshold value (0-255). Pixels with alpha >= threshold are solid.
Properties
| Name | Description | Type |
|---|---|---|
width | The width of the mask | int |
height | The height of the mask | int |
size | The size of the mask | Vec2 |
Methods
Clear
clear() → NoneClear the mask, setting all values to False.
Fill
fill() → NoneFill the mask, setting all values to True.
Add
add(other: Mask, offset: Vec2 = None) → NoneAdd another mask to this mask.
Parameters
other: The other mask to add.
Subtract
subtract(other: Mask, offset: Vec2 = None) → NoneSubtract another mask from this mask.
Parameters
other: The other mask to subtract.
Invert
invert() → NoneInvert the mask, flipping all values.
Collide Mask
collide_mask(other: Mask, offset: Vec2 = None) → boolCheck collision between this mask and another mask with an offset.
Parameters
other: The other mask to test collision with.offset: Position offset between the masks. Defaults to (0, 0).
Returns
bool : True if the masks collide, False otherwise.
Is Empty
is_empty() → boolCheck if the mask is empty (all values are False).
Returns
bool : True if the mask is empty, False otherwise.
Get Rect
get_rect() → RectGet a rectangle defining the width and height of the mask.
Returns
Rect : The rectangle defining the mask area.
Set At
set_at(pos: Vec2, value: bool) → NoneSet the value at a specific position in the mask.
Parameters
pos: The position to set the value at.value: The value to set.
Get At
get_at(pos: Vec2) → boolGet the value at a specific position in the mask.
Parameters
pos: The position to get the value from.
Returns
bool : The value at the specified position.
Get Count
get_count() → intGet the count of True values in the mask.
Returns
int : The number of True values in the mask.
Get Bounding Rect
get_bounding_rect() → RectGet the bounding rectangle of the mask.
Returns
Rect : The bounding rectangle of the mask.
Get Center of Mass
get_center_of_mass() → Vec2Get the center of mass of the mask.
Returns
Vec2 : The center of mass of the mask.
Get Collision Points
get_collision_points(other: Mask, offset: Vec2 = None) → list[Vec2]Get the collision points between this mask and another mask.
Returns
list[Vec2] : The list of collision points.
Get Outline
get_outline() → list[Vec2]Get the outline points of the mask.
Returns
list[Vec2] : The list of outline points.
Get Overlap Area
get_overlap_area(other: Mask, offset: Vec2 = None) → intGet the overlap area (number of pixels) between this mask and another mask.
Returns
int : The overlap area.
Get Overlap Mask
get_overlap_mask(other: Mask, offset: Vec2 = None) → MaskGet the overlap mask between this mask and another mask.
Returns
Mask : The overlap mask.
Get Pixel Array
get_pixel_array(color: Color = None) → PixelArrayConvert the mask to a pixel array with the specified color. Solid pixels become the specified color, transparent pixels become transparent.
Parameters
color: The color to use for solid pixels. Defaults to white (255, 255, 255, 255).
Returns
PixelArray : A new pixel array representation of the mask.
Copy
copy() → MaskCreate a copy of the mask.
Returns
Mask : The copied mask.