[Python] Python Multiple Turtles Moving (seemingly) Simultaneously

Discussão em 'Python' iniciado por Stack, Janeiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on testing something for my teacher, he wants to see how the program below could possibly run faster if we simulated the simultaneous (i know it can't be perfectly simultaneous, this is just an experiment for the sake of learning/practicing) movement of multiple turtles. I've tried using modules like multiprocessing, threading, and even some crazy stupid attempt to time and delay (I'm in high school and I just learned about classes in python because of a previous question I asked I think last week).

    So after many failed attempts I'm asking if someone has a few ideas of what else to try, or a direction to go in to simulate simultaneous movement of the turtles

    import turtle
    from turtle import 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()

    Continue reading...

Compartilhe esta Página