How to move multiple turtles at the same time in python?

情到浓时终转凉″ 提交于 2019-12-24 07:13:02

问题


Hi I have an assignment which is asked to set two turtles in a race track(same size but separate track). I can able to make them move but the second one move only when the first one moved a half of the track. I'm not sure how to make the turtles move at the same time. Here is my code, please help me if you have any idea about it. Thank you!

import turtle
import random
import time


wn = turtle.Screen()
wn.bgcolor("lightgreen")

t = turtle.Turtle()
t.shape('turtle')
t.color('red')

t2 = turtle.Turtle()
t2.shape('turtle')
t2.color('blue')

#user input function

p = float(input('please insert the perimeter:'))

#set the track
def drawTrack(p,r):
    shortside = (p/2.0)/(r+1)
    longside = r*shortside
    turtle.setup((shortside*2)+60, longside +40)
    t.penup()
    t2.penup()
    t.setposition(-shortside-10, -longside/2)
    t2.setposition(10, -longside/2)   
    for i in range (2):
        #first track
        t.speed(1)
        t.pendown()
        t.forward(shortside)
        t.left(90)
        t.forward(longside)
        t.left(90)

        #second track
        t2.speed(1)
        t2.pendown()
        t2.forward(shortside)
        t2.left(90)
        t2.forward(longside)
        t2.left(90) 

drawTrack(p,2)

wn.exitonclick()

回答1:


There are a couple of ways you can go about this.

One approach to use the screen.ontimer() event (see the turtle documentation). This approach is nice as you can adjust the turtles to actual clock time and this can run within the turtle event loop so that other events can also take place (like exitonclick()).

The approach I used below is to break up turtle motion into tiny steps inside a Python generator which yields after every bit of motion. This allows us to alternate between the turtles. The race takes place before the turtle event loop so exitonclick() is invalid until the race is over.

To provide a differential in speed, I've used the turtle drawing speed as part of the motion calculation so if you say turtle1.speed("fast") it will move fast compared to a turtle2.speed("slow"). There are other ways to go about this using random and/or varying speeds.

I've changed the prompt for the perimeter to be a dialog box and made various style tweaks:

from turtle import Turtle, Screen

screen = Screen()
screen.bgcolor("lightgreen")

turtle1 = Turtle(shape='turtle')
turtle1.color('red')
turtle1.speed("slow")  # = 3
turtle1.penup()

turtle2 = Turtle(shape='turtle')
turtle2.color('blue')
turtle2.speed(4)  # "slow" (3) < 4 < "normal" (6)
turtle2.penup()

# user input function

perimeter = screen.numinput("Track Perimeter", "Please enter the perimeter:", default=2000, minval=500, maxval=3000)

def full_track_crawl(turtle, shortside, longside):
    speed = turtle.speed()
    turtle.pendown()

    for j in range (2):
        for i in range(0, int(shortside), speed):
            turtle.forward(speed)
            yield(0)
        turtle.left(90)
        for i in range(0, int(longside), speed):
            turtle.forward(speed)
            yield(0)
        turtle.left(90)

    turtle.penup()

# set the track
def drawTrack(perimeter, ratio):
    shortside = (perimeter / 2.0) / (ratio + 1)
    longside = ratio * shortside

    screen.setup(shortside * 2 + 60, longside + 40)

    turtle1.setposition(-shortside - 10, -longside / 2)
    turtle2.setposition(10, -longside / 2)   

    generator1 = full_track_crawl(turtle1, shortside, longside)
    generator2 = full_track_crawl(turtle2, shortside, longside)

    while (next(generator1, 1) + next(generator2, 1) < 2):
        pass

drawTrack(perimeter, 2)

screen.exitonclick()

Happy racing!



来源:https://stackoverflow.com/questions/40050438/how-to-move-multiple-turtles-at-the-same-time-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!