我编写了以下代码,该代码在两个随机位置创建了扩大的正方形。我想编写一个函数f(squares,seconds),以便如果用户输入f(5,10),则每10秒将开始形成一个新的正方形,直到形成5个。
我似乎找不到任何可以让我在仍在形成新方块的情况下开始新方块的东西。我可以同时制作两个表单(如下面的代码所示),或者先完成一个表单,然后再开始另一个表单。救命?
import sys
sys.setExecutionLimit(1000000)
import turtle
import random
wn = turtle.Screen()
#Creates alex the turtle
alex = turtle.Turtle()
alex.color('blue')
alex.pensize(3)
alex.ht()
alex.penup()
#creates bob the turtle
bob = turtle.Turtle()
bob.color('blue')
bob.pensize(3)
bob.ht()
bob.penup()
#Sets variables so that alex starts in a random location
a=random.randrange(360)
b=random.randrange(360)
x=random.randrange(50,150)
y=random.randrange(50,150)
#Sets variables so that bob starts in a random location
l=random.randrange(360)
m=random.randrange(360)
n=random.randrange(50,150)
o=random.randrange(50,150)
#Moves alex to his random starting location
alex.speed(100)
alex.left(a)
alex.forward(x)
alex.left(b)
alex.forward(y)
alex.pendown()
#Moves bob to his random starting location
bob.speed(100)
bob.left(l)
bob.forward(n)
bob.left(m)
bob.forward(o)
bob.pendown()
#Draws the 2 squares
for i in range(1,500):
alex.forward(i)
alex.left(90)
bob.forward(i)
bob.left(90)
最佳答案
所需的功能需要独立的执行线程。您需要使用多线程package和tutorial
您将需要这样的逻辑:
import time
import threading
def draw_square():
# Draw a square in a random place
length = random.randrange(360)
width = random.randrange(360)
x_pos = random.randrange(50,150)
y_pos = random.randrange(50,150)
# Continue with your square-drawing logic;
# you already know how to do this.
while True:
threading.thread(draw_square)
time.sleep(10)
关于python - 计时 turtle 运动有麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45723373/