对于我的 pygame 项目,我必须检测屏幕上移动的各种 .png 图像之间的碰撞。
我将图像放入 Sprite 中,并将 Sprite 放入组中,然后我使用这个:
pygame.sprite.spritecollide(perso, zombie_group, False)
但是,有时,我的图像没有接触,但 pygame 检测到碰撞...... 这是因为我的图像是带有透明边框的 png。
透明边框发生碰撞,pygame 检测到这一点:(
有什么办法可以阻止透明边框发生碰撞吗?
最佳答案
好吧, Sprite 将拍摄他无法检测到的图像,无论它是在透明背景还是彩色背景上,他现在基本上只是看到一个矩形。
如果您使用不规则形状并且矩形近似值不够,我建议使用 collide_mask另请检查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碰撞检测,透明边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35275699/