如何使车轮旋转?

时间:2019-03-29 22:03:15

标签: python python-3.x animation turtle-graphics

我已经创建了轮子,但是当我尝试旋转代码时将无法工作。

我已经尝试过使用循环来实现,但这对我来说几乎是不可能的。我基本上是把轮子拉过来。这是转轮部分的一些代码:

turtle.listen()
if turtle.onkeypress("space"):
    colors = ['#880000','#884400','#884400','#888800',
              '#888800','#008800','#008800','#008800',
              '#008800','#008800','#008888','#008888',
              '#008888','#008888','#008888','#000088',
              '#000088','#000088','#000088','#000088']

for color in colors:
    slice_angle = 360 / len(colors)
    heading, position = 90, (center[0] + radius, center[1])
    turtle.color(color, color)
    turtle.speed(0)
    turtle.penup()
    turtle.goto(position)
    turtle.setheading(heading)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius, extent=slice_angle)
    heading, position = turtle.heading(), turtle.position()
    turtle.penup()
    turtle.goto(center)
    turtle.end_fill()
    turtle.penup()

time.sleep(0.2)
colors = ['#884400','#884400','#888800',
          '#888800','#008800','#008800','#008800',
          '#008800','#008800','#008888','#008888',
          '#008888','#008888','#008888','#000088',
          '#000088','#000088','#000088','#000088','#880000']
for color in colors:
    slice_angle = 360 / len(colors)
    heading, position = 90, (center[0] + radius, center[1])
    turtle.color(color, color)
    turtle.speed(0)
    turtle.penup()
    turtle.goto(position)
    turtle.setheading(heading)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(radius, extent=slice_angle)
    heading, position = turtle.heading(), turtle.position()
    turtle.penup()
    turtle.goto(center)
    turtle.end_fill()
    turtle.penup()

time.sleep(0.2)

代码继续使轮子“旋转”。

这就是我得到的:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 701, in eventfun
    fun()
TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:0)

我的印象是,您不是在尝试使轮子旋转,而是通过循环其颜色使其看起来像在旋转。这是我这样做的示例,该示例使用https://phinx.org/ http://docs.phinx.org/en/latest/ tracer()来关闭图形,同时重新制作带有偏移颜色的圆。它使用计时器事件触发重绘。我将使用连续的色调,而不是固定的颜色列表,但是您应该可以使用所需的任何颜色:

update()

这可行,但是在我的系统上,随着时间的推移,它会莫名其妙地变慢。

让我们尝试另一种方法,该方法在颜色循环期间不会在Python级别重绘任何内容。我们将设计一个新的光标形状from turtle import Screen, Turtle from colorsys import hsv_to_rgb RADIUS = 100 NUMBER_OF_WEDGES = 20 SLICE_ANGLE = 360 / NUMBER_OF_WEDGES screen = Screen() screen.tracer(False) turtle = Turtle(visible=False) turtle.penup() center = turtle.position() turtle.sety(turtle.ycor() - RADIUS) hues = [color / NUMBER_OF_WEDGES for color in range(NUMBER_OF_WEDGES)] # precompute hues index = 0 def draw_circle(): global index for hue in range(NUMBER_OF_WEDGES): turtle.color(hsv_to_rgb(hues[(hue + index) % NUMBER_OF_WEDGES], 1.0, 1.0)) turtle.pendown() turtle.begin_fill() turtle.circle(RADIUS, extent=SLICE_ANGLE) position = turtle.position() turtle.goto(center) turtle.end_fill() turtle.penup() turtle.goto(position) screen.update() index = (index + 1) % NUMBER_OF_WEDGES screen.ontimer(draw_circle, 40) draw_circle() screen.exitonclick() ,并用乌龟建造一个圆圈!所有楔形将被预先放置,并且不会移动或重新绘制。计时器事件处理程序将简单地要求每只乌龟采用其邻居的颜色:

"wedge"

请注意,我们的主循环from turtle import Screen, Turtle from colorsys import hsv_to_rgb RADIUS = 100 NUMBER_OF_WEDGES = 20 SLICE_ANGLE = 360 / NUMBER_OF_WEDGES screen = Screen() screen.tracer(False) # create a pie wedge-shaped cursor turtle = Turtle(visible=False) turtle.begin_poly() turtle.sety(turtle.ycor() - RADIUS) turtle.circle(RADIUS, extent=SLICE_ANGLE) turtle.home() turtle.end_poly() screen.register_shape("wedge", turtle.get_poly()) # create a turtle for each wedge in the pie turtles = [] for hue in range(NUMBER_OF_WEDGES): turtle = Turtle("wedge") turtle.color(hsv_to_rgb(hue / NUMBER_OF_WEDGES, 1.0, 1.0)) turtle.setheading(hue * SLICE_ANGLE) turtles.append(turtle) def draw_circle(): # have each turtle take on the color of its neighbor for index, turtle in enumerate(turtles): turtle.color(*turtles[(index + 1) % NUMBER_OF_WEDGES].color()) screen.update() screen.ontimer(draw_circle, 40) draw_circle() screen.exitonclick() 简单了多少,而且旋转速度不会变慢。