Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Rainbow and Clouds with Python Turtle
taarimalta
taarimalta

Posted on • Edited on

     

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 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")
Enter fullscreen modeExit fullscreen mode

We will be using a square grid. We have also set the color tolightblue 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

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

importturtleastt.Screen().bgcolor("lightblue")t.color("white","white")t.begin_fill()t.circle(50)t.end_fill()
Enter fullscreen modeExit fullscreen mode

image

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")
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

Multiple calls to filled_circle at different positions

image

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)
Enter fullscreen modeExit 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 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')
Enter fullscreen modeExit fullscreen mode

Reducing radius progressively and changing the color

image

We have aroygbiv 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)
Enter fullscreen modeExit 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

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)
Enter fullscreen modeExit 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 theroygbiv variable

image

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()
Enter fullscreen modeExit fullscreen mode

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

And we get:

image

Yay!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
hatux profile image
Haniel Tuxpeño
Software Engineer from Mexico. Still learrning.My Programming Languages_ Java, Python, C#, C++, JS, PHP.Frmaeworks: Node, React. Laravel,
  • Joined

Looks, pretty cool man

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromtaarimalta

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp