Python Loader使用乌龟

时间:2017-06-01 11:39:46

标签: python loading turtle-graphics

我想制作一个装载机,就像这样:

Loader

到目前为止,这是我的代码:

import turtle, random                # random for colour choice, turtle for design
turtle.hideturtle()                  # don't show arrow

turtle.pensize(10)                   # thick pen
turtle.colormode(255)                # RGB colour format
x = 0                                # loop
while (x < 10):                      # loop
    x = x + 1                        # loop 
    r = random.randint(0,255)        # choose random r
    g = random.randint(0,255)        # choose random g
    b = random.randint(0,255)        # choose random b
    turtle.color((r,g,b))            # new colour every loop
    turtle.circle(100)               # draw circle

如何使用python和turtle使其与上面的类似?我知道这个问题不是很好,但我无法在互联网上找到任何有关此问题的内容。我不想在Turtle中插入图片。我想以图形方式创建它。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我的代码和你的例子之间的妥协。它背后的想法是,使用计时器事件旋转自定义乌龟光标比尝试绘制某些东西更快更简单:

from turtle import Turtle, Screen

STEP = -12  # should be divisor of 360
GAP = 45  # in degrees
PEN_SIZE = 4  # emulated pen width
RADIUS = 24

def await_loading(degrees=[0], color=[1.0, 0.0, 0.0]):  # intentionally dangerous default values

    if degrees[0] == 0:
        color.append(color.pop(0))
        loading.color(color)

    loading.tilt(STEP)

    degrees[0] = (degrees[0] + STEP) % 360

    screen.ontimer(await_loading, 10)

loading = Turtle()
loading.speed('fastest')
loading.backward(RADIUS)
loading.right(90)

loading.begin_poly()
loading.circle(RADIUS, 360 - GAP, 60)
loading.left(90)
loading.forward(PEN_SIZE)
loading.right(90)
loading.circle(RADIUS - PEN_SIZE, GAP - 360, 60)
loading.end_poly()

screen = Screen()
screen.addshape('loading', loading.get_poly())

loading.reset()
loading.shape('loading')

await_loading()

screen.exitonclick()

我没有把它整齐地打包成一个函数,因为我不知道会启动什么样的事件并停止它 - 我只是希望显示图形是可能的。