Python多只乌龟同时移动

2024-09-27 19:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的老师测试一些东西,他想看看如果我们模拟多只海龟同时(我知道不可能完全同步,这只是为了学习/练习)的运动,下面的程序如何可能运行得更快。我尝试过使用多处理、线程等模块,甚至一些疯狂的愚蠢尝试来延迟时间(我在高中,我刚学过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()

谢谢你抽出时间


Tags: selfdefgreentinyturtlerttonygoto
2条回答

解决方案是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()

要想做你想做的事情,你必须让它在一个^{之前完成每一个新操作。您不能像现在这样将其保持为串行执行-换句话说,您不能依次运行flower1,然后flower2。在

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

^{pr2}$

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

你要求两个不同的东西,“跑得更快”和“模拟同步运动”。我相信我们可以(分开)做这两件事,但我不相信tracer()和{}是在这种情况下的答案,因为它们只是一个创可贴来掩盖真正的问题。在

wants to see how the program below could possibly run faster

如果您希望它运行得更快,请修复瓶颈问题,即petal()函数。用turtle内置的circle()函数来替换它,它更快。例如:

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

这样可以将代码速度提高25倍,而无需其他更改。在

simulate simultaneous movement of the turtles

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

^{pr2}$

运行映像

enter image description here

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

相关问题 更多 >

    热门问题