Mask
pykraken.Mask A collision mask for pixel-perfect collision detection.
More about Mask
A Mask represents a 2D bitmap, typically used for precise collision detection based on non-transparent pixels.
At a glance
23 membersConstructormethodheightpropertysizepropertywidthpropertyaddmethodclearmethodcollide_maskmethodcopymethodfillmethodget_atmethodget_bounding_rectmethodget_center_of_massmethodget_collision_pointsmethodget_countmethodget_outlinemethodget_overlap_areamethodget_overlap_maskmethodget_pixel_arraymethodget_rectmethodinvertmethodis_emptymethodset_atmethodsubtractmethodReference
PYTHON APIOverloaded function.
Mask()
Create an empty mask with size (0, 0).
Mask(size: Vec2, filled: bool = False)
Create a mask with specified size.
Args
- size (
Vec2) — The size of the mask as (width, height). - filled (
bool) — Whether to fill the mask with solid pixels. Defaults to False.
Mask(pixel_array: PixelArray, threshold: int = 1)
Create a mask from a pixel array based on alpha threshold.
Args
- pixel_array (
PixelArray) — The source pixel array to create the mask from. - threshold (
int) — Alpha threshold value (0-255). Pixels with alpha >= threshold are solid.
Raises
- RuntimeError — If the pixel array is invalid.
height
propertyheight: intThe height of the mask in pixels.
size
propertysize: Vec2The size of the mask as a Vec2.
width
propertywidth: intThe width of the mask in pixels.
add
methodAdd another mask to this mask with an offset.
Performs a bitwise OR operation between the masks.
Args
- other (
Mask) — The mask to add. - offset (
Vec2) — Position offset for the other mask. Defaults to (0, 0).
clear
methodclear(self) -> NoneClear the entire mask, setting all pixels to transparent.
collide_mask
methodCheck collision between this mask and another mask with an offset.
Args
- other (
Mask) — The other mask to test collision with. - offset (
Vec2) — Position offset between the masks. Defaults to (0, 0).
Returns
- bool — True if the masks collide, False otherwise.
copy
methodcopy(self) -> MaskCreate a copy of this mask.
Returns
- Mask — A new Mask with the same dimensions and pixel data.
fill
methodfill(self) -> NoneFill the entire mask with solid pixels.
get_at
methodget_at(self, x: int, y: int) -> boolGet the pixel value at a specific position.
Args
- x (
int) — The x-coordinate of the position to check. - y (
int) — The y-coordinate of the position to check.
Returns
- bool — True if the pixel is solid (above threshold), False otherwise.
get_bounding_rect
methodget_bounding_rect(self) -> 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
methodget_center_of_mass(self) -> 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
methodGet all points where this mask collides with another mask.
Args
- other (
Mask) — The other mask to test collision with. - offset (
Vec2) — Position offset between the masks. Defaults to (0, 0).
Returns
list[Vec2]: A list of collision points.
get_count
methodget_count(self) -> intGet the number of solid pixels in the mask.
Returns
- int — The count of solid pixels.
get_outline
methodget_outline(self) -> 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
methodGet the number of overlapping pixels between this mask and another.
Args
- other (
Mask) — The other mask to check overlap with. - offset (
Vec2) — Position offset between the masks. Defaults to (0, 0).
Returns
- int — The number of overlapping solid pixels.
get_overlap_mask
methodGet a mask representing the overlapping area between this mask and another.
Args
- other (
Mask) — The other mask to check overlap with. - offset (
Vec2) — Position offset between the masks. Defaults to (0, 0).
Returns
- Mask — A new mask containing only the overlapping pixels.
get_pixel_array
methodget_pixel_array(self, 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 (
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.
get_rect
methodget_rect(self) -> RectGet the bounding rectangle of the mask starting at (0, 0).
invert
methodinvert(self) -> NoneInvert all pixels in the mask.
Solid pixels become transparent and transparent pixels become solid.
is_empty
methodis_empty(self) -> boolCheck if the mask contains no solid pixels.
Returns
- bool — True if the mask is empty, False otherwise.
set_at
methodset_at(self, x: int, y: int, value: bool) -> NoneSet the pixel value at a specific position.
Args
- x (
int) — The x-coordinate of the position to set. - y (
int) — The y-coordinate of the position to set. - value (
bool) — The pixel value (True for solid, False for transparent).
subtract
methodSubtract another mask from this mask with an offset.
Removes pixels where the other mask has solid pixels.
Args
- other (
Mask) — The mask to subtract. - offset (
Vec2) — Position offset for the other mask. Defaults to (0, 0).