关于python:Turtle Graphics – 单词的替代颜色

Turtle Graphics - Alternate Colors of Letters in Words

我有一个颜色列表:

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

我想用turtle.write()来显示文本,以替换单词"dog"和"alligator"中字母的颜色。

"狗"的字母将被涂成"红"、"蓝"和"绿"

"短吻鳄"的字母将是"红"、"蓝"、"绿"、"黄"、"红"、"蓝"、"绿"、"黄"、"红"。

如何在海龟图形中实现这一点?谢谢您!


有几件事可以使这个更容易实现。第一种方法是使用itertools.cycle()反复地浏览颜色列表。另一种方法是使用move=True参数到turtle.write()参数,这样您就可以一个接一个地打印单词的各个字符:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()

enter image description here