Turtle is python’s built-in drawing library, generally can do some simple drawing effects, the advantage is that it will play the drawing animation, so you can create some simple animation effects, this time draw two hearts to make a declaration program, first look at the final effect
Here’s the main code
First draw a heart
A heart code is relatively simple, we use two arcs, two straight lines can draw a simple heart
Import turtle as t t. pet (1) t.pencolor('gray') # Set the brush color T. fowler (100) # 100 pixels forward to the right. T. Circle (50, 210) # Draw an arc. 210) # Draw an arc t.fowler (100) # forward 100 pixels t.ed_fill () # fill color t.Tone () # All drawing process endsCopy the code
For ease of understanding, the diagram corresponds to the code
Here, t.speed(1) controls the drawing speed. If you want to make it slower, you can set it to a higher value. If you set it to 0, it is the fastest (ps: if you don’t need to see the drawing track, you can use the tracer function to achieve a faster effect).
Draw two hearts
Since there are two hearts, it is best to change the code for drawing hearts into a draw_heart function, so that we do not have to write the same code twice
Import turtle as t t. pet (1). T. color('gray') # set the color of the paintbrush. T.pagin_fill () t.ft (40) # Turn left 40 degrees T.fordward (100) # right forward 100 pixels t.circle(50, 210) # draw a circle t. light (138) # turn right 138 degrees T. circle(50, 210) # Draw a circle t.fowler (100) # forward 100 pixels t.ed_fill () # Fill color # Draw the bottom left heart t.penup() t.goto(-80,-80) # Draw_heart () # draw_heart() # draw_heart() # draw_heart() # draw_heart(Copy the code
Draw an arrow
We can see the arrow coming from the upper right corner, and there is some occlusion for each heart insert. Here we can solve the scene very well by using the penup and pendown functions. Penup — lift the brush, and the following movement will not show up
Look at the line segment on the graph that corresponds to the code
The t. fowler (68) after the two penups in the code is the line segment of the two heart occlusion parts
To add text
Write function can be used to draw text, relatively simple, but we add a step here, after drawing the arrow icon back to the arrow head, finally paste the finished code
Import turtle as t t. pet (1). T. color('gray') # set the color of the paintbrush. T.pagin_fill () t.ft (40) # Turn left 40 degrees T.fordward (100) # right forward 100 pixels t.circle(50, 210) # draw a circle t. light (138) # turn right 138 degrees T. circle(50, 210) # Draw a circle t.fowler (100) # forward 100 pixels t.ed_fill () # Fill color # Draw the bottom left heart t.penup() t.goto(-80,-80) # Draw_heart () # draw_heart() # draw_heart() # draw_heart() # draw_heart # move to upper right corner of arrow. t.pendown() t.forward(42) t.penup() t.forward(68) t.pendown() t.forward(60) t.penup() t.goto(-100, -150) t.color ('red') t.rite (True, font=("宋体", 18) t.goto(-170,-90) t.pendown() t.tone () # end of all drawing processesCopy the code