简体   繁体   中英

How to make spiral spin in turtle?

I am trying to make a game, and I made a main character.

Here is the code for the main character:

from turtle import *

from random import *

chandra = Turtle(shape="turtle")

chandra.speed("fastest")

COLORS = ["orange", "blue", "red", "green", "purple"]

def draw_characterpart1():
    for i in range(36):
        for i in range(3):
            chandra.color(choice(COLORS))
            chandra.forward(80)
            chandra.right(120)

        chandra.left(10)

def draw_characterpart2():
    for i in range(36):
        for i in range(4):
            chandra.color(choice(COLORS))
            chandra.forward(70)
            chandra.right(90)
        chandra.left(10)

def draw_spiral():
    for i in range(10, 90, 10):
        chandra.color(choice(COLORS))
        chandra.circle(i, 180)

draw_characterpart1()

draw_characterpart2()

draw_spiral()

mainloop()

I want to make it spin, or just rotate.

I have tried manually creating the character(without for loops) and then assigning each color.

Once I did that, I could just shift the colors.

This however, was a very bad solution.

Thanks!

With turtle, and a little imagination, anything is possible...

my only idea is to use compound-shapes to create turtle shape which maybe you could rotate. – furas

Compound shapes, as used for making cursors, want to be filled polygons which would be hard to work with in this case.

But if it will not work then you may need to clear character and draw it again with different angle, and later clear it again and draw again with different angle, etc. – furas

Yes, that seems to be the viable approach. However, using random colors works against this illusion somewhat so I've switched to cycling colors instead:

from turtle import Screen, Turtle
from itertools import cycle

COLORS = ["orange", "blue", "red", "green", "purple"]

def draw_character():
    color = cycle(COLORS)

    for _ in range(36):
        for _ in range(3):
            chandra.color(next(color))
            chandra.forward(80)
            chandra.right(120)

        chandra.left(10)

    for _ in range(36):
        for _ in range(4):
            chandra.color(next(color))
            chandra.forward(70)
            chandra.right(90)

        chandra.left(10)

    for radius in range(10, 90, 10):
        chandra.color(next(color))
        chandra.circle(radius, 180)

screen = Screen()
screen.tracer(False)

chandra = Turtle()

for angle in range(720):
    chandra.reset()
    chandra.hideturtle()
    chandra.left(angle)
    draw_character()
    screen.update()

screen.tracer(True)
screen.mainloop()

在此处输入图像描述

Animated GIF is only a sampled approximation of nicer turtle graphics.

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