简体   繁体   中英

How to make this wheel spin?

I have created the wheel, but when I try to make code to spin it will not work.

I have already tried to make it using a loop but that was near impossible for me. I am basically drawing the wheel over. Here is some code from the spinning wheel part:

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)

The code keeps on going to make the wheel 'spin'.

This is what I get:

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

My impression is that you're not trying to make the wheel spin but rather make it look like it's spinning by cycling its colors. Here's my example of doing such which uses tracer() and update() to turn off drawing while remaking the circle with the colors shifted. It uses a timer event to trigger redraws. Rather than your fixed list of colors, I'm going to use continuous hues, but you should be able to use any colors you wish:

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

This works, but, on my system, inexplicably slows down over time.

Let's try a different approach that doesn't redraw anything at the Python level during the cycling of colors. We're going to design a new cursor shape, "wedge" and build our circle out of turtles! All the wedges will be prepositioned and not move nor be redrawn. The timer event handler will simply ask each turtle to take on the color of its neighbor:

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

Notice how much simpler our main loop, draw_circle() , is and that the spinning doesn't slow down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM