What is Peppa? Let’s Show You with Python!

What is Peppa? Let's Show You with Python!

Big Data Digest Submission

Author: Ding Yanjun

Just now,

the advertisement “What is Peppa” has gone viral.

Peppa is clearly a comedic character,

yet it moved everyone to tears.

What is Peppa

As the New Year approaches,

the grandfather in the countryside calls his grandson in the city.

The grandson says he wants “Peppa”.

To fulfill his grandson’s wish,

the grandfather starts searching the village for Peppa.

The grandfather’s heartfelt words on the phone are very touching,

all grandfathers in the world love their grandchildren like this,

instantly creating empathy among the audience.

With the question “What is Peppa?”

it sets the stage for the grandfather’s journey to find Peppa,

and in the end, the Peppa he finds is uniquely charming,

but it is the cutest little pig Peppa that the circle owner has ever seen!

What is Peppa? Let's Show You with Python!

I wonder how everyone felt after watching it.

Anyway, I cried after watching it.

I saw many netizens also left comments,

saying they laughed and cried at the same time.

Seeing the grandfather searching the village for Peppa, I felt a bit heartbroken. Therefore, I want to use pure Python to tell the grandfather, what is Peppa?

First, let’s take a look at the effect video:

The basic idea: choose the canvas size, set the pen color and thickness, position correctly, and sequentially draw the nose, head, ears, eyes, cheeks, mouth, body, limbs, and tail, and it’s done.

As we all know, turtle is a built-in module in Python that is quite fun, commonly known as turtle graphics. It is built on the tkinter module and provides some simple drawing tools.

In turtle graphics, we can write commands to make a virtual (imaginary) turtle move back and forth on the screen. This turtle carries a pen, and we can make it draw lines wherever it moves. By writing code to move the turtle in various cool patterns, we can create amazing images. Using turtle graphics, we can create impressive visual effects with just a few lines of code, and we can follow the turtle to see how each line of code affects its movement. This helps us understand the logic of the code. Therefore, turtle graphics is often used as a way for beginners to learn Python. For more detailed functions and knowledge, you can refer to the official documentation:

https://docs.python.org/3/library/turtle.html

After understanding the usage of turtle, we can start the practical work.

Code example:

from turtle import*

def nose(x,y):#Nose
    penup()#Lift pen
    goto(x,y)#Position
    pendown()#Put pen down, start drawing
    setheading(-30)#Set turtle's direction to to_angle/number (0-East, 90-North, 180-West, 270-South)
    begin_fill()#Prepare to fill the shape
    a=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) #Turn left 3 degrees
            forward(a) #Move forward a steps
        else:
            a=a-0.08
            left(3)
            forward(a)
    end_fill()#Filling complete

    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    pencolor(255,155,192)#Pen color
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)#Return or set pencolor and fillcolor
    end_fill()

    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255,155,192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)
    end_fill()

def head(x,y):#Head
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300,-30)
    circle(100,-60)
    circle(80,-100)
    circle(150,-20)
    circle(60,-95)
    setheading(161)
    circle(-300,15)
    penup()
    goto(-100,100)
    pendown()
    setheading(-30)
    a=0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            lt(3) #Turn left 3 degrees
            fd(a) #Move forward a steps
        else:
            a=a-0.08
            lt(3)
            fd(a)
    end_fill()


def cheek(x,y):#Cheek
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()

def mouth(x,y): #Mouth
    color(239,69,19)
    penup()
    goto(x,y)
    pendown()
    setheading(-80)
    circle(30,40)
    circle(40,80)

def setting():          #Parameter settings
    pensize(4)
    hideturtle()        #Make turtle invisible (hide)
    colormode(255)      #Set it to 1.0 or 255. Then the r, g, b values of the color tuple must be in the range of 0 .. cmode
    color((255,155,192),"pink")
    setup(840,500)
    speed(10)

def main():
    setting()           #Canvas and pen settings
    nose(-100,100)      #Nose
    head(-69,167)       #Head
    ears(0,160)         #Ears
    eyes(0,140)         #Eyes
    cheek(80,10)        #Cheek
    mouth(-20,30)       #Mouth
    done()

if __name__ == '__main__':
	main()

The idea is actually quite simple, which is to use the turtle module to achieve basic circles, ellipses, curves, etc. The difficulty lies in how to position each part (it is recommended to sketch first).

What is Peppa? Let's Show You with Python!

The complete code requires 300 lines, and due to space limitations, only part of the code is provided. Friends who want the complete source code can follow Big Data Digest and reply Peppa to obtain it.

What is Peppa? Let's Show You with Python!What is Peppa? Let's Show You with Python!

What is Peppa? Let's Show You with Python!I heard that those who clicked “Like” have become more attractive!

Leave a Comment