这是我的代码(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)
我的老师告诉我,“穿线”对此有用,但我不太明白。如果有人可以帮我解决这个问题,我将不胜感激;)
答案 0 :(得分:1)
有一只乌龟limitation不允许它使用多线程。
虽然,您不必将乌龟移动到整个圆圈,但您可以将它移动到一半。另外,我认为你误解了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,就像每一步火箭一样。
答案 1 :(得分:0)
在像乌龟这样的事件驱动的世界中,你不应该这样做:
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()