严重的(至少在我看来)定位问题(模块)



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

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

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

原因可能是因为很难让准确地到达点 (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.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()方法很灵活,因为除了单独的坐标外,它还可以接受元组坐标,甚至是另一只!

最新更新