【问题标题】:Python Multiple Turtles Moving (seemingly) SimultaneouslyPython 多只海龟同时移动(看似)
【发布时间】:2017-05-18 19:39:36
【问题描述】:

我正在为我的老师测试一些东西,他想看看如果我们同时模拟下面的程序如何可能运行得更快(我知道它不可能完全同时,这只是一个实验学习/练习)多只海龟的运动。我尝试过使用多处理、线程等模块,甚至是一些疯狂愚蠢的时间和延迟尝试(我在高中,我刚刚了解了 python 中的类,因为我上周问过一个问题) 因此,经过多次失败的尝试后,我想问是否有人对其他尝试有什么想法,或者有一个方向来模拟海龟的同时运动

进口乌龟 从乌龟进口乌龟

turtle.getscreen().delay(0)
class MyTurtle(Turtle):
    def petal(self):
        for i in range(90):
            self.fd(1)
            self.rt(1)
        self.rt(90)
        for i in range(90):
            self.fd(1)
            self.rt(1)

    def stem(self):
        self.pencolor('green')
        self.fd(250)

    def flowerhead(self):
        for i in range(9):
          self.pencolor('red')
          self.begin_fill()
          self.petal()
          self.lt(230)
          self.end_fill()

    def stempetal(self):
        self.seth(90)
        self.rt(15)
        self.fillcolor('green')
        self.begin_fill()
        self.petal()
        self.end_fill()



tony = MyTurtle(shape='turtle')
todd = MyTurtle(shape='turtle')
tina = MyTurtle(shape='turtle')
tiny = MyTurtle(shape='turtle')
tweeny = MyTurtle(shape='turtle')


def flower1():
    todd.speed('fastest')
    todd.fillcolor('blue')
    todd.flowerhead()
    todd.seth(270)
    todd.stem()
    todd.stempetal()

def flower2():
    tony.speed('fastest')
    tony.setpos(80, -15)
    tony.pencolor('green')
    tony.goto(0, -200)
    tony.fillcolor('purple')
    tony.goto(80,-15)
    tony.rt(40)
    tony.flowerhead()


def flower3():
    tina.speed('fastest')
    tina.setpos(-80, -15)
    tina.pencolor('green')
    tina.goto(0, -200)
    tina.fillcolor('teal')
    tina.goto(-80,-15)
    tina.lt(40)
    tina.flowerhead()


def flower4():
    tiny.speed('fastest')
    tiny.setpos(160, -25)
    tiny.pencolor('green')
    tiny.goto(0, -200)
    tiny.fillcolor('black')
    tiny.goto(160, -25)
    tiny.flowerhead()


def flower5():
    tweeny.speed('fastest')
    tweeny.setpos(-160, -25)
    tweeny.pencolor('green')
    tweeny.goto(0, -200)
    tweeny.fillcolor('pink')
    tweeny.goto(-160,-25)
    tweeny.lt(40)
    tweeny.flowerhead()


flower2()
tony.hideturtle()
flower4()
tiny.hideturtle()
flower3()
tina.hideturtle()
flower5()
tweeny.hideturtle()
flower1()
todd.hideturtle()

感谢您的宝贵时间

【问题讨论】:

    标签: python turtle-graphics simultaneous


    【解决方案1】:

    解决方案是disable updating the position of each turtle,然后在计算出新位置后强制整个屏幕更新。

    import turtle
    
    # our two turtle instances
    first, second = turtle.Turtle(), turtle.Turtle()
    
    first.tracer(False)  # disable updating view on screen for this turtle!
    second.tracer(False)
    
    # make one move - note this will not appear on screen.
    first.forward(50)
    second.left(20)
    
    # when you are ready to see the whole screen update
    turtle.update()
    

    要做你想做的事,你必须基本上做到每一个新动作在turtle.update()之前完成。您不能像现在这样将其保持为串行执行 - 换句话说,您不能依次运行flower1,然后是flower2

    下面是一对海龟同时在屏幕上生成随机图案的例子:

    import turtle
    import random
    
    # our two turtle instances
    turtles = [turtle.Turtle(), turtle.Turtle()]
    for turtle_object in turtles:
        turtle_object.tracer(False)
    
    for _ in range(10000):  # make ten thousand moves.
    
        for t in turtles:
            # list the possible moves available
            possible_moves = [t.forward, t.back, t.right, t.left]
            # give it a random value
            random_value = random.randint(0, 100)
            # make a random move 
            random.choice(possible_moves)(random_value)
    
        # update the whole screen now that the new positions have been calculated
        turtle.update()
    

    这里的诀窍是要注意每个每只海龟的新位置都会被计算出来,然后整个屏幕都会被告知要更新,然后才可以继续下一步。每一步都必须尽可能细化。

    【讨论】:

    • 此代码在 Python 3 下无法正常工作。问题在于 tracer() 方法实际上是屏幕的方法,而不是单个海龟。在 Python 2 中,turtle tracer() 方法只是在屏幕实例上重新调用它,因此对多个海龟执行此操作不会增加任何内容。在 Python 3 中,该方法的海龟版本已消失,您只需直接在屏幕实例上调用它即可。 IE。一个微不足道的修复,使相同的代码在其中任何一个中工作。
    【解决方案2】:

    您要求两个不同的东西,“跑得更快”和“模拟同时移动”。我相信我们可以(分别)做这两个,但我相信 tracer()update() 在这种情况下是答案,因为它们只是一个创可贴来覆盖真正的问题。

    想看看下面的程序如何可能运行得更快

    如果您希望它运行得更快,请修复 瓶颈,即 petal() 函数。将其替换为使用更快的乌龟内置 circle() 函数的东西。例如:

    def petal(self):
        self.circle(-60, 90)
        self.rt(90)
        self.circle(-60, 90)
    

    这可以将您的代码速度提高 25 倍,而无需进行其他更改。

    模拟海龟的同时运动

    这可以通过turtle 自己的ontimer() 事件处理程序和一些仔细的编程来完成。令人惊讶的是,我们使用了您原来的 petal() 逻辑,因为它将图形分解为微小的步骤,在这些步骤之间我们可以将处理关闭到另一个定时事件:

    from random import randint
    from turtle import Turtle, Screen
    
    class MyTurtle(Turtle):
    
        def petals(self, size=30, count=8, speed=100):
            if size == 30:
                self.begin_fill()
    
            if size > 0:  # drawing leading edge of petal
                self.fd(3)
                self.rt(3)
    
                screen.ontimer(lambda: self.petals(size - 1, count, speed), speed)
                return
    
            if size == 0:  # switch to other edge of petal
                self.rt(90)
    
            if size > -30:  # drawing trailing edge of petal
                self.fd(3)
                self.rt(3)
    
                screen.ontimer(lambda: self.petals(size - 1, count, speed), speed)
                return
    
            self.end_fill()  # finish this petal
            self.lt(230) # prepare for the next petal
    
            if count > 0:  # drawing the next petal
                screen.ontimer(lambda: self.petals(count=count - 1, speed=speed), speed)
                return
    
            self.hideturtle()  # finished drawing
    
        def stem(self):
            self.pencolor('green')
            self.fd(250)
    
        def flowerhead(self):
            self.pencolor('red')
    
            self.petals(speed=randint(50, 250))
    
    def flower2():
        tony.color('green', 'purple')
        tony.penup()
        tony.goto(0, -200)
        tony.pendown()
        tony.showturtle()
        tony.goto(80, -15)
        tony.rt(40)
        tony.flowerhead()
    
    def flower3():
        tina.color('green', 'turquoise')
        tina.penup()
        tina.goto(0, -200)
        tina.pendown()
        tina.showturtle()
        tina.goto(-80, -15)
        tina.lt(40)
        tina.flowerhead()
    
    def flower5():
        tweeny.color('green', 'pink')
        tweeny.penup()
        tweeny.goto(0, -200)
        tweeny.pendown()
        tweeny.showturtle()
        tweeny.goto(-160, -25)
        tweeny.lt(40)
        tweeny.flowerhead()
    
    tony = MyTurtle(shape='turtle', visible=False)
    tina = MyTurtle(shape='turtle', visible=False)
    tweeny = MyTurtle(shape='turtle', visible=False)
    
    screen = Screen()
    
    screen.ontimer(flower2, 100)
    screen.ontimer(flower3, 120)
    screen.ontimer(flower5, 100)
    
    screen.mainloop()
    

    运行图像

    它不会更快,因为它只是一个模拟。 (嗯,它确实会快一点,因为我让花瓣画得稍微粗糙一些以换取速度。)如果你仔细观察,你会看到海龟(故意)以自己的速度移动。

    【讨论】:

    • 这正是我要找的东西,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多