带有x和y坐标边框的Python碰撞检测 [英] Python Collision Detection with x and y coordinates for border

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

问题描述

我正在开发一个Python乌龟游戏,该乌龟可以使用命令移动,但它必须能够检测到屏幕上的矩形和圆形以及边框的碰撞。我不知道该怎么做,任何人都可以帮忙吗?

Im working on a python turtle game where the turtle can move with commands but it has to be able to detect collision with rectangles and circles on the screen as well as the border. I have no idea how to do this can anyone help?

推荐答案

碰撞很容易!在深入了解之前,您需要了解如何获取两点之间的距离。如果您尚未完成此操作,那就只是pythag!

Collision is easy! Before the nitty gritty, you need to understand how to obtain the distance between two points. If you have not done this before it is just pythag!

如果在平面上描绘了两个点(图中的红色点)之间的最短距离是直接从一个点到另一个点,而无需任何转弯,这是两个点之间的距离。在上图中,让y为垂直轴,x为水平轴。点d和e之间的水平距离由值b表示。点d和e之间的垂直距离由值a表示。这样...

If you picture two points on a plane (red points on the picture), the shortest distance to travel between them, is directly from one point to another, without needing to make any turns, this is the distance between the points. In the picture above, let y be the vertical axis and x the horizontal axis. The horizontal distance between points d and e is represented by the value b. The vertical distance between points d and e is represented by the value a. As such...

a = dy-ey

b = dx-ex

a = d.y - e.y
b = d.x - e.x

尽管a和be可能为负,但这没关系,因为我们在下一步中对它们进行平方。

Although a and be might be negative, it doesn't matter, because we sqaure them in a the next step.

要得到c的值,得到a和b的平方和的平方根。乍一看可能很棘手,但非常简单!

To get the value of c, we then have to get the square root of the sum of the squares of a and b. Might sound tricky at first, but very easy!

Python代码
在python中执行此操作很简单。

Python code To do this in python is simple.

c = ((a**2)+(b**2))**0.5
# a**2 is a squared
# anything to the power of 0.5 is square rooted, test it in console
# 25**0.5 = 5.0
# 5**2 = 25

我们现在有了两个点d和e之间的距离。假设d和e的半径为rd和re。然后,通过从圆心之间的距离中减去每个半径,可以检查圆d是否与圆e碰撞。所以c变成...

We now have the distance between the two points d and e. Lets say d and e have the radius rd and re. We can then check if the circle d is colliding with circle e, by subtracting each radius from the distance between the centre of the circles. So c becomes...

c -= rd - re

如果c小于或等于零,则圆之间会发生碰撞!

If c is less than or equal to zero, then you have a collision between circles!

def collision(d, e, rd, re):
    a = d.y-e.y
    b = d.x-e.x
    c = ((a**2)+(b**2))**0.5
    if c > 0:
        # no collision
        return False
    return True

矩形
矩形要容易一些,检查点是否位于矩形内,您只需要一些if语句即可。让这些变量代表矩形x = x位置,y = y位置,w =宽度,h =高度。假设您要检查点p是否与矩形碰撞。

Rectangles Rectangles are a little easier, to check if a point is inside a rectangle all you need is some if statements. Let these variables represent the rectangle x = x location, y = y location, w = width, h = height. Suppose you want to check if point p is colliding with the rectangle.

def check_rect_collision(p, x, y, w, h): 
    if p.x >= x and p.x <= x+w and p.y >= y and p.y <= y+h:
        # collision between p and rectangle
        return True
    return False

这篇关于带有x和y坐标边框的Python碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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