# Turtle Colision

## Question:

I am making a game using the turtle module, and want to detect colision betwen the original and the clone.I have tried saving the position of the clone, and then checking if the original has the same position, but the turtle only detects the clone if it has the exacly same position. Is there another way to detect colisions?

``````  import turtle
import time
import random
points = 0
t = turtle.Turtle()
sc = turtle.Screen()
turtle.bgcolor("black")
t.turtlesize(3)
c = t.clone()
c.goto(200, 200)
c.color("red")
c.turtlesize(1)
t.color("blue")
def turnleft():
t.left(90)
def turnright():
t.right(90)

while True:
t.forward(10)
time.sleep(0.1)
sc.onkey(turnleft, "Left")
sc.onkey(turnright, "Right")
sc.listen()
# here is the no so working collision detector
if t.pos() == (200, 200):
points = (points + 1)

``````

To detect collisions between turtles in the turtle module, you can use the distance method to calculate the distance between the turtles. If the distance between the turtles is less than the sum of their radii, then they are considered to be colliding.

Check this:

``````import turtle

# create the original turtle
original = turtle.Turtle()

# create the clone turtle
clone = original.clone()

# move the clone turtle to a new position
clone.setpos(50, 50)

# calculate the distance between the original and clone turtles
distance = original.distance(clone)

# calculate the sum of their radii (use a value that makes sense for your game)

# check for a collision
print("Collision detected!")
else:
print("No collision.")
``````

We create the original turtle and clone it to create a second turtle. We then move the clone turtle to a new position. We calculate the distance between the original and clone turtles using the distance method and then check if the distance is less than the sum of their radii. If it is, we consider that a collision has occurred.

You can adjust the value of radius_sum to make it appropriate for your game. Note that this method of collision detection assumes that your turtles are circular and have a fixed radius. If your turtles have a different shape or size, you may need to use a different approach to collision detection.

I know about 2 ways of detecting collisions:

1. Distance-based collision detection: You can use the distance method to calculate the distance between two turtles. It’s very simple and not verry accurate.
``````import turtle
original = turtle.Turtle()
clone = original.clone()

clone.goto(100, 100)
distance = original.distance(clone)
if distance < 20:
print("Collision detected!")
else:
print("No collision.")
``````
1. Rectangle-based collision detection. Use the bounding boxes of the turtles to detect collisions.
``````def get_bounds(t):
x, y = t.pos()
w, h = t.shapesize()
w *= 20
h *= 20
left = x - w/2
right = x + w/2
top = y + h/2
bottom = y - h/2
return left, right, top, bottom

def check_collision(t1, t2):
l1, r1, t1, b1 = get_bounds(t1)
l2, r2, t2, b2 = get_bounds(t2)
if r1 >= l2 and l1 <= r2 and t1 >= b2 and b1 <= t2:
print('collision detected')
``````

Note that this is a sample code and I didn’t test it properly. i found it in my notes.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.