“The only way to go fast, is to go well.”
Robert C. Martin, Clean Architecture
Related Stack Overflow questions:
Related Stack Overflow questions:
Collision between masks in PyGame

Can’t figure out how to check mask collision between two sprites

why collide_mask is significantly slower than collide_rect in pygame?
Check collision between a image and a line

How to do a collision detection between line and rectangle in pygame?

Overlap between mask and fired beams in PyGame [AI car model vision]

How can I rotate my hitbox with my rotating and moving car in pygame?
Pygame mask collision only putting damage on base on first collision

Pygame mask collision. Using a transparent image as background not working
wondering about pixel perfect mousepointer collision in the case of a button

How do I check if a mask fully contains another mask in Pygame?
How would I clamp the player sprite to a certain polygon boundary

Related Stack Overflow questions:
Related Stack Overflow questions:
How to add a white surface with the shape of my original image in pygame?

Related Stack Overflow questions:
Related Stack Overflow questions:
Related Stack Overflow questions:
How to get the correct dimensions for a pygame rectangle created from an image
Questions regarding the pygame.mask function

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object. This function does not consider the drawing area in the image. If you want to find the bounding rectangle of the painted area in the surface, you need to create a mask.
pygame.mask.from_surface creates a pygame.mask.Mask object form a pygame.Surface.
A Surface is bitmap. A Mask is an 2 dimensional array with Boolean values. The Mask created is the size of the _Surface. A field is True if the corresponding pixel in the surface is not transparent, and False if it is transparent:
surf_mask = pygame.mask.from_surface(surf)
Get a list containing a bounding rectangles (sequence of pygame.Rect objects) for each connected component with get_bounding_rects.
pygame.mask.Mask.get_bounding_rects creates a list of pygame.Rect objects. Each rectangle describes a bounding area of connected pixles. If the Surface contains exactly 1 connected image, you will get exactly 1 rectangle surrounding the image:
rect_list = surf_mask.get_bounding_rects()
Create the union rectangle of the sequence of rectangles with unionall:
surf_mask_rect = rect_list[0].unionall(rect_list)
def getMaskRect(surf, top = 0, left = 0):
surf_mask = pygame.mask.from_surface(surf)
rect_list = surf_mask.get_bounding_rects()
surf_mask_rect = rect_list[0].unionall(rect_list)
surf_mask_rect.move_ip(top, left)
return surf_mask_rect