SoFunction
Updated on 2024-11-21

Python using turtle library to draw Peppa Pig (example code)

Turtle is one of the important standard libraries of Python, which is capable of basic graphical drawing. turtle graphical drawing concept was born in 1969, and successfully applied to the LOGO programming language.

The turtle library has a basic framework for drawing graphics: a small turtle crawls through a coordinate system, and its trajectory forms the drawing. At the beginning of the drawing, the turtle is located in the center of the canvas, where the coordinates are (0,0) and the direction of travel is to the right of the horizontal.
The files can be found in the Lib folder of the installation directory of the Python 3 series version.

The following code gives you an introduction to Python using the turtle library to draw Peppa Pig.

The specific code is shown below:

# -*- coding:utf-8 -*-
import turtle as t
import time
def main():
 (4)
 ()
 (255)
 ((255, 155, 192), "pink")
 (840, 500)
 (10)
 # Nose #
 ()
 (-100, 100)
 ()
 (-30)
 t.begin_fill()
 a = 0.4
 for i in range(120):
  if 0 <= i < 30 or 60 <= i < 90:
   a = a + 0.08
   (3) # Turn 3 degrees to the left
   (a) # Steps forward a
  else:
   a = a - 0.08
   (3)
   (a)
 t.end_fill()
 ()
 (90)
 (25)
 (0)
 (10)
 ()
 (255, 155, 192)
 (10)
 t.begin_fill()
 (5)
 (160, 82, 45)
 t.end_fill()
 ()
 (0)
 (20)
 ()
 (255, 155, 192)
 (10)
 t.begin_fill()
 (5)
 (160, 82, 45)
 t.end_fill()
 # Head
 ((255, 155, 192), "pink")
 ()
 (90)
 (41)
 (0)
 (0)
 ()
 t.begin_fill()
 (180)
 (300, -30)
 (100, -60)
 (80, -100)
 (150, -20)
 (60, -95)
 (161)
 (-300, 15)
 ()
 (-100, 100)
 ()
 (-30)
 a = 0.4
 for i in range(60):
  if 0 <= i < 30 or 60 <= i < 90:
   a = a + 0.08
   (3) # Turn 3 degrees to the left
   (a) # Steps forward a
  else:
   a = a - 0.08
   (3)
   (a)
 t.end_fill()
 # Ears
 ((255, 155, 192), "pink")
 ()
 (90)
 (-7)
 (0)
 (70)
 ()
 t.begin_fill()
 (100)
 (-50, 50)
 (-10, 120)
 (-50, 54)
 t.end_fill()
 ()
 (90)
 (-12)
 (0)
 (30)
 ()
 t.begin_fill()
 (100)
 (-50, 50)
 (-10, 120)
 (-50, 56)
 t.end_fill()
 # Eyes
 ((255, 155, 192), "white")
 ()
 (90)
 (-20)
 (0)
 (-95)
 ()
 t.begin_fill()
 (15)
 t.end_fill()
 ("black")
 ()
 (90)
 (12)
 (0)
 (-3)
 ()
 t.begin_fill()
 (3)
 t.end_fill()
 ((255, 155, 192), "white")
 ()
 (90)
 (-25)
 (0)
 (40)
 ()
 t.begin_fill()
 (15)
 t.end_fill()
 ("black")
 ()
 (90)
 (12)
 (0)
 (-3)
 ()
 t.begin_fill()
 (3)
 t.end_fill()
 # Blush
 ((255, 155, 192))
 ()
 (90)
 (-95)
 (0)
 (65)
 ()
 t.begin_fill()
 (30)
 t.end_fill()
 # Mouth
 (239, 69, 19)
 ()
 (90)
 (15)
 (0)
 (-100)
 ()
 (-80)
 (30, 40)
 (40, 80)
 # The body
 ("red", (255, 99, 71))
 ()
 (90)
 (-20)
 (0)
 (-78)
 ()
 t.begin_fill()
 (-130)
 (100, 10)
 (300, 30)
 (0)
 (230)
 (90)
 (300, 30)
 (100, 3)
 ((255, 155, 192), (255, 100, 100))
 (-135)
 (-80, 63)
 (-150, 24)
 t.end_fill()
 # Hands
 ((255, 155, 192))
 ()
 (90)
 (-40)
 (0)
 (-27)
 ()
 (-160)
 (300, 15)
 ()
 (90)
 (15)
 (0)
 (0)
 ()
 (-10)
 (-20, 90)
 ()
 (90)
 (30)
 (0)
 (237)
 ()
 (-20)
 (-300, 15)
 ()
 (90)
 (20)
 (0)
 (0)
 ()
 (-170)
 (20, 90)
 # Feet
 (10)
 ((240, 128, 128))
 ()
 (90)
 (-75)
 (0)
 (-180)
 ()
 (-90)
 (40)
 (-180)
 ("black")
 (15)
 (20)
 (10)
 ((240, 128, 128))
 ()
 (90)
 (40)
 (0)
 (90)
 ()
 (-90)
 (40)
 (-180)
 ("black")
 (15)
 (20)
 # Tail
 (4)
 ((255, 155, 192))
 ()
 (90)
 (70)
 (0)
 (95)
 ()
 (0)
 (70, 20)
 (10, 330)
if __name__ == '__main__':
 # Drawing Peppa Pig
 main()
 (10)

Implementation results:

小猪佩奇

summarize

The above is a small introduction to Python using turtle library to draw piggy Peach, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!