Turtle Graphics - Alternate Colors of Letters in Words
我有一个颜色列表:
1 | colors = ["red","blue","green","yellow"] |
我想用
"狗"的字母将被涂成"红"、"蓝"和"绿"
"短吻鳄"的字母将是"红"、"蓝"、"绿"、"黄"、"红"、"蓝"、"绿"、"黄"、"红"。
如何在海龟图形中实现这一点?谢谢您!
有几件事可以使这个更容易实现。第一种方法是使用
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() |