Turtle Colision

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()
c = t.clone()
c.goto(200, 200)
def turnleft():
def turnright():

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

>Solution :

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)
radius_sum = 20

# check for a collision
if distance < radius_sum:
    print("Collision detected!")
    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.

Leave a Reply