
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 thescreensize command to set the size of the window, and thesetworldcoordinates command to subdivide the window into the desired number of pixels.setworldcoordinates
also sets up the coordinate axes for us
importturtleastt.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
usingbgcolor
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.
First start with a filled circle
importturtleastt.Screen().bgcolor("lightblue")t.color("white","white")t.begin_fill()t.circle(50)t.end_fill()
Now, lets put that code into a function
importturtleastt.Screen().bgcolor("lightblue")deffilled_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
importturtleastt.Screen().bgcolor("lightblue")deffilled_circle(radius,color):t.color(color,color)t.begin_fill()t.circle(radius)t.end_fill()radius=50cloud_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
We have a cloud now! ( Hope you think so too :) )
importturtleastt.Screen().bgcolor("lightblue")deffilled_circle(radius,color):t.color(color,color)t.begin_fill()t.circle(radius)t.end_fill()defcloud(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=50cloud(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 thefilled_circle
function.
importturtleastt.Screen().bgcolor("lightblue")deffilled_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
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
importturtleastt.Screen().bgcolor("lightblue")deffilled_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=200interval=10forr_colorinroygbiv:filled_circle(radius,r_color)radius-=interval# Move turtle a up by a littlet.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
importturtleastt.Screen().bgcolor("lightblue")t.Screen().screensize(500,500)t.Screen().setworldcoordinates(-300,300,300,900)deffilled_circle(radius,color):t.color(color,color)t.begin_fill()t.circle(radius)t.end_fill()defrainbow(radius=200,interval=10):roygbiv=['red','orange','yellow','green','blue','indigo','violet','lightblue']forr_colorinroygbiv:filled_circle(radius,r_color)radius-=interval# Move turtle a up by a littlet.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 theroygbiv
variable
Putting it all together
We can now use the functions that we created to draw a rainbow amongst the clouds
importturtleastt.Screen().bgcolor("lightblue")t.Screen().screensize(500,500)t.Screen().setworldcoordinates(-300,300,300,900)t.Screen().tracer(0,0)deffilled_circle(radius,color):t.color(color,color)t.begin_fill()t.circle(radius)t.end_fill()defcloud(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)defrainbow(radius=200,interval=10):roygbiv=['red','orange','yellow','green','blue','indigo','violet','lightblue']forr_colorinroygbiv:filled_circle(radius,r_color)radius-=interval# Move turtle a up by a littlet.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)
andt.Screen().update()
lines to speed up the drawing
And we get:
Yay!
Top comments(1)

- Joined
Looks, pretty cool man
For further actions, you may consider blocking this person and/orreporting abuse