问题
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?
回答1:
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!
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 = d.y - e.y
b = d.x - e.x
Although a and be might be negative, it doesn't matter, because we sqaure them in a the next step.
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 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
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
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
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
回答2:
To test collision against a circle is trivial -- use the distance()
method which measures from the center of the cursor to a position or center of another turtle. Given a circle's center
position and its radius
:
def circle_collision(the_turtle, center, radius):
return the_turtle.distance(center) <= radius
If you need to know if the turtle's nose has touched the circle, you could add half the turtle's size to the radius
, for a (possibly resized) cursor which would be very roughly:
def circle_collision(the_turtle, center, radius):
dx, dy, border = the_turtle.shapesize()
return the_turtle.distance(center) <= radius + 5 * (dx + dy) + border
I.e. half the default turtle size of 20 pixels times the average of dx
and dy
plus the width of the border around the turtle. Or some such approximation.
To detect rectangle collision is also reasonably simple:
def rectangle_collision(the_turtle, x, y, width, height):
tx, ty = the_turtle.position()
return x <= tx <= x + width and y <= ty <= y + height
Adjust to whatever rectangle measure you're using:
def rectangle_collision(the_turtle, llx, lly, urx, ury):
x, y = the_turtle.position()
return llx <= x <= urx and lly <= y <= ury
a la the coordinate arguments for setworldcoordinates()
.
来源:https://stackoverflow.com/questions/53511394/python-collision-detection-with-x-and-y-coordinates-for-border