ThreeGordons
ThreeGordons

Reputation: 11

How can I make a mask for a rect?

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?

Upvotes: 0

Views: 1391

Answers (2)

DeathCatThor
DeathCatThor

Reputation: 1

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()

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 211278

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.

Upvotes: 0

Related Questions