繁体   English   中英

Python冲突检测,不在pygame中

[英]Python collision detection, NOT in pygame

我正在尝试使用Zelle在python图形模块中构建2D地图。 我使用Polygon类对象创建了地图边界。 如果我想检查一个cirlce对象是否正在接触地图边界以检测碰撞,该怎么办?

这是我的意思的一个例子:

poly  = Polygon(Point(x1,y1), Point(x2,y2), Point(x3,y3)) .draw(win)  # a triangle shape
circ = Circle (Point(x4,y4), radius) .draw(win)      # drawn in the middle of the triangle map

我可以通过使用circ.getCenter()来获得circ位置,但是我不知道检查这两个对象是否交叉的最佳方法是什么。 也许像这样

def collision(circ,poly,x,y):

    if position of circle passes the position of the line of the poly at x,y:
        detect collision

   else:
       pass

我在python中找到了一个碰撞检测程序。

if circle_x < rect_x + circle_width and circle_x + rect_width > rect_x and circle_y < rect_y + circle_height and circle_height + circle_y > rect_height :

但是,这可以检测圆的边缘是否接触到矩形,因此,要使其检测到圆是否接触到地图,您需要将所有rect_x替换为0,将所有rect_y替换为0,然后替换rect_width和屏幕宽度,rect_height和屏幕高度,如下所示:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height :

现在,它可以检测出该圆形是否在总体上触摸地图为了检测该圆形是否在触摸屏幕的边缘,您将不需要在if语句中放置任何内容,并在要放置所需内容的地方添加else语句,例如这个:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height :
    #put nothing here
else:
    #put the code you need here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM