-3

I want to know if a turtle is in the same place as it was before (for a maze game in python). I tried:

import turtle
c == 1
a = turtle.Turtle()
b = turtle.Turtle()
b.fd(100)
while c == 1:
  a.fd(10)
if a.is_collided_with(b):
  print("Collision")
break

The problem is that it says that there is no such thing as .is_collided_with() (it might because I'm using python 3.9.1). Is there a way to do this without using several turtles? Thanks

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jonathan Drukker
  • 119
  • 1
  • 2
  • 12
  • 2
    `turtle` never had `is_collided_with()` - where did you find it? – furas Jan 01 '21 at 19:23
  • 1
    can't you simply get its position and keep on some list and after move check if new position is on this list? – furas Jan 01 '21 at 19:26
  • 1
    You could keep track of already visited locations in a set, or you could define tiles and each tile keeps track of whether it was visited by the turtle or not. – Vepir Jan 01 '21 at 19:26
  • Looks like the indenting got messed up. You can [edit] to fix it. – wjandrea Jan 01 '21 at 21:02

1 Answers1

0

Ignoring the surrounding code, what we really want is:

if a.distance(b) < 5:
    print("Collision")

As turtles wander their floating point plane, two turtles right atop each other may not be at the same exact coordinates. The 5 value can be set to however close you want to consider a collision to be.

As far as distance() goes, it can take another turtle, as in your example, or it can take a position from a list of visited positions.

I want something that detects a collision with the turtle drawing.

Here's a turtle-based maze that does that by making the walls themselves turtles (though they look like lines) and uses the distance function above to detect collision.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • It means that it will print collision only when the turtle toches a turtle; I want something that detects a collision with the turtle drawing. Thanks for trying! – Jonathan Drukker Jan 02 '21 at 08:46
  • @JonathanDrukker, I've added a pointer to a turtle maze that works the way you described but is implemented the way I described. – cdlane Jan 03 '21 at 01:40