【问题标题】:Having trouble timing turtle movement无法计时乌龟运动
【发布时间】:2018-01-25 04:13:30
【问题描述】:

我编写了以下代码,它在两个随机位置创建扩展方块。我想写一个函数f(squares, seconds),这样如果用户输入f(5,10),每10秒就会开始形成一个新的方块,直到形成5。 p>

我似乎找不到任何东西可以让我在一个新的方格还在形成的时候开始一个新的方格。我可以同时制作两个表格,如下面的代码所示,或者完成一个表格,然后另一个开始形成。帮忙?

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)

【问题讨论】:

    标签: python python-3.x time timer turtle-graphics


    【解决方案1】:

    您想要的功能需要独立的执行线程。您需要使用多线程 packagetutorial

    你会想要这样的逻辑:

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2018-03-31
      • 2017-08-09
      • 2014-12-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      相关资源
      最近更新 更多