Christmas is around the corner, so this time you'll use the turtle module to write a small, expressive program
Development time: 2019-12-15
Development Tools: Sublime
Development module: turtle
Here's the turtle library. If you're not familiar with it, you can see my previous blog:
turtle Library Related Knowledge Points
First, we need to create a function to hold all the initialization data, including the size of the canvas, the size, color of the brush, and the speed at which the brush moves.
As an example, a 800*600 canvas with a red brush width of 5 and a speed of 10 is set up
The code is as follows:
def initdata(): t.setup(800,600) t.pencolor('red') t.pensize(5) t.speed(10)
Once you've set the properties, you can start drawing
First, the brush is in the center. If you want to move the brush, it will leave a mark on the canvas, that is, it will draw a line.
But some lines are not needed by the painting itself, which requires that the brush move without leaving any trace on the canvas
up() and down() are required; two functions, such as literal translation, are generally used to lift and drop the brush
So the action is to lift the brush - move it to the specified place - drop the brush
The code is as follows:
ef move_pen(x,y):
t.hideturtle()
t.up()
t.goto(x,y)
t.down()
t.showturtle()
Note: hideturtle () and showturtle () are hidden brushes and display brushes.Has no effect on the painting itself
Where is the hardest part of drawing a presentation?
It must be the two semicircles of the upper half of your heart, so for convenience, let's talk about this section alone and propose a function to draw a semicircle.
There are no functions in the turtle library to draw curves, and the closest thing is to draw circles.
But we can draw it by cutting the circle, which is to think of it as a polygon with a very short edge
So we draw with a for loop, with small segments that deflect continuously
The code is as follows:
def hart_arc(): for i in range(200): t.right(1) t.forward(2)
Now let's start drawing the graph draw ()
To begin with, there must always be a speaker and a speaker, so first we create a name and a sign to store the name.
You then get information by calling the initialization function
Finally, you can manipulate the brush to start drawing.
First move the brush to (0, -180), which is the lower tip of love, using the move function you just created.
After controlling the direction of the brush, move 224 distances to the right side of the bottom of the love before drawing is completed
Then draw the upper part of love, call hart_arc() to draw the left side of the upper part of love.
Then adjust the direction, and the brush turns 120 degrees to the left before drawing another half circle
Finally, the brush returns to its original position, draws the first love, and fills it with pink.
Similarly, a second love can be drawn, and at the end a double arrow will be enough
When the drawing is finished, we'll output the two names we just recorded.
This requires the write() function
The code is as follows:
def draw(): name=input("Please enter a form name:") sign=input("Please enter your name:") initdata() move_pen(0,-180) t.left(140) t.fillcolor("pink") t.begin_fill() t.forward(224) hart_arc() t.left(120) hart_arc() t.forward(224) t.end_fill() move_pen(x=70, y=160) t.left(185) t.circle(-110,185) t.forward(50) move_pen(-180,-180) t.left(180) t.forward(600) move_pen(0,50) t.hideturtle() t.color('#CD5C5C', 'red') t.write(name, font=('Arial', 20, 'bold'), align="center") t.color('red', 'pink') time.sleep(2) move_pen(220, -180) t.hideturtle() t.write(sign, font=('Arial', 20), align="center")
This completes the presentation code
Thank you for your support. The overall code is as follows:
import turtle as t import time def initdata(): t.setup(800,600) t.pencolor('red') t.pensize(5) t.speed(10) def move_pen(x,y): t.hideturtle() t.up() t.goto(x,y) t.down() t.showturtle() def hart_arc(): for i in range(200): t.right(1) t.forward(2) def draw(): name=input("Please enter a form name:") sign=input("Please enter your name:") initdata() move_pen(0,-180) t.left(140) t.fillcolor("pink") t.begin_fill() t.forward(224) hart_arc() t.left(120) hart_arc() t.forward(224) t.end_fill() move_pen(x=70, y=160) t.left(185) t.circle(-110,185) t.forward(50) move_pen(-180,-180) t.left(180) t.forward(600) move_pen(0,50) t.hideturtle() t.color('#CD5C5C', 'red') t.write(name, font=('Arial', 20, 'bold'), align="center") t.color('red', 'pink') time.sleep(2) move_pen(220, -180) t.hideturtle() t.write(sign, font=('Arial', 20), align="center") def main(): draw() time.sleep(5) if __name__ == '__main__': main()