3

我有一个任务要求在赛道上设置两只乌龟(相同大小但不同的赛道)。我可以让它们移动,但只有当第一个移动了一半的轨道时,第二个才会移动。我不知道如何让乌龟同时移动。这是我的代码:

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

2 回答 2

3

有几种方法可以解决这个问题。

使用screen.ontimer()事件的一种方法(参见海龟文档)。这种方法很好,因为您可以将海龟调整为实际时钟时间,这可以在海龟事件循环中运行,以便其他事件也可以发生(如exitonclick())。

我在下面使用的方法是在 Python生成器中将海龟运动分解成微小的步骤,该生成器会在每次运动后产生。这允许我们在海龟之间交替。比赛发生在海龟事件循环之前,因此exitonclick()在比赛结束之前无效。

为了提供速度差异,我使用海龟绘制速度作为运动计算的一部分,所以如果你说它turtle1.speed("fast")会比turtle2.speed("slow"). 还有其他方法可以使用随机和/或变化的速度来解决这个问题。

我已将周界的提示更改为对话框,并进行了各种样式调整:

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

快乐赛车!

于 2016-10-14T22:21:32.053 回答
3

我让每只海龟重复移动一个微小的增量,因此它们之间的最大间隔只有 1 个像素:

# Setup 
import turtle

screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Turtle Movement")
screen.setup(width=500, height=500)

# Object 1 set up
obj1 = turtle.Turtle()
obj1.color("red") # Sets obj1's colour
obj1.speed(0) # The drawing speed will go as fast as it can if it is set to zero
obj1.penup() # Don't want it to draw a line as I goes to 0, -50
obj1.goto(0, -50) # goes to 0, -50
obj1.pendown() # Now we want it to draw

# Object 2 set up
obj2 = turtle.Turtle()
obj2.color("blue") # sets obj2's colour
obj2.speed(0) # The drawing speed will go as fast as it can if it is set to zero
obj2.penup() # Don't want it to draw a line as I goes to 0, -50
obj2.goto(0, 50) # goes to 0, 50
obj2.pendown() # Now we want it to draw

# Movement of objects
while True: # infinite loop
    obj1.forward(1) # Moves obj1 forwards by 1
    obj2.forward(1) # Moves obj2 forwards by 1

注意:“运动量”越小,例如forward(Movement amount is in here),运动越平滑,但您会失去速度。我鼓励您调整值并进行实验!

于 2020-02-25T08:36:09.620 回答