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


NameDescriptionType
widthThe width of the maskint
heightThe height of the maskint
sizeThe size of the maskVec2

Methods


Clear

clear() None

Clear the mask, setting all values to False.

Fill

fill() None

Fill the mask, setting all values to True.

Add

add(other: Mask, offset: Vec2 = None) None

Add another mask to this mask.

Parameters

  • other : The other mask to add.

Subtract

subtract(other: Mask, offset: Vec2 = None) None

Subtract another mask from this mask.

Parameters

  • other : The other mask to subtract.

Invert

invert() None

Invert the mask, flipping all values.

Collide Mask

collide_mask(other: Mask, offset: Vec2 = None) bool

Check 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() bool

Check if the mask is empty (all values are False).

Returns

bool : True if the mask is empty, False otherwise.

Get Rect

get_rect() Rect

Get 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) None

Set 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) bool

Get 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() int

Get the count of True values in the mask.

Returns

int : The number of True values in the mask.

Get Bounding Rect

get_bounding_rect() Rect

Get the bounding rectangle of the mask.

Returns

Rect : The bounding rectangle of the mask.

Get Center of Mass

get_center_of_mass() Vec2

Get 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) int

Get 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) Mask

Get the overlap mask between this mask and another mask.

Returns

Mask : The overlap mask.

Get Pixel Array

get_pixel_array(color: Color = None) PixelArray

Convert 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() Mask

Create a copy of the mask.

Returns

Mask : The copied mask.