【问题标题】:How to use multithreading with turtle module如何在 turtle 模块中使用多线程
【发布时间】:2017-03-10 17:21:18
【问题描述】:

这是我最近一直在研究的作品。该项目的任务是创建三个符号作为三角形的顶点。符号将始终以相等的速度相互移动(即 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)

任何帮助都会很棒...

【问题讨论】:

    标签: multithreading python-3.x python-multithreading turtle-graphics


    【解决方案1】:

    我总是建议在考虑线程之前尝试 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多