Pygame collision detection, transparent border

北慕城南 提交于 2021-02-08 10:50:38

问题


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

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

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

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.

The transparent border collide, and pygame detect this :(

Any idea to stop the transparent border from colliding ?


回答1:


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.

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

update

Regarding performance from the 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.



来源:https://stackoverflow.com/questions/35275699/pygame-collision-detection-transparent-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!