A collision mask for pixel-perfect collision detection.
Mask() → MaskMask() → Mask
Mask(size: Vec2, filled: bool = False) → MaskMask(size: Vec2, filled: bool = False) → Mask
Mask(pixel_array: PixelArray, threshold: int = 1) → MaskMask(pixel_array: PixelArray, threshold: int = 1) → Mask
A collision mask for pixel-perfect collision detection.
| Name | Description | Type |
|---|---|---|
height | The height of the mask in pixels. | int |
size | The size of the mask as a Vec2. | Vec2 |
width | The width of the mask in pixels. | int |
add(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → Noneadd(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → NoneAdd another mask to this mask with an offset.
Performs a bitwise OR operation between the masks.
Args
other : The mask to add.offset : Position offset for the other mask. Defaults to (0, 0).clear() → Noneclear() → NoneClear the entire mask, setting all pixels to transparent.
collide_mask(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → boolcollide_mask(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → boolCheck collision between this mask and another mask with an offset.
Args
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.
copy() → Maskcopy() → MaskCreate a copy of this mask.
Returns
Mask : A new Mask with the same dimensions and pixel data.
fill() → Nonefill() → NoneFill the entire mask with solid pixels.
get_at(x: int, y: int) → boolget_at(x: int, y: int) → boolGet the pixel value at a specific position.
Args
x : The x-coordinate of the position to check.y : The y-coordinate of the position to check.Returns
bool : True if the pixel is solid (above threshold), False otherwise.
get_bounding_rect() → Rectget_bounding_rect() → RectGet the bounding rectangle that contains all solid pixels.
Returns
Rect : The smallest rectangle containing all solid pixels.
Returns empty rect if mask has no solid pixels.
get_center_of_mass() → Vec2get_center_of_mass() → Vec2Calculate the center of mass of all solid pixels.
Returns
Vec2 : The center of mass position. Returns (0, 0) if mask is empty.
get_collision_points(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → list[Vec2]get_collision_points(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → list[Vec2]Get all points where this mask collides with another mask.
Args
other : The other mask to test collision with.offset : Position offset between the masks. Defaults to (0, 0).Returns
list[Vec2] : A list of collision points.
get_count() → intget_count() → intGet the number of solid pixels in the mask.
Returns
int : The count of solid pixels.
get_outline() → list[Vec2]get_outline() → list[Vec2]Get the outline points of the mask.
Returns a list of points that form the outline of all solid regions.
Returns
list[Vec2] : A list of outline points.
get_overlap_area(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → intget_overlap_area(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → intGet the number of overlapping pixels between this mask and another.
Args
other : The other mask to check overlap with.offset : Position offset between the masks. Defaults to (0, 0).Returns
int : The number of overlapping solid pixels.
get_overlap_mask(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → Maskget_overlap_mask(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → MaskGet a mask representing the overlapping area between this mask and another.
Args
other : The other mask to check overlap with.offset : Position offset between the masks. Defaults to (0, 0).Returns
Mask : A new mask containing only the overlapping pixels.
get_pixel_array(color: Color = Color(255, 255, 255, 255)) → PixelArrayget_pixel_array(color: Color = Color(255, 255, 255, 255)) → PixelArrayConvert the mask to a pixel array with the specified color.
Solid pixels become the specified color, transparent pixels become transparent.
Args
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.
Raises
RuntimeError : If pixel array creation fails.invert() → Noneinvert() → NoneInvert all pixels in the mask.
Solid pixels become transparent and transparent pixels become solid.
is_empty() → boolis_empty() → boolCheck if the mask contains no solid pixels.
Returns
bool : True if the mask is empty, False otherwise.
set_at(x: int, y: int, value: bool) → Noneset_at(x: int, y: int, value: bool) → NoneSet the pixel value at a specific position.
Args
x : The x-coordinate of the position to set.y : The y-coordinate of the position to set.value : The pixel value (True for solid, False for transparent).subtract(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → Nonesubtract(other: Mask, offset: Vec2 = Vec2(0.0, 0.0)) → NoneSubtract another mask from this mask with an offset.
Removes pixels where the other mask has solid pixels.
Args
other : The mask to subtract.offset : Position offset for the other mask. Defaults to (0, 0).