简体   繁体   中英

Draw faster circles with Python turtle

I have an exercise wherein I have to draw a lot of circles with Python turtle. I have set speed(0) and I am using:

from turtle import*
speed(0)
i=0
while i < 360:
    forward(1)
    left(1)
    i+=1

to draw circles. It takes so long. Is there any faster way?

Have you tried turtle.delay() or turtle.tracer() ? See documentation here and here . These set options for screen refreshing which is responsible for most of the delays.

You could draw fewer segments, so rather than 360 you go for 120:

while i < 360:
    forward(3)
    left(3)
    i+=3

That will make your circle less smooth, but three times faster to draw.

circle() 方法可能不会更快,但可能更容易管理: turtle.circle()

Turtle has a function for drawing circles and the speed of that is much faster than going in a circle one step at a time.

 import turtle
 tina=turtle.Turtle()
 
 tina.speed(0)
 tina.circle(70)

It will look like this

在此处输入图像描述

Use Multithread to draw two semi circles simultaneously. Initially the turtle will be at (0,0) so just clone the turtle and make them both face in opposite direction by 180° then draw semicircles. The code is below:

from threading import Thread
import turtle
t = turtle.Turtle()
t.speed(0)
def semi1(r):
   r.circle(50,180)
def semi2(t):
   t.circle(50,180)

r = t.clone()
r.rt(180)

a = Thread(target=semi1).start()
b = Thread(target=semi2).start()

This may draw the circle fast.

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