0

Can I make a mask of a rect, if so, how? I'm trying to make collisions between a mask and a rect, and since I can't make a rect of a mask(No pixel perfect collision then) I figured I'd ask if I could make a mask of a rect? Like given the dimensions and x/y positon of a rect, how could I make a mask based on that?

2 Answers2

0

Create a pygame.mask.Mask object and set all bits 1 with fill()

rect_mask = pygame.mask.Mask((rect.width, rect.height)))
rect_mask.fill()

Use pygame.mask.Mask.overlap to for the collision detection between Mask objects. The offset parameter of the method overlap() is the relative position of the othermask in relation to the pygame.mask.Mask object.

e.g.:

object 1: x1, y1, mask1
object 2: x2, y2, mask2

offset = (x2 - x1, y2 - y1)
if mask1.overlap(mask2, offset):
    print("hit")    

See also PyGame collision with masks and Pygame mask collision.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

you can create a pygame.mask.Mask using the width and height of the rect object:

playermask = pygame.mask.Mask((player.width, player.height))
playermask.fill()