Pygame碰撞检测,透明边框

标签 pygame png transparency collision

对于我的 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/

相关文章:

r - 将 png 添加到具有透明背景并通过 {magick} 旋转的 {ggplot2}

Java Swing : Make a dialog partially transparent.

php - 使用 gdlib 复制透明 PNG

c++ - std::vector 到 GLvoid*?

python - 描绘一个 pygame 圆圈动画

python - 如何使我的 pygame 窗口最小化和最大化?

svg - 用于显示 PNG 代替旧浏览器的 SVG 的条件代码?

javascript - iframe 如何获得其父背景颜色?

PYTHON 使用转义键退出时遇到问题

python - 有没有更好的方法来使用 Pygame 的键盘移动 Sprite ?