How do I get the X coordinate for my turtle graphics turtle

懵懂的女人 提交于 2019-12-02 17:17:20

问题


I can create a turtle that will be in a window with the following code:

Turtle t1 = new Turtle(w,100,100);

If I want to know its coordinates, I can write:

int getX(w);

But when I have 2 turtles, t1 and t2, I don't know what to do if I want to know turtle1's X coordinates.

Turtle t1 = new Turtle(w,100,100);
Turtle t2 = new Turtle(w,200,100);

If I were to write int getX(w), which turtles X coordinate would I get? How do I write so I get t1s cordinate?


回答1:


I have no idea what Turtle class you are using, but I'd imagine you'd do something like this:

int x1 = t1.getX(w);
int x2 = t2.getX(w);



回答2:


Use turtle.xcor() it returns the x position. Use turtle.ycor() it returns the y position.




回答3:


For python turtle module, get the x and y coordinates of the turtle use the getPosition method like this:

import turtle
import time
alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.write("hello")
time.sleep(1)

turtle.position() - Return the turtle’s current location (x,y) (as a Vec2D vector).

Read more about it here: https://docs.python.org/3.3/library/turtle.html#turtle.write



来源:https://stackoverflow.com/questions/19155992/how-do-i-get-the-x-coordinate-for-my-turtle-graphics-turtle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!