如何同时移动多只乌龟?

How to move multiple turtles at the same time?(如何同时移动多只乌龟?)
本文介绍了如何同时移动多只乌龟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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个像素:

# 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), 动作越流畅,速度就会越慢。 我鼓励你调整价值观,尝试一下!

这篇关于如何同时移动多只乌龟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)