如何在乌龟中重复时改变颜色

时间:2020-04-14 10:16:19

标签: python colors repeat turtle-graphics

我已经设置好所有可以重复正常工作的姓名缩写的过程,但是,我需要在每次重复之后更改颜色。 (我知道在我不明白答案之前就已经问过这个问题)

import turtle 
s = turtle.Turtle()
s.color("purple")



def Square(turtle):
for i in range(4):
    s.left(90)
    s.forward(150)
 s.right(20)


for i in range(16):
Square(s.right(20))
Square(s.right(10))
Square(s.right(20))

1 个答案:

答案 0 :(得分:0)

import turtle 
s = turtle.Turtle()

#Create list of colors you want
colors = ["red", "green", "blue", "orange", "pink", "yellow"]

def Square(turtle):
    for i in range(4):

        #Choosing color from list- this option is repeating colors at list from first to last 
        color_number = i % len(colors)
        current_color = colors[color_number] 

        #Here you set color you want
        s.color(current_color)

        s.left(90)
        s.forward(150)
        s.right(20)

for i in range(16):
    Square(s.right(20))
    Square(s.right(10))
    Square(s.right(20))
相关问题