## DEV Community

taarimalta

Posted on • Updated on

# Rainbow and Clouds with Python Turtle

In the previous posts we did

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")

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

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.

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

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

Now, lets put that code into a function

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

t.color(color,color)
t.begin_fill()
t.end_fill()

filled_circle(50,"white")

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")

t.color(color,color)
t.begin_fill()
t.end_fill()

cloud_color="white"

t.right(90)
t.right(90)
t.right(90)
t.right(90)

Multiple calls to filled_circle at different positions

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

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

t.color(color,color)
t.begin_fill()
t.end_fill()

t.right(90)
t.right(90)
t.right(90)
t.right(90)

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")

t.color(color,color)
t.begin_fill()
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')

Reducing radius progressively and changing the color

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")

t.color(color,color)
t.begin_fill()
t.end_fill()

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

interval=10

for r_color in roygbiv:

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

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.

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)

t.color(color,color)
t.begin_fill()
t.end_fill()

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

for r_color in roygbiv:

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

rainbow(300,10)

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

# 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)

t.color(color,color)
t.begin_fill()
t.end_fill()

t.right(90)
t.right(90)
t.right(90)
t.right(90)

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

for r_color in roygbiv:

# 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()

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:

Yay!