(Turtle / Python3)如何制作一个在乌龟中独立工作的while循环?

时间:2017-10-18 01:35:07

标签: python python-3.x while-loop turtle-graphics infinite

所以我想做的是显示: Alex与Alice的距离是:(我已经计算过的距离)

我需要将它显示在屏幕的顶部..永远,令人耳目一新,有点像PING统计数据在游戏中显示在那里..

我认为它会是一个while循环?我需要运行程序的其余部分,同时不断显示和刷新..

1 个答案:

答案 0 :(得分:0)

这是我对这个问题的极简主义方法(好吧,不完全是,我确实提出了几个细节):

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

当您拖动Alex或Alice时,屏幕顶部的中心距离会更新。这使用一个单独的,不可见的,固定的乌龟来处理显示,谁.undo()删除以前的值,.write()显示一个新的。CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "JavaClassName" AS public class JavaClassName{ public static String MyFunction(){ .....do something...... return "output"; } } 。 更新距离显示后让海龟转向彼此确保它们不会被困在隐形展示乌龟下面(即不可打开),所以它们甚至可以在文本上面张贴:

enter image description here