我该如何制作碰撞遮罩?

3

我试图创建一个碰撞掩码来检测两个精灵是否相互碰撞,但完全不起作用,我遇到了即时崩溃问题,你能帮我吗?

我的代码如下:

        Player.rect = Player.image.get_rect()
        oc1.rect = oc1.image.get_rect()
        mask_1 = pg.mask.from_surface(Player)
        mask_2 = pg.mask.from_surface(oc1)
        Cm = pg.sprite.collide_mask(mask_1, mask_2)
        if Cm != None :
            print('life - 1')
1个回答

3
请参阅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.

.collide_mask()方法的参数必须是两个pygame.sprite.Sprite对象,而不是两个pygame.mask.Mask对象:

以下假设Playeroc1pygame.sprite.Sprite对象:

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

最简单的例子: repl.it/@Rabbid76/PyGame-SpriteMask

另请参见Sprite mask


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接