问题描述
你好,我正在开发简单的太空入侵者克隆游戏,我需要想办法在我射击时如何检测子弹和外星人的碰撞.有什么建议 ?谢谢
Hello I am developing easy space invaders clone game and i need to figure out a way how to detect collision of the bullet and the alien when i shoot. Any suggestions ? Thanks
推荐答案
使用 Pygame 代替 Tkinter(最终代替 Canvas) - 有检查碰撞的函数.
Use Pygame in place of Tkinter (eventually in place of Canvas) - there are functions to check collisions.
要检查碰撞,您必须获得两个元素的位置并检查它们之间的距离:
To check collision you have to get position both elements and check distance between them:
a + b = c
a = x1 - x2
, b = y1 - y2
, c = 物体 A(x1,y1) 和 B(x2,y2) 之间的距离
如果距离小于某个值,那么您就会发生碰撞 - 距离不一定为零才能发生碰撞.通过这种方式,您可以检查对象周围的圆形碰撞.
If distance is smaller then some value then you have collision - distance doesn't have to be zero to have collision. This way you check collision in circle around object.
但是您可以检查对象周围正方形区域的碰撞 - 计算起来会更容易
But you can check collision in square area around the object - it wil be easer for you to calculate it
对象 A (x1,y1)
的面积为 x1-10 ... x1+10, y1-10 ... y1+10
.您必须检查对象 B (x2,y2)
是否在该正方形中.
Object A (x1,y1)
has square area x1-10 ... x1+10, y1-10 ... y1+10
. You have to check whether object B (x2,y2)
is in that square.
这篇关于如何检测两个画布对象 Tkinter 的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!