Rainbow and Clouds with Python Turtle

taarimalta

taarimalta

Posted on May 28, 2021

Rainbow and Clouds with Python Turtle

In the previous posts we did

  1. Filled Stars
  2. Spirals

We will draw a rainbow with some clouds today. This one should be especially fun for kids because of the brights colors and the joyful objects that we will be drawing

Setting up the screen

To set up the stage let's first configure the height and the width of the canvas. We need the screensize command to set the size of the window, and the setworldcoordinates command to subdivide the window into the desired number of pixels. setworldcoordinates also sets up the coordinate axes for us



import turtle as t

t.Screen().screensize(500,500)
t.Screen().setworldcoordinates(-300,300,300,900)
t.Screen().bgcolor("lightblue")


Enter fullscreen mode Exit fullscreen mode

We will be using a square grid. We have also set the color to lightblue using bgcolor

In the code above we have set the coordinate y-axis range to be within 300 and 900. This will make sense in the upcoming sections

image

We now have a blue sky

Draw a cloud

We will be using circles to draw a cloud but there could be multiple other ways to draw a cloud.

First start with a filled circle



import turtle as t
t.Screen().bgcolor("lightblue")

t.color("white","white")
t.begin_fill()
t.circle(50)
t.end_fill()


Enter fullscreen mode Exit fullscreen mode

image

Now, lets put that code into a function



import turtle as t
t.Screen().bgcolor("lightblue")

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

filled_circle(50,"white")


Enter fullscreen mode Exit fullscreen mode

Filled circle function. Code does the same thing as before

Next we will draw multiple circles using the function above



import turtle as t
t.Screen().bgcolor("lightblue")

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()


radius = 50
cloud_color="white"

filled_circle(radius,cloud_color)
t.forward(radius)
filled_circle(radius,cloud_color)
t.right(90)
filled_circle(radius,cloud_color)
t.right(90)
filled_circle(radius,cloud_color)
t.right(90)
filled_circle(radius,cloud_color)
t.right(90)


Enter fullscreen mode Exit fullscreen mode

Multiple calls to filled_circle at different positions

image

We have a cloud now! ( Hope you think so too :) )



import turtle as t
t.Screen().bgcolor("lightblue")

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()


def cloud(radius, cloud_color="white"):
    filled_circle(radius,cloud_color)
    t.forward(radius)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)

radius = 50

cloud(radius)


Enter fullscreen mode Exit fullscreen mode

cloud function for use later

Concentric circles

Let's work on the rainbow now. Our approach will be to use concentric filled circles to draw the rainbow. Let's see if we can get what we want by progressively reducing the radius of the filled_circle function.



import turtle as t
t.Screen().bgcolor("lightblue")

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()


filled_circle(100,'red')
filled_circle(95,'orange')
filled_circle(90,'yellow')
filled_circle(85,'green')
filled_circle(80,'blue')
filled_circle(75,'indigo')
filled_circle(70,'violet')


Enter fullscreen mode Exit fullscreen mode

Reducing radius progressively and changing the color

image

We have a roygbiv but the circles are not concentric

Let's see how to make the circles above concentric. Let's also use a loop



import turtle as t
t.Screen().bgcolor("lightblue")

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

roygbiv=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']


radius=200
interval=10

for r_color in roygbiv:
    filled_circle(radius,r_color)
    radius -= interval

    # Move turtle a up by a little
    t.left(90)
    t.forward(interval)
    t.right(90)


Enter fullscreen mode Exit fullscreen mode

In the code above we move the turtle up a little every time it completes a circle. We have also stored the colors of the rainbow in a list.

image

There are two additional things we should do. First, a rainbow could be circular but generally we see just a portion of the rainbow. To get that effect, we can move half the rainbow off the screen. Second, the center of the rainbow should be the sky. So let's make those adjustments



import turtle as t
t.Screen().bgcolor("lightblue")
t.Screen().screensize(500,500)
t.Screen().setworldcoordinates(-300,300,300,900)

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()


def rainbow(radius=200,interval=10):
    roygbiv=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'lightblue']

    for r_color in roygbiv:
        filled_circle(radius,r_color)
        radius -= interval

        # Move turtle a up by a little
        t.left(90)
        t.forward(interval)
        t.right(90)


rainbow(300,10)


Enter fullscreen mode Exit fullscreen mode

In the code above, we have changed the screen size and set up the coordinates such that only half the rainbow shows. Also, the rainbow code is now in a function. Lastly to show the center of the rainbow as the sky, lightblue has been added to the roygbiv variable

image

Putting it all together

We can now use the functions that we created to draw a rainbow amongst the clouds



import turtle as t
t.Screen().bgcolor("lightblue")
t.Screen().screensize(500,500)
t.Screen().setworldcoordinates(-300,300,300,900)
t.Screen().tracer(0,0)

def filled_circle(radius, color):
    t.color(color,color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

def cloud(radius, cloud_color="white"):
    filled_circle(radius,cloud_color)
    t.forward(radius)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)
    filled_circle(radius,cloud_color)
    t.right(90)

def rainbow(radius=200,interval=10):
    roygbiv=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'lightblue']

    for r_color in roygbiv:
        filled_circle(radius,r_color)
        radius -= interval

        # Move turtle a up by a little
        t.left(90)
        t.forward(interval)
        t.right(90)

t.penup()

t.goto(100,900)
cloud(20)
t.goto(-50,850)
cloud(30)
t.goto(50,600)
cloud(50)
t.goto(200,500)
cloud(5)

t.goto(0,0)
rainbow(300,10)

t.goto(-200,500)
cloud(15)
t.goto(50,400)
cloud(10)
t.goto(50,300)
cloud(10)

t.Screen().update()




Enter fullscreen mode Exit fullscreen mode

Note that we have also added the t.Screen().tracer(0,0) and t.Screen().update() lines to speed up the drawing

And we get:

image

Yay!

💖 💪 🙅 🚩
taarimalta
taarimalta

Posted on May 28, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related