如何在 turtle 模块中使用多线程
How to use multithreading with turtle module
这是我最近一直在研究的作品。该项目的任务是创建三个符号作为三角形的顶点。这些符号将始终以相同的速度相互移动(即 1-->2、2-->3、3-->1)。随着符号开始同时移动,最终的数字必须是这样的
这是我的代码。我正在使用多线程同时调用所有三种方法,但该程序似乎无法正常工作。这些方法一个接一个地单独执行,这是不应该发生的。
import turtle
import time
import threading
class TurObj:
def __init__(self, x, y, color, speed):
self.t = turtle.Pen()
self.t.ht()
self.t.shape('circle')
self.t.fillcolor(color)
self.t.speed(speed)
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
self.t.st()
def get_loc(self):
cor = [self.t.xcor(), self.t.ycor()]
return cor
def move_to(self, n):
# self.t.setx(n.get_loc().__getitem__(0))
# self.t.sety(n.get_loc().__getitem__(1))
self.t.goto(n.get_loc().__getitem__(0), n.get_loc().__getitem__(1))
t1 = TurObj(300, 200, 'red', 5)
t2 = TurObj(-300, 200, 'yellow', 5)
t3 = TurObj(0, -200, 'blue', 5)
p1 = threading.Thread(target=t1.move_to(t2))
p2 = threading.Thread(target=t2.move_to(t3))
p3 = threading.Thread(target=t3.move_to(t1))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
time.sleep(3)
任何帮助都会很棒...
我总是建议在考虑线程之前尝试 turtle 自己的 ontimer()
事件。您的代码似乎还使用 goto()
跳得太远,而不是使用 setheading(towards())
和 forward()
组合采取小步骤。最后,您的起始位置似乎不对称。以下是我针对这些问题所做的返工:
from turtle import Turtle, Screen
class TurtleObject:
def __init__(self, position, color, speed):
self.t = Turtle(shape='circle', visible=False)
self.t.fillcolor(color)
self.t.speed(speed)
self.t.penup()
self.t.goto(position)
self.t.pendown()
self.t.showturtle()
def get_turtle(self):
return self.t
def on_top_of(self, n):
other = n.get_turtle()
return abs(self.t.xcor() - other.xcor()) < 5 and abs(self.t.ycor() - other.ycor()) < 5
def move_towards(self, n):
self.t.setheading(self.t.towards(n.get_turtle()))
self.t.forward(1)
if not self.on_top_of(n):
screen.ontimer(lambda: self.move_towards(n), 100)
t1 = TurtleObject((300, 200), 'red', "fast")
t2 = TurtleObject((-300, 200), 'yellow', "fast")
t3 = TurtleObject((0, -320), 'blue', "fast")
screen = Screen()
screen.ontimer(lambda: t1.move_towards(t2), 100)
screen.ontimer(lambda: t2.move_towards(t3), 100)
screen.ontimer(lambda: t3.move_towards(t1), 100)
screen.exitonclick()
这是我最近一直在研究的作品。该项目的任务是创建三个符号作为三角形的顶点。这些符号将始终以相同的速度相互移动(即 1-->2、2-->3、3-->1)。随着符号开始同时移动,最终的数字必须是这样的
这是我的代码。我正在使用多线程同时调用所有三种方法,但该程序似乎无法正常工作。这些方法一个接一个地单独执行,这是不应该发生的。
import turtle
import time
import threading
class TurObj:
def __init__(self, x, y, color, speed):
self.t = turtle.Pen()
self.t.ht()
self.t.shape('circle')
self.t.fillcolor(color)
self.t.speed(speed)
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
self.t.st()
def get_loc(self):
cor = [self.t.xcor(), self.t.ycor()]
return cor
def move_to(self, n):
# self.t.setx(n.get_loc().__getitem__(0))
# self.t.sety(n.get_loc().__getitem__(1))
self.t.goto(n.get_loc().__getitem__(0), n.get_loc().__getitem__(1))
t1 = TurObj(300, 200, 'red', 5)
t2 = TurObj(-300, 200, 'yellow', 5)
t3 = TurObj(0, -200, 'blue', 5)
p1 = threading.Thread(target=t1.move_to(t2))
p2 = threading.Thread(target=t2.move_to(t3))
p3 = threading.Thread(target=t3.move_to(t1))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
time.sleep(3)
任何帮助都会很棒...
我总是建议在考虑线程之前尝试 turtle 自己的 ontimer()
事件。您的代码似乎还使用 goto()
跳得太远,而不是使用 setheading(towards())
和 forward()
组合采取小步骤。最后,您的起始位置似乎不对称。以下是我针对这些问题所做的返工:
from turtle import Turtle, Screen
class TurtleObject:
def __init__(self, position, color, speed):
self.t = Turtle(shape='circle', visible=False)
self.t.fillcolor(color)
self.t.speed(speed)
self.t.penup()
self.t.goto(position)
self.t.pendown()
self.t.showturtle()
def get_turtle(self):
return self.t
def on_top_of(self, n):
other = n.get_turtle()
return abs(self.t.xcor() - other.xcor()) < 5 and abs(self.t.ycor() - other.ycor()) < 5
def move_towards(self, n):
self.t.setheading(self.t.towards(n.get_turtle()))
self.t.forward(1)
if not self.on_top_of(n):
screen.ontimer(lambda: self.move_towards(n), 100)
t1 = TurtleObject((300, 200), 'red', "fast")
t2 = TurtleObject((-300, 200), 'yellow', "fast")
t3 = TurtleObject((0, -320), 'blue', "fast")
screen = Screen()
screen.ontimer(lambda: t1.move_towards(t2), 100)
screen.ontimer(lambda: t2.move_towards(t3), 100)
screen.ontimer(lambda: t3.move_towards(t1), 100)
screen.exitonclick()