Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何让海龟一起移动?_Python 2.7_Turtle Graphics - Fatal编程技术网

Python 2.7 如何让海龟一起移动?

Python 2.7 如何让海龟一起移动?,python-2.7,turtle-graphics,Python 2.7,Turtle Graphics,这是我的代码(Python初学者,请携带任何不专业的代码),基本上我想要的是让两只海龟一起在同一个圆圈上移动(正如你可能猜到的,我的任务是模拟一艘宇宙飞船追逐国际空间站)。在我的代码中,第一只乌龟将绕圆圈移动,然后第二只乌龟: from turtle import * rocket=Turtle() ISS=Turtle() counter=1 title("ISS") screensize(750,750) ISS.hideturtle() rocket.hideturtle() ISS.pe

这是我的代码(Python初学者,请携带任何不专业的代码),基本上我想要的是让两只海龟一起在同一个圆圈上移动(正如你可能猜到的,我的任务是模拟一艘宇宙飞船追逐国际空间站)。在我的代码中,第一只乌龟将绕圆圈移动,然后第二只乌龟:

from turtle import *
rocket=Turtle()
ISS=Turtle()
counter=1
title("ISS")
screensize(750,750)
ISS.hideturtle()
rocket.hideturtle()
ISS.penup()
ISS.left(90)
ISS.fd(250)
ISS.left(90)
ISS.showturtle()
ISS.pendown()
rocket.penup()
rocket.fd(250)
rocket.left(90)
rocket.showturtle()
rocket.pendown()
while counter==1:
    ISS.speed(1)
    rocket.speed(2)
    ISS.circle(250)
    rocket.circle(250)
我的老师告诉我“线程”可以解决这个问题,但我不太明白。如果有人能帮我解决这个问题,我将不胜感激;)

有一个海龟不允许它多线程工作

虽然,你不必把海龟绕着整个圆圈移动,但你可以把它移动一部分。另外,我认为你误解了
speed
的作用。这只是海龟的速度

from turtle import *

def move(thing, distance):
    thing.circle(250, distance)

def main():
    rocket = Turtle()
    ISS = Turtle()
    rocket.speed(10)
    ISS.speed(10)
    counter = 1
    title("ISS")
    screensize(750, 750)
    ISS.hideturtle()
    rocket.hideturtle()
    ISS.penup()
    ISS.left(90)
    ISS.fd(250)
    ISS.left(90)
    ISS.showturtle()
    ISS.pendown()
    rocket.penup()
    rocket.fd(250)
    rocket.left(90)
    rocket.showturtle()
    rocket.pendown()

    while counter == 1:
        move(ISS, 3)
        move(rocket, 4)

if __name__ == '__main__':
    main()

我重复了移动物体的步骤,不管是国际空间站还是火箭,并使之成为一种功能。我把绘图速度提高到了10,因为我觉得它看起来更平滑了。国际空间站现在每一步的移动距离仅为火箭的3/4。

在海龟这样的事件驱动世界中,你不应该这样做:

counter = 1
...
while counter==1:
这有可能锁定事件。而且,没有办法完全退出这个程序。与其在
while
循环中移动不同的距离,不如在
ontimer()
事件中移动恒定的距离,但延迟不同(应适用于Python 2或3):

从海龟导入海龟,屏幕
半径=250
ISS#U延迟=100毫秒
火箭延迟=75毫秒
光标大小=20
def move_ISS():
国际空间站圆(半径1)
屏幕时间(移动时间、延迟时间)
def move_rocket():
火箭。圆(半径,1)
#一旦对接,火箭将减速至国际空间站的速度

如果你的老师知道如何使用线程,她比我聪明。建议使用更少的空格。你不需要在每一行之间都有一个空行。还有一种编码方法,当国际空间站和火箭相遇(在同一位置)时,破坏窗口并创建一个新的Tkinter窗口,谢谢:)这个答案中链接的错误报告被讨论得非常糟糕。请记住,标准库中的
turtle.py
代码依赖于使用Tk的Tkinter。Tk和其他许多典型的GUI工具包一样,不允许多个线程修改在单个线程(例如,主线程)中创建的GUI元素(在某种意义上,当程序运行时,它会导致错误,不一定总是这样)。但是您可以在每个线程中运行多个Tcl解释器,并安排它们进行通信。也就是说,这里的问题不需要任何形式的线程。
from turtle import Turtle, Screen

RADIUS = 250
ISS_DELAY = 100  # milliseconds
ROCKET_DELAY = 75  # milliseconds
CURSOR_SIZE = 20

def move_ISS():
    ISS.circle(RADIUS, 1)

    screen.ontimer(move_ISS, ISS_DELAY)

def move_rocket():
    rocket.circle(RADIUS, 1)

    # rocket slows to ISS' speed once docked
    delay = ISS_DELAY if rocket.distance(ISS) <= CURSOR_SIZE else ROCKET_DELAY

    screen.ontimer(move_rocket, delay)

screen = Screen()
screen.title("ISS")
screen.setup(750, 750)

ISS = Turtle("square", visible=False)
ISS.speed('fastest')
ISS.penup()
ISS.left(180)
ISS.sety(RADIUS)
ISS.showturtle()
ISS.pendown()

rocket = Turtle("triangle", visible=False)
rocket.speed('fastest')
rocket.penup()
rocket.left(90)
rocket.setx(RADIUS)
rocket.showturtle()
rocket.pendown()

move_ISS()
move_rocket()

screen.exitonclick()
from threading import Thread, active_count
from turtle import Turtle, Screen
from queue import Queue  # use for thread-safe communications
from time import sleep

RADIUS = 250
ISS_DISTANCE = 3
ROCKET_DISTANCE = 4
CURSOR_SIZE = 20
QUEUE_SIZE = 1

def move_ISS(turtle):
    while True:
        actions.put((turtle.circle, RADIUS, ISS_DISTANCE))
        sleep(0.1)

def move_rocket(turtle):
    while True:
        # rocket slows to ISS' speed once docked
        distance = ISS_DISTANCE if rocket.distance(ISS) <= CURSOR_SIZE else ROCKET_DISTANCE

        actions.put((turtle.circle, RADIUS, distance))
        sleep(0.1)

def process_queue():
    while not actions.empty():
        action, *arguments = actions.get()
        action(*arguments)

    if active_count() > 1:
        screen.ontimer(process_queue, 100)

actions = Queue(QUEUE_SIZE)

screen = Screen()
screen.title("ISS")
screen.setup(750, 750)

ISS = Turtle("square", visible=False)
ISS.speed('fastest')
ISS.penup()
ISS.left(180)
ISS.sety(RADIUS)
ISS.showturtle()
ISS.pendown()

ISS_thread = Thread(target=move_ISS, args=[ISS], daemon=True)

rocket = Turtle("triangle", visible=False)
rocket.speed('fastest')
rocket.penup()
rocket.left(90)
rocket.setx(RADIUS)
rocket.showturtle()
rocket.pendown()

rocket_thread = Thread(target=move_rocket, args=[rocket], daemon=True)

ISS_thread.start()
rocket_thread.start()

process_queue()

screen.exitonclick()