Python Turtle drawing [difficulty 2 stars]: Rainbow (completed in five steps)

Posted by ridgerunner on Sat, 05 Mar 2022 05:46:54 +0100

I like turtle drawing, because a little change in the code will always bring unexpected surprises.

Some cases that make me excited are shared with you and cherished to myself.

——Crawler on Python Teaching Road

 

The rainbow is so beautiful and moving that it spans from one end of the earth to the other, like a ring woven by flowers on the skirt skirt of the sky blue. Red, orange, yellow, green, blue, purple, how charming. It solidifies all the soft colors in the world in the sky. It seems to bring us into a fairy tale world, brilliant and dazzling.

This case will be divided into five steps to complete the drawing of the rainbow in the figure below.

Difficulty: ★★☆☆☆☆

Step 1: set the canvas according to the background picture

[breakthroughs in key and difficult points]

1. How to set canvas size:

turtle.setup(839,612)

#Set the width of the window to 839 pixels and the height to 612 pixels

Just click to select the picture "blue sky and white clouds", and the size of the picture will be displayed in the status bar below. As shown below, the size of this picture is 839 × 612, that is, the width is 839 pixels and the height is 612 pixels.

2. Method of setting background picture:

turtle.bgpic("blue sky and white clouds. gif")

#Set the picture "blue sky and white clouds" as the background

Key points:

1. The background picture must be in gif or png format

2. The picture must be in the same folder as the code file

3. The name of the picture must be enclosed in double quotation marks

4. The picture name must be added with format suffix

This is the background picture used in this case # from the screen

3. How to quickly convert JPG pictures to GIF format?

When you right-click to save a picture on a web page, you cannot directly save it to GIF format. You can use the screenshot tools of chat boxes such as wechat, QQ and nailing, and then select GIF format to save the screenshot.

Step 2: determine the value of rainbow seven colors

[breakthroughs in key and difficult points]

1. Is it "red orange yellow green blue purple" or "red orange yellow green blue indigo purple"?

Students often ask. I searched the Internet for a long time and found a more reasonable explanation.

In literature, it is often said that "red (red) orange yellow green blue purple", but in scientific research, the rigorous saying is "red orange yellow green blue indigo purple".

2. "Rainbow" Baidu Encyclopedia

Rainbow, also known as Tiangong (Hakka), Tianhong and Jiang, is an optical phenomenon in meteorology.

When the sunlight shines on the water droplets in the air, the light is refracted and reflected, forming an arched colorful spectrum in the sky, which is red, orange, yellow, green, blue, indigo and purple from the outer ring to the inner ring.

In fact, there are countless colors in the rainbow. For example, there are many subtle colors between red and orange, but for simplicity, only seven colors are used as the difference.

3. Rainbow seven color hexadecimal value

The hexadecimal values of rainbow seven colors provided on the Internet are basically "red, orange, yellow, green, cyan, blue and purple".

Red #FF0000

Orange #FF7F00

Yellow #FFFF00

Green #00FF00

Cyan #00FFFF

Blue #0000FF

Purple #

Step 3: determine the radius angle of the arc

[breakthroughs in key and difficult points]

To draw an arc like a rainbow, you need to first adjust the orientation of the turtle's head, and then use the circle (radius, angle) to achieve.

Example 1:

Let the turtle's head up, i.e. setheading(90)

① Circle can be used to draw an arc from right to left,

② Circle (negative radius, positive angle) can also be used to draw an arc from left to right.

Example 2:

Let the turtle's head down, i.e. setheading(-90)

③ Circle (positive radius, negative angle) can be used to draw an arc from left to right,

④ Circle (negative radius, negative angle) can also be used to draw an arc from right to left.     

The first method is used in this case.

Try as like as two peas of the following two codes:

If methods 2 and 3 are used, the coordinates of turtle goto(x,y) need to be reset.

Step 4: try to draw a two-color arc

[breakthroughs in key and difficult points]

When drawing a rainbow, the relationship between the radius of the arc and the thickness of the brush is as follows:

[code sharing]

Think about how to change repetitive code into a loop?

# Rainbow (draw two-color arc)
import turtle
turtle.pensize(30)

# ① Draw a red circle
turtle.penup()
turtle.setheading(90)   # Let the turtle head up
turtle.goto(260,0)      # The turtle moves to the right tangent of the circle, that is, the x value is equal to the radius length
turtle.pendown()

turtle.pencolor("#ff0000")
turtle.circle(260,180)  # Draw an arc with a radius of 280 and a degree of 180

# ② Draw an orange circle
turtle.penup()
turtle.setheading(90)   # Put the turtle's head up again
turtle.goto(230,0)      # The turtle moves to the right tangent of the circle, that is, the x value is equal to the radius length
turtle.pendown()

turtle.pencolor("#ff7f00")
turtle.circle(230,180)  # Draw an arc with a radius of 270 and a degree of 180. 270 is 280 minus the thickness of the brush.

Step 5: add a circle and draw a seven color arc

[preview of final effect]

[code sharing]

When drawing the seven arcs of the rainbow, you need to reset the thickness of the brush and the radius of the arc according to the size of the canvas. Although the value has changed, the relationship between the two has not changed.

r=r-size+1

# turtle case: Rainbow
# ① Program initialization settings
import turtle
turtle.setup(839,612)        # Set the canvas size according to the picture size
#turtle.bgcolor('#92e8ff')   # Set the background color to light blue
turtle.bgpic("The blue sky and white clouds.gif") # Set picture as background
turtle.speed(1)              # Set turtle drawing speed
turtle.delay(0)              # Set the delay time for turtle drawing

# ② Variable initialization settings
r=330     # Radius of circle
size=10   # Setting the thickness of an arc
colors=["#ff0000","#ff7f00","#ffff00","#00ff00","#00ffff","#0000ff","#8b00ff"] # Rainbow seven color list red orange yellow green blue indigo purple

# ③ Draw a rainbow
for i in range(7):             # Draw 7 arcs
    # Draw an arc
    turtle.penup()
    turtle.setheading(90)      # Turtle head up
    turtle.goto(r,-50)         # The turtle moves from the center of the circle to the right of the circle
    turtle.pendown()

    turtle.pencolor(colors[i]) # Traverse color list
    turtle.pensize(size)       # The thickness of the brush is set to size
    turtle.circle(r,180)       # Draw an arc with a radius of r and an angle of 180 degrees
    r=r-size+1                 # Decreases the radius of the next circle

# ④ Write text
turtle.penup()
turtle.home()                  # Let the turtle return to the origin (0,0)
turtle.pendown()
turtle.pencolor("#ffffff")     # Set the brush color to white
turtle.write("How can you see a rainbow without going through wind and rain?",align="center",font=("official script",30)) # Centered text

# ⑤ The drawing is over and the turtle is hidden
turtle.hideturtle()

-end-

Perhaps, there is no long-awaited rainbow after the storm. Maybe, after working hard, we didn't get the corresponding return, but after all, we all worked hard. Perhaps, the rainbow is not far away, and the return is waiting for you not far ahead. Please live every day carefully.

Topics: Python