简体   繁体   English

如何在乌龟中做螺旋旋转?

[英]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.我尝试手动创建角色(没有 for 循环),然后分配每种颜色。

Once I did that, I could just shift the colors.一旦我这样做了,我就可以移动 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但是如果它不起作用,那么您可能需要清除字符并以不同的角度重新绘制它,然后再次清除它并以不同的角度重新绘制,等等。 - 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:但是,使用随机 colors 在某种程度上可以对抗这种错觉,所以我改用循环 colors 代替:

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.动画 GIF 只是更好的海龟图形的采样近似值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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