Java 如何获得我的海龟图形海龟的 X 坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19155992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:35:02  来源:igfitidea点击:

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

javaturtle-graphics

提问by user2779497

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.

但是当我有2只海龟t1和t2时,如果我想知道turtle1的X坐标,我不知道该怎么办。

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?

如果我写int getX(w),我会得到哪些海龟的 X 坐标?我怎么写才能得到t1坐标?

回答by Geobits

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

我不知道你在使用什么 Turtle 类,但我想你会做这样的事情:

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

回答by Eric Leschinski

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

对于 python 海龟模块,使用 getPosition 方法获取海龟的 x 和 y 坐标,如下所示:

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).

turtle.position() - 返回海龟的当前位置 (x,y)(作为 Vec2D 向量)。

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

在此处阅读更多相关信息:https: //docs.python.org/3.3/library/turtle.html#turtle.write

回答by Ja S

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

使用turtle.xcor()它返回 x 位置。使用turtle.ycor()它返回 y 位置。