简体   繁体   English

乌龟图形乌龟减速

[英]turtle graphics Turtle slows down

I set the turtle to fastest and when I ran the first loop alone it was fine but as I added more it became comparably to when it was just executing first loop alone.我将乌龟设置为最快,当我单独运行第一个循环时它很好,但是随着我添加更多它变得与单独执行第一个循环时相当。 I don't know if this is just because of the complexity of the drawing but it takes a decently long amount of time to complete the shape.我不知道这是否只是因为绘图的复杂性,但完成形状需要相当长的时间。 Is there anthing I can do to fix this?我能做些什么来解决这个问题吗?

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

My analysis is that your filling, ie turtle.begin_fill() and turtle.end_fill() , is slowing down the code 3X to no real effect.我的分析是您的填充,即turtle.begin_fill()turtle.end_fill()将代码减慢了3 倍,但没有实际效果。 One of these images is with fill, one is without :这些图像之一填充,一个是没有

在此处输入图片说明

If you can't appreciate the difference (even at full size) then the fill is probably just a waste of time.如果您无法体会差异(即使是全尺寸),那么填充可能只是浪费时间。 If you just want the final image, and don't care about watching it being drawn, then for more performance, I suggest something like:如果您只想要最终图像,而不关心观看它的绘制,那么为了获得更高的性能,我建议如下:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM