Pygame 碰撞检测,透明边框
Pygame collision detection, transparent border
对于我的 pygame 项目,我必须检测屏幕上移动的各种 .png 图像之间的碰撞。
我把图片放在精灵里面,精灵放在组里面,然后我用这个:
pygame.sprite.spritecollide(perso, zombie_group, False)
然而,有时我的图像没有接触,但 pygame 检测到碰撞...
这是因为我的图像是带有透明边框的 png。
透明边框碰撞,pygame检测到这个:(
有什么办法可以防止透明边框发生碰撞吗?
好的,精灵会拍摄他无法检测到的图像是在透明 BG 上还是在彩色背景上,他现在基本上只看到一个矩形。
如果您使用的是不规则形状并且矩形近似值不够,我建议您使用 collide_mask also check masks 它可能正是您想要的
更新
关于 tutorial 的表现:
There are other ways to do this, with ANDing sprite masks and so on,
but any way you do it in pygame, it’s probably going to be too slow.
For most games, it’s probably better just to do ‘sub-rect collision’ –
create a rect for each sprite that’s a little smaller than the actual
image, and use that for collisions instead. It will be much faster,
and in most cases the player won’t notice the inprecision.
对于我的 pygame 项目,我必须检测屏幕上移动的各种 .png 图像之间的碰撞。
我把图片放在精灵里面,精灵放在组里面,然后我用这个:
pygame.sprite.spritecollide(perso, zombie_group, False)
然而,有时我的图像没有接触,但 pygame 检测到碰撞... 这是因为我的图像是带有透明边框的 png。
透明边框碰撞,pygame检测到这个:(
有什么办法可以防止透明边框发生碰撞吗?
好的,精灵会拍摄他无法检测到的图像是在透明 BG 上还是在彩色背景上,他现在基本上只看到一个矩形。
如果您使用的是不规则形状并且矩形近似值不够,我建议您使用 collide_mask also check masks 它可能正是您想要的
更新
关于 tutorial 的表现:
There are other ways to do this, with ANDing sprite masks and so on, but any way you do it in pygame, it’s probably going to be too slow. For most games, it’s probably better just to do ‘sub-rect collision’ – create a rect for each sprite that’s a little smaller than the actual image, and use that for collisions instead. It will be much faster, and in most cases the player won’t notice the inprecision.