'How do I check if a mask fully contains another mask in Pygame?

I was just wondering if there was a way of knowing if a mask is fully contained inside another mask in Pygame.



Solution 1:[1]

You have to use 2 methods:

  1. pygame.mask.Mask.count():

    Returns the number of set bits in the mask

  2. pygame.mask.Mask.overlap_area():

    Returns the number of overlapping set bits

If you want to test whether mask1 is completely contained in mask2, the number of bits of mask1 must be equal to the number of overlapping bits:

bits_mask1 = mask1.size()
offset = (rect2.x - rect1.x), (rect2.y - rect1.y)
if bits_mask1 == mask1.overlap_area(mask2, offset):
    print('overlapp')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rabbid76