Search code examples
pythonpython-turtle

Changing the color gradually turtle (python)


I want to make a pattern that gradually changes color as it is being drawn, something like this:

#by the way this is in a loop
turtle.fd(100)
turtle.rt(30)
turtle.fd(50)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.rt(3)
#something here to change the color a little bit

Without the color, this is still a pretty cool pattern, but I want to know how to make the color gradually change from, say red, to yellow to green and blue then back to red eventually.


Solution

  • import turtle
    color = ['red','yellow','green','blue']
    for i in range(100):
        #by the way this is in a loop
        turtle.color(color[i%4])
        turtle.fd(100)
        turtle.rt(30)
        turtle.fd(50)
        turtle.penup()
        turtle.goto(0,0)
        turtle.pendown()
        turtle.rt(3)
        #something here to change the color a little bit
    

    This is a good option