current position:Home>Python uses turtle to express white

Python uses turtle to express white

2022-01-30 06:07:29 Ice and wind all over the sky

turtle yes python My own drawing library , Generally, you can do some simple drawing effects , The advantage is that it will play the drawn animation , So you can simply create some animation effects , This time draw two hearts and make a confession program , Let's see the final effect first

1.gif

Here is the main code

First draw a love

A love code is relatively simple , We use two arcs , Two straight lines can draw a simple love

import turtle as t
t.speed(1)
t.pencolor('gray')  #  Set brush color 
t.fillcolor('red')  #  Set fill color 

t.begin_fill()
t.left(40)  #  Turn left 40 degree 
t.forward(100)  #  Right forward 100 Pixels 
t.circle(50, 210)  #  Draw an arc 
t.right(138)  #  turn right 138 degree 
t.circle(50, 210)  #  Draw an arc 
t.forward(100)  #  forward 100 Pixel 
t.end_fill()  #  Fill color 
t.done()  #  End of all drawing processes 
 Copy code 

For the sake of understanding , The diagram corresponds to the code

1.png

there t.speed(1) It controls the speed of drawing , If you want to be slower, you can adjust a larger value , If it is set to 0 Is the fastest (ps: If you don't have to look at the drawing track, you can use tracer Function to achieve faster results )

Draw two hearts

Because there are two loves , We'd better turn the code of drawing love into a draw_heart function , Let's not write duplicate code twice

import turtle as t
t.speed(1)
t.pencolor('gray')  #  Set brush color 
t.fillcolor('red')  #  Set fill color 

#  Draw love function 
def draw_heart():
    t.begin_fill()
    t.left(40)  #  Turn left 40 degree 
    t.forward(100)  #  Right forward 100 Pixels 
    t.circle(50, 210)  #  Draw an arc 
    t.right(138)  #  turn right 138 degree 
    t.circle(50, 210)  #  Draw an arc 
    t.forward(100)  #  forward 100 Pixel 
    t.end_fill()  #  Fill color 

#  Draw love at the bottom left 
t.penup()
t.goto(-80,-80)  #  The starting point of love at the bottom left 
t.pendown()
draw_heart()

#  Draw love on the top right 
t.penup()
t.goto(0,0)  #  The starting point of love on the upper right 
t.pendown()
t.left(40)
draw_heart()
t.done()  #  End of all drawing processes 
 Copy code 

2.png

Draw arrow

We saw the arrow coming from the upper right corner , Every time you insert a love, there will be some shelter , Here we use penup、pendown Function can solve this scenario well penup --- Bring up the brush , The following movement will not show graphics pendown --- Put down the paintbrush , The following movement will show the figure

Look at the line segment on the graph corresponding to the code

3.png Two in the code penup After that t.forward(68) Is the line segment of the two love shielding parts

Add text

You can draw words with write Functions can be implemented , Relatively simple , Just one more step here , After drawing, let the arrow icon return to the head of the arrow , Finally, paste the completed code

import turtle as t
t.speed(1)
t.pencolor('gray')  #  Set brush color 
t.fillcolor('red')  #  Set fill color 

#  Draw love function 
def draw_heart():
    t.begin_fill()
    t.left(40)  #  Turn left 40 degree 
    t.forward(100)  #  Right forward 100 Pixels 
    t.circle(50, 210)  #  Draw an arc 
    t.right(138)  #  turn right 138 degree 
    t.circle(50, 210)  #  Draw an arc 
    t.forward(100)  #  forward 100 Pixel 
    t.end_fill()  #  Fill color 

#  Draw love at the bottom left 
t.penup()
t.goto(-80,-80)  #  The starting point of love at the bottom left 
t.pendown()
draw_heart()

#  Draw love on the top right 
t.penup()
t.goto(0,0)  #  The starting point of love on the upper right 
t.pendown()
t.left(40)
draw_heart()

#  Draw an arrow 
t.pensize(4)
t.penup()
t.goto(100,180)  #  Move to the upper right corner of the arrow 
t.pendown()
t.setheading(225)  #  Specify the direction of motion 
t.forward(140)  #  Forward 140 Pixels 
t.penup()
t.forward(68)
t.pendown()
t.forward(42)
t.penup()
t.forward(68)
t.pendown()
t.forward(60)

t.penup()
t.goto(-100, -150)
t.pencolor('red')
t.write(" Here you know ~", True, font=(" Song style ", 18))
t.goto(-170,-90)
t.pendown()
t.done()  #  End of all drawing processes 
 Copy code 

copyright notice
author[Ice and wind all over the sky],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300607256258.html

Random recommended