如何比较坐标 python
how to compare coordinates python
我有两只不同的海龟,每只都是独立的实体,我想比较它们的坐标,但在我的代码中它们是图片,所以 0,0 在不同的地方,所以我必须将 1 移动到 100 ,-100 让它看起来不错,而且我想要它。
import turtle
turtle = Turtle()
Shop = Turtle()
Shop.up()
turtle.up()
Shop.goto(100,-100)
if (#insert comparison here):
print("compared")
提前致谢
使用对象方法
xcor()
和
ycor()
在你的情况下你想使用
if (turtle.xcor() == shop.xcor() and turtle.ycor() == turtle.ycor()): #rest of code
要做到这一点,我们需要处理两个问题。首先是你的图像偏移。其次是 turtle 使用浮点坐标,所以简单的 ==
会导致麻烦。 (即 0.00001 == 0.0
是否适合您的目的?)这是一种方法:
from turtle import Turtle
customer = Turtle()
customer.up()
SHOP_OFFSET = (100, -100)
X, Y = 0, 1
shop = Turtle()
shop.up()
shop.goto(SHOP_OFFSET[X], SHOP_OFFSET[Y])
if abs(customer.xcor() - (shop.xcor() - SHOP_OFFSET[X])) < 0.5 and \
abs(customer.ycor() - (shop.ycor() - SHOP_OFFSET[Y])) < 0.5:
print("same location")
我有两只不同的海龟,每只都是独立的实体,我想比较它们的坐标,但在我的代码中它们是图片,所以 0,0 在不同的地方,所以我必须将 1 移动到 100 ,-100 让它看起来不错,而且我想要它。
import turtle
turtle = Turtle()
Shop = Turtle()
Shop.up()
turtle.up()
Shop.goto(100,-100)
if (#insert comparison here):
print("compared")
提前致谢
使用对象方法
xcor()
和
ycor()
在你的情况下你想使用
if (turtle.xcor() == shop.xcor() and turtle.ycor() == turtle.ycor()): #rest of code
要做到这一点,我们需要处理两个问题。首先是你的图像偏移。其次是 turtle 使用浮点坐标,所以简单的 ==
会导致麻烦。 (即 0.00001 == 0.0
是否适合您的目的?)这是一种方法:
from turtle import Turtle
customer = Turtle()
customer.up()
SHOP_OFFSET = (100, -100)
X, Y = 0, 1
shop = Turtle()
shop.up()
shop.goto(SHOP_OFFSET[X], SHOP_OFFSET[Y])
if abs(customer.xcor() - (shop.xcor() - SHOP_OFFSET[X])) < 0.5 and \
abs(customer.ycor() - (shop.ycor() - SHOP_OFFSET[Y])) < 0.5:
print("same location")