(至少在我眼中)定位(乌龟模块)有严重问题

问题描述 投票:0回答:2

我做了一个简单的游戏,用户需要以指定的移动次数回到相同的位置。但是,与识别用户是否已返回初始位置的定位有关的代码似乎不起作用。

这是用于指定海龟起始位置的代码,以防万一:

my_turtle.setposition(0.00, 0.00)

这里是代码(仅涉及定位的部分):

if my_turtle.position() == (0.00, 0.00) and tries == score:
    print("Well done")
        break
elif my_turtle.position() == (0.00, 0.00) and (tries > score or score > tries):
    print("Return to original position in exactly moves")
python python-3.x turtle-graphics
2个回答
2
投票

原因可能是因为很难使乌龟精确地指向(0,0)点,所以最好设置边界:

if 10>my_turtle.xcor()>-10 and 10>my_turtle.ycor()>-10 and tries == score:
    print("Well done")
    break
elif 10>my_turtle.xcor()>-10 and 10>my_turtle.ycor()>-10 and tries != score:
    print("Return to original position in exactly moves")

0
投票

海龟在浮点平面上徘徊。当您的海龟在屏幕上离开家(0, 0)并最终返回时,它会积聚一些浮点噪声,例如(0.0001, -0.000)。因此,无法使用像==这样的整数比较。相反,我们可以使用distance()方法:

if my_turtle.distance(0, 0) < 10:

    if tries == score:
        print("Well done")
            break

    print("Return to original position in exactly moves")

distance()方法非常灵活,它与单独的坐标一起还可以接受元组坐标,甚至可以接受另一个乌龟!

© www.soinside.com 2019 - 2024. All rights reserved.