pygame documentation |
||
Pygame Home ||
Help Contents ||
Reference Index ||
Camera || Cdrom || Color || Cursors || Display || Draw || Event || Examples || Font || Gfxdraw || Image || Joystick || Key || Locals || Mask || Midi || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Tests || Time || Transform |
pygame.mask.from_surface - Returns a Mask from the given surface. | Returns a Mask from the given surface. |
pygame.mask.from_threshold - Creates a mask by thresholding Surfaces | Creates a mask by thresholding Surfaces |
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.
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.
This is a more featureful method of getting a Mask from a Surface. If supplied with only one Surface, all pixels within the threshold of the supplied color are set in the Mask. If given the optional othersurface, all pixels in Surface that are within the threshold of the corresponding pixel in othersurface are set in the 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.overlap_mask - Returns a mask of the overlapping pixels | Returns a mask of the overlapping pixels |
Mask.fill - Sets all bits to 1 | Sets all bits to 1 |
Mask.clear - Sets all bits to 0 | Sets all bits to 0 |
Mask.invert - Flips the bits in a Mask | Flips the bits in a Mask |
Mask.scale - Resizes a mask | Resizes a mask |
Mask.draw - Draws a mask onto another | Draws a mask onto another |
Mask.erase - Erases a mask from another | Erases a mask from another |
Mask.count - Returns the number of set pixels | Returns the number of set pixels |
Mask.centroid - Returns the centroid of the pixels in a Mask | Returns the centroid of the pixels in a Mask |
Mask.angle - Returns the orientation of the pixels | Returns the orientation of the pixels |
Mask.outline - list of points outlining an object | list of points outlining an object |
Mask.convolve - Return the convolution of self with another mask. | Return the convolution of self with another mask. |
Mask.connected_component - Returns a mask of a connected region of pixels. | Returns a mask of a connected region of pixels. |
Mask.connected_components - Returns a list of masks of connected regions of pixels. | Returns a list of masks of connected regions of 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. |
Coordinates start at (0,0) is top left - just like Surfaces.
The overlap tests uses the following offsets (which may be negative):
+----+----------.. |A | yoffset | +-+----------.. +--|B |xoffset | | : :
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. An approximate collision normal can be found by calculating the gradient of the overlap area through the finite difference.
dx = Mask.overlap_area(othermask,(x+1,y)) - Mask.overlap_area(othermask,(x-1,y)) dy = Mask.overlap_area(othermask,(x,y+1)) - Mask.overlap_area(othermask,(x,y-1))
Returns a Mask the size of the original Mask containing only the overlapping pixels between Mask and othermask.
Sets all bits in a Mask to 1.
Sets all bits in a Mask to 0.
Flips all of the bits in a Mask, so that the set pixels turn to unset pixels and the unset pixels turn to set pixels.
Returns a new Mask of the Mask scaled to the requested size.
Performs a bitwise OR, drawing othermask onto Mask.
Erases all pixels set in othermask from Mask.
Returns the number of set pixels in the Mask.
Finds the centroid, the center of pixel mass, of a Mask. Returns a coordinate tuple for the centroid of the Mask. In the event the Mask is empty, it will return (0,0).
Finds the approximate orientation of the pixels in the image from -90 to 90 degrees. This works best if performed on one connected component of pixels. It will return 0.0 on an empty Mask.
Returns a list of points of the outline of the first object it comes across in a Mask. For this to be useful, there should probably only be one connected component of pixels in the Mask. The every option allows you to skip pixels in the outline. For example, setting it to 10 would return a list of every 10th pixel in the outline.
Returns a mask with the (i-offset[0],j-offset[1]) bit set if shifting othermask so that it's lower right corner pixel is at (i,j) would cause it to overlap with self.
If an outputmask is specified, the output is drawn onto outputmask and outputmask is returned. Otherwise a mask of size self.get_size() + othermask.get_size() - (1,1) is created.
This uses the SAUF algorithm to find a connected component in the Mask. It checks 8 point connectivity. By default, it will return the largest connected component in the image. Optionally, a coordinate pair of a pixel can be specified, and the connected component containing it will be returned. In the event the pixel at that location is not set, the returned Mask will be empty. The Mask returned is the same size as the original Mask.
Returns a list of masks of connected regions of pixels. An optional minimum number of pixels per connected region can be specified to filter out noise.
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.