严重的(至少在我看来)定位问题(乌龟模块)
Severe (in my eyes at least) issue with positioning (turtle module)
我做了一个简单的游戏,用户需要在指定的步数内回到相同的位置。但是,识别用户是否返回初始位置的定位代码似乎不起作用。
为了以防万一,这是指定乌龟起始位置的代码:
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)
并最终 returns 时,它会积累一些浮点噪声,例如(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()
方法的灵活性在于,除了单独的坐标外,它还可以接受元组坐标,甚至是另一只海龟!
我做了一个简单的游戏,用户需要在指定的步数内回到相同的位置。但是,识别用户是否返回初始位置的定位代码似乎不起作用。
为了以防万一,这是指定乌龟起始位置的代码:
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)
并最终 returns 时,它会积累一些浮点噪声,例如(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()
方法的灵活性在于,除了单独的坐标外,它还可以接受元组坐标,甚至是另一只海龟!