如何使用乌龟在python中同时绘制项目? [英] How to make items draw at the same time in python using turtle?

查看:37
本文介绍了如何使用乌龟在python中同时绘制项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业,我必须让四只不同的海龟像行星一样围绕太阳移动.我已经写好了,这只是让海龟同时绘制的问题.我想知道是否有一种相对简单的方法可以让它们在同一时间(在合理范围内)开始?无论如何,这是代码:

I have a homework assignment, and I have to make four different turtles move like they are planets around the sun. I have it all written, its just a matter of making the turtles draw at the same time. I was wondering if there was a relatively easy way to make them start around the same time (within reason)? Anyway, here's the code:

def planets():
    """simulates motion of Mercury, Venus, Earth, and Mars"""
    import turtle

    mercury = turtle.Turtle()
    venus = turtle.Turtle()
    earth = turtle.Turtle()
    mars = turtle.Turtle()
    mercury.shape('circle')
    venus.shape('circle')
    earth.shape('circle')
    mars.shape('circle')
    mercury.pu()
    venus.pu()
    earth.pu()
    mars.pu()
    mercury.sety(-58)
    venus.sety(-108)
    earth.sety(-150)
    mars.sety(-228)
    mercury.pd()
    venus.pd()
    earth.pd()
    mars.pd()
    mars.speed(7.5)
    venus.speed(3)
    earth.speed(2)
    mars.speed(1)
    mercury.circle(58)
    venus.circle(108)
    earth.circle(150)
    mars.circle(228)

提前致谢!

推荐答案

一般来说,如果你想同时做多件事,有两种选择:

In general, if you want to do multiple things at the same time, there are two options:

  • 抢占式多线程,您只需为每件事创建一个线程,它们都尝试全速工作,而计算机会找出如何交错工作.
  • 合作调度:你为一件事做一小部分工作,然后为下一件事做一小部分,依此类推,然后回到第一件事.

在这种情况下,它是您想要的第二个.(好吧,您可能想要第一个,但您不能拥有它;tkinter,因此 turtle,只能在主服务器上运行线.)比如说,画每个圆的前 1°,然后是每个圆的下一个 1°,依此类推.

In this case, it's the second one that you want. (Well, you might want the first, but you can't have it; tkinter, and therefore turtle, can only run on the main thread.) Draw, say, the first 1° of each circle, then the next 1° of each circle, and so on.

那么,你是怎么做到的?circle 方法有一个可选的extent 参数,它是要绘制的角度(以度为单位).所以,你可以这样做:

So, how do you do that? The circle method has an optional extent parameter, which is an angle (in degrees) to draw. So, you can do this:

for i in range(360):
    mercury.circle(58, 1)
    venus.circle(108, 1)
    earth.circle(150, 1)
    mars.circle(228, 1)

当然,您设置的 extent 值越小,每只海龟走的步数"就越多,因此它们进入轨道的速度就越慢.

Of course the smaller you make that extent value, the more "steps" each turtle is taking, so the slower they will take to orbit.

此外,我不确定您是否真的希望以您使用它的方式使用 speed.这会导致每个动作的动画速度更慢.它不会影响它们绕太阳公转的速度,它只会影响每一步绘制所需的时间.所以我认为你在这里真正想要做的是将所有速度保持为 0(没有动画延迟),但每一步都将更快的行星移动更大的范围:

Also, I'm not sure you really want to use speed the way you're using it. That causes each move to animate more slowly. It doesn't affect how quickly they orbit around the sun, it just affects how long each step takes to draw. So I think what you really want to do here is leave all the speeds at 0 (no animation delay), but move the faster planets by a larger extent each step:

mercury.speed(0)
venus.speed(0)
earth.speed(0)
mars.speed(0)
for i in range(360):
    mercury.circle(58, 7.5)
    venus.circle(108, 3)
    earth.circle(150, 2)
    mars.circle(228, 1)

当然,这意味着水星最终会绕太阳公转 7.5 次,而火星只会绕太阳公转一次……但这正是您想要的,对吧?

Of course this means Mercury will end up orbiting the sun 7.5 times, while Mars will only orbit once… but that's exactly what you want, right?

这篇关于如何使用乌龟在python中同时绘制项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆