How can I made a collision mask?

See the documentation of pygame.sprite.collide_mask():

Collision detection between two sprites, using masks.

collide_mask(SpriteLeft, SpriteRight) -> point

Tests for collision between two sprites, by testing if their bitmasks overlap. If the sprites have a “mask” attribute, that is used as the mask, otherwise a mask is created from the sprite image. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a “rect” and an optional “mask” attribute.

The parameters to .collide_mask() have to be 2 pygame.sprite.Sprite objects, rather than 2 mask pygame.mask.Mask objects:

In the following is assumed, that Player and oc1 are pygame.sprite.Sprite objects:

Player.rect = Player.image.get_rect()
oc1.rect = oc1.image.get_rect()

Cm = pg.sprite.collide_mask(Player, oc1)

if Cm != None :
    print('life - 1')

Minimal example: repl.it/@Rabbid76/PyGame-SpriteMask

See also Sprite mask

Leave a Comment