[英]turtle graphics Turtle slows down
我將烏龜設置為最快,當我單獨運行第一個循環時它很好,但是隨着我添加更多它變得與單獨執行第一個循環時相當。 我不知道這是否只是因為繪圖的復雜性,但完成形狀需要相當長的時間。 我能做些什么來解決這個問題嗎?
import turtle
turtle.bgcolor("Red")
turtle.color("Yellow", "Pink")
turtle.shape("turtle")
turtle.begin_fill()
turtle.speed("fastest")
while True:
turtle.forward(300)
turtle.left(179)
turtle.circle(20)
if abs(turtle.pos()) < 1:
break
turtle.setheading(270)
while True:
turtle.forward(300)
turtle.left(179)
turtle.circle(20)
if abs(turtle.pos()) < 1:
break
turtle.setheading(180)
while True:
turtle.forward(300)
turtle.left(179)
turtle.circle(20)
if abs(turtle.pos()) < 1:
break
turtle.setheading(90)
while True:
turtle.forward(300)
turtle.left(179)
turtle.circle(20)
if abs(turtle.pos()) < 1:
break
turtle.end_fill()
turtle.getscreen()._root.mainloop()
我的分析是您的填充,即turtle.begin_fill()
和turtle.end_fill()
將代碼減慢了3 倍,但沒有實際效果。 這些圖像之一是填充,一個是沒有:
如果您無法體會差異(即使是全尺寸),那么填充可能只是浪費時間。 如果您只想要最終圖像,而不關心觀看它的繪制,那么為了獲得更高的性能,我建議如下:
from turtle import Screen, Turtle
screen = Screen()
screen.bgcolor("Red")
screen.tracer(False)
turtle = Turtle(visible=False)
turtle.color("Yellow", "Pink")
for heading in range(0, 360, 90):
turtle.setheading(heading)
turtle.begin_fill()
while True:
turtle.forward(300)
turtle.left(179)
turtle.circle(20)
if abs(turtle.pos()) < 1:
break
turtle.end_fill()
screen.tracer(True)
screen.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.