海龟图形 - 单词中字母的交替颜色

Turtle Graphics - Alternate Colors of Letters in Words

我有一个颜色列表:

colors = ["red", "blue", "green", "yellow"]

并且我想使用 turtle.write() 来显示文本,以交替 "DOG" 和 "ALLIGATOR" 中字母的颜色。

"DOG" 的字母将被着色为 "red"、"blue" 和 "green"

"ALLIGATOR" 的字母将被着色 "red"、"blue"、"green"、"yellow"、"red"、"blue", "green", "yellow", "red".

如何在 Turtle Graphics 中完成此操作?谢谢!

有几件事应该可以使这更容易实施。第一种是使用 itertools.cycle() 重复处理您的颜色列表。另一种方法是使用 turtle.write()move=True 参数,以便您可以一个接一个地打印单词的各个字符:

from turtle import Turtle, Screen
from itertools import cycle

FONT = ('Arial', 36, 'normal')
COLORS = ["red", "blue", "green", "yellow"]

def stripe_write(turtle, string):
    color = cycle(COLORS)

    for character in string:
        turtle.color(next(color))
        turtle.write(character, move=True, font=FONT)

yertle = Turtle(visible=False)
yertle.penup()

stripe_write(yertle, "DOG")
yertle.goto(100, 100)
stripe_write(yertle, "ALLIGATOR")

screen = Screen()
screen.exitonclick()