utilize Python Of turtle Library to achieve an animation

Turtle The library is Python Built in graphical module , Is the function library for drawing images . The turtle is the cursor on the screen ( Small triangle ), to write Python The command allows the turtle to move and draw lines on the screen , Think of a turtle as moving in a coordinate system , The position can be expressed by coordinates (x,y).

be based on turtle Animation of , Animation can be understood as a quick switch from one to another :
1, preparation :
set up speed by 0( Fastest , Don't show turtle dynamics );
Hidden turtle ——hideturtle();
close tracer;
2, utilize clear() Function to clear the current screen display or reset Clear display reset turtle , The last graph is cleared ;
3, Draw new graphics ;
4, utilize update( Update );
5, Wait for a while sleep(time), Set the delay according to the actual animation effect time second ;
6, Loop drawing after completion ;
example : Achieve animated smile
import turtle
from time import sleep
# Defines the size and background of the canvas
turtle.screensize(300,200,“yellow”)

# Defines the speed of the brush
turtle.speed(0)

# Define a method to draw different radians in different positions
def hudu(x,r,y,t,f):
turtle.penup()
turtle.goto(x,r)
turtle.pendown()
turtle.left(y)
turtle.circle(t,f)

# Define a function to draw the whole face
def lian():
# Left eye
hudu(-240,0,90,-90,180)

# Right eye
hudu(50,0,180,-90,180)

# nose
hudu(-15,-40,0,0,0)
turtle.fillcolor(“black”)
turtle.begin_fill()
turtle.left(90)
turtle.forward(30)
turtle.right(150)
turtle.forward(20)
turtle.right(60)
turtle.forward(20)
turtle.end_fill()

# mouth - Lower lip
hudu(120,-105,120,0,0)
turtle.fillcolor(“red”)
turtle.begin_fill()
turtle.circle(-120,177)
turtle.end_fill()

# mouth - Upper lip
hudu(0,-100,160,0,0)
turtle.fillcolor(“yellow”)
turtle.begin_fill()
turtle.circle(-70,150)
hudu(0,-100,190,70,140)
turtle.end_fill()

# animation : Animation to make face laugh —— blink
turtle.tracer(False)
turtle.hideturtle()

# Set a cycle to blink
for i in range(30000):
turtle.reset()
turtle.pensize(6)
lian()
# Close your eyes
if i%2==0:
hudu(-240,0,-10,-100,127)
hudu(50,0,125,-100,127)
turtle.update()
sleep(0.5)
# open one 's eyes else : hudu(-240,0,-125,110,110) hudu(50,0,-110,110,110)
turtle.fillcolor("black") turtle.begin_fill() hudu(-120,0,0,40,360)
turtle.end_fill() turtle.fillcolor("black") turtle.begin_fill()
hudu(175,0,0,40,360) turtle.end_fill() turtle.update() sleep(0.5)
turtle.exitonclick()

Running results :
Due to unable to upload the animation , Replace it with two graphs of different states , Two pictures can be switched back and forth to achieve animation

Technology