我怎样才能制作碰撞面具? [英] How can I made a collision mask?
问题描述
我尝试制作一个碰撞掩码来检测两个精灵是否发生碰撞,但它根本不起作用,我立即崩溃了,你能帮我吗?
我的代码是:
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屋!