【问题标题】:How to make this wheel spin?如何让这个轮子旋转?
【发布时间】:2019-03-29 22:03:14
【问题描述】:

我已经创建了轮子,但是当我尝试让代码旋转时它不起作用。

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

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

【问题讨论】:

  • 我认为 - 但由于回调不包含您的任何代码而无法确定 - 发生异常是因为对 turtle.onkeypress("space") 的调用错误(或其他地方的另一个错误) .第一个参数应该是函数名,而不是字符串。

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


【解决方案1】:

我的印象是,您并没有试图让轮子旋转,而是通过循环颜色让它看起来像在旋转。这是我这样做的示例,它使用tracer()update() 关闭绘图,同时重新制作颜色变化的圆圈。它使用计时器事件来触发重绘。我将使用连续的色调,而不是您的固定颜色列表,但您应该能够使用您希望的任何颜色:

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

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

让我们尝试一种不同的方法,在颜色循环期间不会在 Python 级别重绘任何东西。我们将设计一个新的光标形状"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()

请注意我们的主循环 draw_circle() 多么简单,并且旋转并没有减慢。

【讨论】:

  • 非常感谢。这真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 2012-01-22
  • 2020-03-04
相关资源
最近更新 更多