0

这是我的代码(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)

我的老师告诉我“线程”可以解决这个问题,但我不太明白。如果有人可以帮助我解决这个问题,将不胜感激;)

4

2 回答 2

1

有一个乌龟限制,不允许它在多线程中工作。

虽然,您不必将乌龟移动整个圆圈,您可以将其移动一部分。另外,我认为您误解了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。

于 2013-01-24T04:14:04.527 回答
0

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

counter = 1
...
while counter==1:

这有可能锁定事件。此外,没有办法彻底退出该程序。不是在循环中移动不同的距离while,而是移动一个恒定的距离,但在一个事件中具有不同的延迟ontimer()(应该与 Python 2 或 3 一起使用):

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

您可以随时单击窗口上的任意位置,以干净地退出事件处理程序的程序。我加入了一些逻辑,让太空船在它们停靠而不是飞过国际空间站后就粘在国际空间站上。

这也可以通过线程来完成,但所有图形操作都必须通过主线程进行引导以避免 Tkinter 错误(仅限 Python3 的解决方案):

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()
于 2018-05-18T23:14:29.980 回答