2017-10-18 136 views
0

所以我想做的事,就是顯示: 亞歷克斯來自Alice的距離是:(距離我已經計算)(Turtle/Python3)如何創建一個獨立工作在龜上的while循環?

我需要這顯示在屏幕上方..永遠,提神種,就像PING統計在遊戲節目上面那樣..

我假設它會是一個while循環?我需要的程序運行的剩餘部分,而這是始終顯示爽口..

+0

歡迎堆棧溢出。你已經嘗試過這麼做了嗎?請回顧[預計需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)。堆棧溢出不是一種編碼服務。您需要研究您的問題,並嘗試在發佈之前親自編寫代碼。如果遇到某些特定問題,請返回幷包含[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)以及您嘗試的內容摘要,以便我們提供幫助。 – Sand

回答

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

當你拖動亞歷克斯或愛麗絲,他們的中心到屏幕頂部的中心距離更新。這使用一個獨立的,不可見的固定烏龜來處理顯示屏,.undo()刪除以前的值,.write()顯示新的值。使龜向着對方後的距離顯示更新可確保它們不會被困在無形顯示龜下方(即undragable),所以他們甚至可以在文本頂上postitioned:

enter image description here

相關問題