1
\$\begingroup\$

I'm new to using masks in Pygame and I'm having trouble with them. I have already learned how to check collisions between two masks but I'm wondering about an effective way to check collisions between a rect and a mask.

I've seen this post on stackoverflow : collisions between rect and mask but I didn't understand the answer and I don't even want to create my own function to do this work as I think there is a way to do it with the pygame functions.

So I tried to find a way myself and this is my attempt:

class Rectangle:
    def __init__(self) :
        self.x, self.y = 240, 300
        self.rect = Rect(self.x, self.y, 50, 50)
        # I have made a function to get the coordinates of the center of an image or a rect
        self.center = center(self.x, self.y, self.rect)
        self.mask = pygame.mask.Mask((50, 50), True)
        # some code

    def blit(self) :
        # update the pos of the center
        self.center = center(self.x, self.y, self.rect)
        pygame.draw.rect(MyScreen, 'green', self.rect, border_radius=2)
        # some code

class Circle:
    def __init__(self) :
        self.image = load('redcircle.png')
        self.x, self.y = 400, 220
        self.mask = pygame.mask.from_surface(self.image)
        self.center = center(self.x, self.y, self.image)

    def blit(self) :
        # update the pos of the center
        self.center = center(self.x, self.y, self.image)
        MyScreen.blit(self.image, (self.x, self.y))

myrectangle = Rectangle()
mycircle = Circle()

collision_detection() :
    # I also created a function called 'offset' to calculate the offset
    # However I still don't understand what an offset is, that's why I'm just calculating it automatically
    if mycircle.mask.overlap(myrectangle.mask.get_rect(center=myrectangle.center), offset(mycircle, myrectangle)):
            print('it worked !')

This code didn't work And I got a "TypeError: argument 1 must be pygame.mask.Mask, not pygame.Rect" error.

So now I'm looking for a simple way to detect collisions between a mask and a rect object and as I mentionned in the comments in the code above, I still don't understand what is an offset that the function pygame.mask.overlap takes though I also checked this post. So I'm looking for an easy explanation, thanks !

\$\endgroup\$
3
  • 1
    \$\begingroup\$ overlap appears to be taking a Mask as its first parameter, did you try to just pass myrectangle.mask? \$\endgroup\$
    – Vaillancourt
    Commented Jul 21, 2021 at 14:03
  • \$\begingroup\$ @Vaillancourt, no but I'll try that ! \$\endgroup\$
    – Salem
    Commented Jul 21, 2021 at 14:05
  • \$\begingroup\$ @Vaillancourt, I can't believe it, it worked, thank youu. Please write your suggestion as an answer with some more details so that I accept it. \$\endgroup\$
    – Salem
    Commented Jul 21, 2021 at 14:13

1 Answer 1

1
\$\begingroup\$

The way you create your "rectangle mask" seems appropriate and roughly in line with the question you've linked to on StackOverflow:

self.mask = pygame.mask.Mask((50, 50), True)

Now that this is out of the way, it seems that the mask.overlap method that you use expects a Mask, not a rectangle. Since you have defined that "rectangle mask" in your Rectangle object, you just need to pass it to the other object's overlap method.

if mycircle.mask.overlap(myrectangle.mask, offset(mycircle, myrectangle)):

The offset the overlap method expects is only the difference in x/y between the two objects "top left" corners. In your example, I assume that the call to offset(mycircle, myrectangle) returns the difference

[myrectangle.x - mycircle.x, myrectangle.y - mycircle.y]
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks a lot for your help ;) \$\endgroup\$
    – Salem
    Commented Jul 21, 2021 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .