How do I repeat this turtle function in the window for a certain period of time? This code draws squares in a circular shape and I want it to restart once it is done and to do this for 3min and 30sec.
import turtle
import threading
def draw_square(some_shape):
for i in range (1,5):
some.forward(100)
some.right(90)
def draw_art():
window = turtle.Screen()
window.bgcolor("yellow")
sqr = turtle.Turtle()
sqr.shape("triangle")
sqr.color("purple")
sqr.speed(1.5)
for i in range(1,37):
draw_square(sqr)
sqr.right(10)
window.exitonclick()
def timed():
threading.Timer(208.0, printit).start()
draw_art()
timed()
Here is a way you could do it. In your timed function, you record the start time. You then calculate the end time by adding 3 minutes and 30 seconds to the start time.
In a loop, you can continue drawing while the current time is less than the end time.
from datetime import datetime, timedelta
def timed():
start_time = datetime.now()
end_time = start_time + timedelta(minutes=3, seconds=30)
while datetime.now() < end_time:
draw_art()
Here are some resources on the python datetime module, it's the go-to module for any kind of time or date operations!
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.