How can I find the coordinate of a turtle in python?
For example, if the turtle is located at (200, 300)
, how would I retrieve that position?
How can I find the coordinate of a turtle in python?
For example, if the turtle is located at (200, 300)
, how would I retrieve that position?
As shown in the Python documentation, turtle.pos()
returns the turtle’s current location as a 2D vector of (x,y)
.
The turtle documentation says that turtle.pos()
gets the position as a Vec2D
vector.
Vec2D
objects are equal, so yes –
Hoick I wanted to know if turtle1 and turtle2 were on the same coordinate I would do if turtle1.pos() == turtle2.pos()
Yes, but since the turtle plays on a floating point plane, this might cause you trouble when they are close but not exactly atop each other. You'll probably be better off with a test like:
turtle1.distance(turtle2) < 0.1
I.e. is the distance between centers less than some fuzz factor that you determine. If you want to know if any turtle parts overlap (e.g. legs touching) then your fuzz factor may be as high as 10.0
Well the turtle.pos() method returns a tuple
A Vector is derived from tuple, so a vector is a tuple!
so you can check if the turtle is at a specific coordinate by using:
if turtle.pos() == (5.0, 0.0):
print(“at coordinate”)
© 2022 - 2024 — McMap. All rights reserved.
fx = food.xcor()
(orfood.ycor()
) But best to work withfood.getposition()
(or its synonymfood.getpos()
) if you need both coordinates. – Glynas