How can I made a collision mask?

前端 未结 1 434
遇见更好的自我
遇见更好的自我 2020-12-12 04:19

I try to make a collision mask to detect if two sprites collide themselves, but It\'s not working at all, I got an instant crash, Can you help me ?

My code is :

1条回答
  •  囚心锁ツ
    2020-12-12 04:57

    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

    0 讨论(0)
提交回复
热议问题