pygame documentation | || Pygame Home || Help Contents || Reference Index || Cdrom || Color || Cursors || Display || Draw || Event || Font || Image || Joystick || Key || Mask || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Time || Transform

pygame.mask

pygame module for image masks.

pygame.mask.from_surface - Returns a Mask from the given surface. | Returns a Mask from the given surface.
pygame.mask.Mask - pygame object for representing 2d bitmasks | pygame object for representing 2d bitmasks

Useful for fast pixel perfect collision detection. `A` Mask uses 1bit per pixel to store which parts collide.

New in pygame `1.8`.

pygame.mask.from_surface

Returns a Mask from the given surface.
`pygame.mask.from_surface(Surface, threshold = 127) -> Mask`

Makes the transparent parts of the Surface not set, and the opaque parts set.

The alpha of each pixel is checked to see if it is greater than the given threshold.

If the Surface is color keyed, then threshold is not used.

pygame.mask.Mask

pygame object for representing 2d bitmasks
`pygame.Mask((width, height): return Mask`

Mask.get_size - Returns the size of the mask. | Returns the size of the mask.
Mask.get_at - Returns nonzero if the bit at (x,y) is set. | Returns nonzero if the bit at (x,y) is set.
Mask.set_at - Sets the position in the mask given by x and y. | Sets the position in the mask given by x and y.
Mask.overlap - Returns the point of intersection if the masks overlap with the given offset - or None if it does not overlap. | Returns the point of intersection if the masks overlap with the given offset - or None if it does not overlap.
Mask.overlap_area - Returns the number of overlapping 'pixels'. | Returns the number of overlapping 'pixels'.
Mask.get_bounding_rects - Returns a list of bounding rects of regions of set pixels. | Returns a list of bounding rects of regions of set pixels.

Mask.get_size

Returns the size of the mask.
`Mask.get_size() -> width,height`

Mask.get_at

Returns nonzero if the bit at (x,y) is set.
`Mask.get_at((x,y)) -> int`

Coordinates start at (0,0) is top left - just like Surfaces.

Mask.set_at

Sets the position in the mask given by x and y.
`Mask.set_at((x,y),value)`

Mask.overlap

Returns the point of intersection if the masks overlap with the given offset - or None if it does not overlap.
`Mask.overlap(othermask, offset) -> x,y`

The overlap tests uses the following offsets (which may be negative):

       +----+----------..
       |A   | yoffset
       |  +-+----------..
       +--|B
       |xoffset
       |  |
       :  :

Mask.overlap_area

Returns the number of overlapping 'pixels'.
`Mask.overlap_area(othermask, offset) -> numpixels`

You can see how many pixels overlap with the other mask given. This can be used to see in which direction things collide, or to see how much the two masks collide.

Mask.get_bounding_rects

Returns a list of bounding rects of regions of set pixels.
`Mask.get_bounding_rects() -> Rects`

This gets a bounding rect of connected regions of set pixels. `A` bounding rect is one for which each of the connected pixels is inside the rect.