简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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