leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python turtle module example: draw Peppa Pig (below)

Estimated reading: 4 minutes 14 views

In this tutorial, we will finish drawing Peppa Pig.

feet() function

Next, we define the feet() function, which is used to draw the feet. The code for the feet() function is shown below.

def feet():           
    #leftleg
    pensize(10)
    color((240,128,128))
    penup()
    goto(2,-177)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    #left foot
    color("black")
    pensize(15)
    forward(20)
    #right leg
    pensize(10)
    color((240,128,128))
    penup()
    setheading(90)
    forward(40)
    setheading(0)
    forward(90)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    #right foot
    color("black")
    pensize(15)
    forward(20)

For the sake of simplicity, we use straight lines to represent the legs and feet, call this function, and see the drawing effect, as shown in Figure 1.

python feet() function
figure 1

tail() function

The tail() function is defined here to draw the tail. The code for the tail() function is shown below.

def tail():
     pensize(4)
     color((255,155,192))
     penup()
     goto(148,-155)
     pendown()
     setheading(0)
     #rolled tail
     circle(70,20)
     circle(10,330)
     circle(70,30)

Call this function to see the effect, as shown in Figure 2.

python tail() function
figure 2
At this point, our little pig Peppa has been drawn. Does it look similar to the original picture?

Did you draw successfully? The following is the complete code of Peppa Pig. If there is an error, please modify it in time;

from turtle import *
def nose(): #nose
    penup()
    goto(-100,100)
    setheading(-30)
    color((255,155,192),"pink") #The brush color is light pink, and the fill color is pink
    pendown()
    begin_fill()
    #Draw an ellipse as the outline of the nose
    segment=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            segment=segment+0.08
            left(3)
            forward(segment)
        else:
            segment=segment-0.08
            left(3)
            forward(segment)
    end_fill()
    #left nostril
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    color((255,155,192),(160,82,45)) #The brush color is light pink, and the fill color is loess ochre
    pendown()
    begin_fill()
    circle(5)
    end_fill()
    #right nostril
    penup()
    setheading(0)
    forward(20)
    pendown()
    begin_fill()
    circle(5)
    end_fill()
def head():#head
    penup()
    goto(-69,167)
    pendown()
    color((255,155,192),"pink")
    begin_fill()
    setheading(180)
    circle(300,-30)
    circle(100,-60)
    circle(80,-100)
    circle(150,-20)
    circle(60,-95)
    setheading(161)
    circle(-300,15)
   
    #Outline the outline of the right half of the nose to avoid covering it when filling
    penup()
    goto(-100,100)
    pendown()
    setheading(-30)
    segment=0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            segment=segment+0.08
            left(3)
            forward(segment)
        else:
            segment=segment-0.08
            left(3)
            forward(segment)
    end_fill()
def ears(): #ears
    color((255,155,192),"pink")
    #left ear
    penup()
    goto(0,160)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,54)
    end_fill()
    #right ear
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,56)
    end_fill()
def eyes(): #eyes
    #left eye frame
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    #left eyeball
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    #right eye frame
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-25)
    setheading(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    #right eyeball
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
def cheek():# cheek
    penup()
    goto(80,10)
    setheading(0)
    color((255,155,192))
    pendown()
    begin_fill()
    circle(30)
    end_fill()
def mouth(): #mouth
    penup()
    goto(-20,30)
    color(239,69,19)
    pendown()
    setheading(-80)
    circle(35,120)
def body():#body
    color("red",(255,99,71))
    #The curve on the left side of the body
    penup()
    goto(-32,-8)
    pendown()
    begin_fill()
    setheading(-130)
    circle(100,10)
    circle(300,30)
    #body bottom
    setheading(0)
    forward(230)
    #The curve on the right side of the body
    setheading(90)
    circle(300,30)
    circle(100,3)
    color((255,155,192),(255,100,100))
    #Draw the chin on the face to avoid covering it when filling
    setheading(-135)
    circle(-80,63)
    circle(-150,24)
    end_fill()
def hands():#hands
    color((255,155,192))
    # middle finger of left hand
    penup()
    goto(-56,-45)
    pendown()
    setheading(-160)
    circle(300,15)
    # Represent the other two fingers by an arc
    penup()
    setheading(90)
    forward(15)
    setheading(0)
    pendown()
    setheading(-10)
    circle(-20,90)
    #middle finger of right hand
    penup()
    setheading(90)
    forward(30)
    setheading(0)
    forward(237)
    pendown()
    setheading(-20)
    circle(-300,15)
    # Represent the other two fingers by an arc
    penup()
    setheading(90)
    forward(20)
    setheading(0)
    pendown()
    setheading(-170)
    circle(20,90)
def feet():#legs and feet
    #leftleg
    pensize(10)
    color((240,128,128))
    penup()
    goto(2,-177)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    #leftfoot
    color("black")
    pensize(15)
    forward(20)
    #rightleg
    pensize(10)
    color((240,128,128))
    penup()
    setheading(90)
    forward(40)
    setheading(0)
    forward(90)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    #rightfoot
    color("black")
    pensize(15)
    forward(20)
def tail():#tail
    pensize(4)
    color((255,155,192))
    penup()
    goto(148,-155)
    pendown()
    setheading(0)
    #rolled tail
    circle(70,20)
    circle(10,330)
    circle(70,30)
def setting(): #Parameter setting
    #Set the window size
    setup(800,500)
    #setup brush
    pensize(4)
    hideturtle()
    colormode(255)
    speed(20)
setting() #Canvas, brush settings
nose() #nose
head() #head
ears() #ears
eyes() #eyes
cheek() # cheek
mouth() #mouth
body() #body
hands() #hands
feet() #foot
tail() #tail

Leave a Comment

CONTENTS