Rainbow and Clouds with Python Turtle
Tags: python, turtle, graphics, tutorial
Published: 2021-05-28 15:53:34

In the previous posts we did

1. [Filled Stars](https://dev.to/taarimalta/how-to-draw-a-filled-star-with-python-turtle-2k0g)
2. [Spirals](https://dev.to/taarimalta/how-to-draw-a-spiral-with-python-turtle-2n5c)

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](https://docs.python.org/3/library/turtle.html#turtle.screensize) command to set the size of the window, and the [setworldcoordinates](https://docs.python.org/3/library/turtle.html#turtle.setworldcoordinates) command to subdivide the window into the desired number of pixels. `setworldcoordinates` also sets up the coordinate axes for us

```python
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](https://docs.python.org/3/library/turtle.html#turtle.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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cncy3dxbnvgyr7xq0im.png)

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

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

t.color("white","white")
t.begin_fill()
t.circle(50)
t.end_fill()
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuml527b52o9dv7d2hrd.png)
 
Now, lets put that code into a function

```python
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")
```
> Filled circle function. Code does the same thing as before

Next we will draw multiple circles using the function above

```python
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)
```
> Multiple calls to filled_circle at different positions

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pon1yo15g09qjzmnfidd.png)

We have a cloud now! ( Hope you think so too :) )
 
```python
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)
```
> `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.

```python
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')
```
> Reducing radius progressively and changing the color

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrodhxd43lnvx10yr4lv.png)
> 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

```python
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)
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omgfj5i4cxrq3yccmhsb.png)

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

```python
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)
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkmww2ig6eu11x1ibv3q.png)


# Putting it all together

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

```python
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()


```
> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sgg785kkt8971514xbr.png)

Yay!