pygame碰撞检测,透明边框 [英] Pygame collision detection, transparent border

查看:109
本文介绍了pygame碰撞检测,透明边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的pygame项目,我必须检测屏幕上移动的各种.png图像之间的碰撞.

For my pygame project, I must detect collision betweeen various .png image that move on the screen.

我已将图像放置在sprite中,并将sprite放置在组中,然后我使用了它:

I have put the image inside sprite, and the sprite inside group, then I use this :

pygame.sprite.spritecollide(perso, zombie_group, False)

但是,有时我的图像没有碰到,但是pygame检测到碰撞...这是因为我的图片是带有透明边框的png.

However, sometime, my image don't touch, but pygame detect a collision... This is due to the fact that my images are png with transparent borders.

透明边框碰撞,并且pygame检测到此:(

The transparent border collide, and pygame detect this :(

是否有任何阻止透明边框碰撞的想法?

Any idea to stop the transparent border from colliding ?

推荐答案

好吧,精灵会拍摄他无法检测到的图像,该图像是在透明BG上还是在彩色bg上,他现在基本上只是看到一个矩形

Ok the sprite will take the image he can't detect if it was on a trasnparent BG or a color bg, he basically is just seeing a rectangle right now.

如果您使用的形状不规则并且矩形近似值还不够,我建议您使用 collide_mask 还要检查面具可能是什么您想要

If you are using irregular shapes and a rectangle approximation is not enough I would recommend using collide_mask also check masks it is probably what you want

更新

关于教程的性能:

还有其他方法,例如使用ANDing子画面遮罩等,但是,无论您在pygame中做任何事情,它都可能太慢了.对于大多数游戏,最好做次矩形碰撞"-为每个精灵创建一个比实际小一点的矩形图片,并将其用于碰撞.这样会更快而且在大多数情况下,玩家不会注意到这种错误.

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碰撞检测,透明边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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