gpt4 book ai didi

Python 多只 turtle (看似)同时移动

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:25 27 4
gpt4 key购买 nike

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

进口 turtle 从 turtle 导入 turtle

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()

谢谢你的时间

最佳答案

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

下面是一对 turtle 的例子,它们会同时在屏幕上生成一个随机图案:

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()

这里的技巧是要注意计算每只 turtle 的每个 新位置,然后通知整个屏幕进行更新,然后才继续下一步。每一步都必须尽可能细化。

关于Python 多只 turtle (看似)同时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44056498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com