[英]Python Turtle Graphics - Bring A Turtle To The Front
我在對Make one turtle object always above another的回應中簡要討論了這一點,這種簡單情況的經驗法則是:
最后到達的在最上面
由於 Python turtle 沒有優化零運動,一個簡單的方法是將你想要的 turtle 移動一個零量:
import turtle
def tofront(t):
t.forward(0)
gold = turtle.Turtle("square")
gold.turtlesize(5)
gold.color("gold")
gold.setx(-10)
blue = turtle.Turtle("square")
blue.turtlesize(5)
blue.color("blue")
blue.setx(10)
turtle.onscreenclick(lambda x, y: tofront(gold))
turtle.done()
藍龜與金龜重疊。 單擊任意位置,情況將相反。
雖然@Bally 的解決方案有效,但我的問題是每次調整分層時它都會創建一個新的海龜。 而這些海龜不會 go 離開。 觀看turtle.turtles()
成長。 即使是我的解決方案也會在 turtle 撤消緩沖區中留下足跡,但我不得不相信它消耗的資源更少。
只需將其添加到 turtle.py 文件中
def tofront(self, tobring):
newfront = tobring.clone()
tobring.ht()
return newfront
工作正常,所以方法 - 會返回給你新的海龜克隆,在所有其他海龜之上。 參數 tobring - 這是你當前的烏龜(你想放在前面的那個)
你可以在你的程序中使用這個 def 或者將它放入 turtle.py 文件中,如果是這樣,不要忘記將它添加到命令列表中, - _tg_turtle_functions
_tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'tofront', 'color',
我是在克隆命令之后做的
希望對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.