嗨我有一项任务,要求在赛道上设置两只海龟(相同大小但是单独的赛道)。我能够让他们移动,但第二个移动只有当第一个移动了一半的轨道。我不知道如何让乌龟同时移动。这是我的代码,如果您对此有任何想法,请帮助我。谢谢!
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()
答案 0 :(得分:2)
有几种方法可以解决这个问题。
使用screen.ontimer()
事件的一种方法(请参阅海龟文档)。这种方法很好,因为你可以将乌龟调整到实际的时钟时间,这可以在乌龟事件循环中运行,这样也可以发生其他事件(如exitonclick()
)。
我在下面使用的方法是将Python运动分解成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()
快乐的比赛!
答案 1 :(得分:2)
我让海龟每只重复微小的增量,因此它们彼此之间的最大间隔只有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
# Notes
# -----
# The smaller the 'Movement amount' is,
# e.g forward(Movement amount is in here)
# the smoother the movement gets, but you will lose speed.
# I encourage you to tweak the values and experiment!
希望有帮助!