我怎样才能制作碰撞面具? [英] How can I made a collision mask?

查看:52
本文介绍了我怎样才能制作碰撞面具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作一个碰撞掩码来检测两个精灵是否发生碰撞,但它根本不起作用,我立即崩溃了,你能帮我吗?

我的代码是:

 Player.rect = Player.image.get_rect()oc1.rect = oc1.image.get_rect()mask_1 = pg.mask.from_surface(玩家)mask_2 = pg.mask.from_surface(oc1)Cm = pg.sprite.collide_mask(mask_1, mask_2)如果厘米!= 无:打印('生活 - 1')

解决方案

查看

另见精灵掩码

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 :

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

解决方案

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:

See also Sprite mask

这篇关于我怎样才能制作碰撞面具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆