(Turtle/Python3)如何创建一个独立工作在龟上的while循环?
问题描述:
所以我想做的事,就是显示: 亚历克斯来自Alice的距离是:(距离我已经计算)(Turtle/Python3)如何创建一个独立工作在龟上的while循环?
我需要这显示在屏幕上方..永远,提神种,就像PING统计在游戏节目上面那样..
我假设它会是一个while循环?我需要的程序运行的剩余部分,而这是始终显示爽口..
答
这里是我对这个问题极简方法(当然,不是全部,我也扔在几个细微的):
from turtle import Turtle, Screen
FONT_SIZE = 24
FONT = ('Arial', FONT_SIZE, 'normal')
def turtle_drag(turtle, other, x, y):
turtle.ondrag(None) # disable event hander in event hander
turtle.goto(x, y)
display.undo() # erase previous distance
distance = turtle.distance(other)
display.write('Distance: {:.1f}'.format(distance), align='center', font=FONT)
turtle.setheading(turtle.towards(other)) # make them always look
other.setheading(other.towards(turtle)) # lovingly at each other
turtle.ondrag(lambda x, y: turtle_drag(turtle, other, x, y)) # reenable
screen = Screen()
screen.setup(500, 500)
display = Turtle(visible=False)
display.penup()
display.goto(0, screen.window_height()/2 - FONT_SIZE * 2)
display.write('', align='center', font=FONT) # garbage for initial .undo()
Alex = Turtle('turtle')
Alex.speed('fastest')
Alex.color('blue')
Alex.penup()
Alice = Turtle('turtle')
Alice.speed('fastest')
Alice.color('red')
Alice.penup()
turtle_drag(Alex, Alice, -50, -50) # initialize position and event hander
turtle_drag(Alice, Alex, 50, 50)
screen.mainloop()
当你拖动亚历克斯或爱丽丝,他们的中心到屏幕顶部的中心距离更新。这使用一个独立的,不可见的固定乌龟来处理显示屏,.undo()
删除以前的值,.write()
显示新的值。使龟向着对方后的距离显示更新可确保它们不会被困在无形显示龟下方(即undragable),所以他们甚至可以在文本顶上postitioned:
欢迎堆栈溢出。你已经尝试过这么做了吗?请回顾[预计需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)。堆栈溢出不是一种编码服务。您需要研究您的问题,并尝试在发布之前亲自编写代码。如果遇到某些特定问题,请返回并包含[最小,完整和可验证示例](https://stackoverflow.com/help/mcve)以及您尝试的内容摘要,以便我们提供帮助。 – Sand