A trick is hard to find? Bing dwen dwen, then draw an ice pier.

Recently, the Beijing Winter Olympic Games are in full swing. Some athletes have created legends through their efforts. Although some athletes have paid a lot, they still haven't reached the other side of the ideal.

Anyway, they have gained a valuable experience in life. Let's cheer for the Olympic athletes.

Beijing Winter Olympics

Cheer for the Olympic athletes

Bing dwen dwen became a "top stream" with the Winter Olympic Games going on.

The official account Bing dwen dwen also wrote a message in the background, trying to draw an ice pier with python. This article will explore the process of drawing with you.

Contents of this article

  1. Effect display
  2. Code explanation 2.1 import and stock in 2.2 playing music 2.3 draw Bing dwen dwen outside. 2.4 draw inner ears 2.5 draw eyes 2.6 painting mouth

1, Effect display

Before officially entering the code explanation, let's take a look at the implementation effect of this article.

http://mpvideo.qpic.cn/0bc33eackaaavmajsjiiwnrfbwodexmqajia.f10002.mp4?dis_k=af09fdf65560ebf28f93bed19b120153&dis_t=1645755479&vid=wxv_2267397590713630726&format_id=10002&support_redirect=0&mmversion=false

2, Code explanation

The principle of python Bing dwen dwen is: first, use turtle library to draw the external contour, and then draw different modules of inner ear, hand, foot, eyes, nose and mouth.

1. Import and stock in

First, import the libraries that need to be loaded in this article. If some of your libraries have not been installed, resulting in errors in running code, you can install them in Anaconda Prompt with pip method.

import pygame
import turtle as t

This article applies to fewer libraries, only pygame and turtle.

The turtle library is a drawing library, which is equivalent to giving you a brush. You can complete the drawing on the canvas with the code controlled by mathematical logic.

pygame library is to make the drawing process more interesting. Background music is added in the drawing process.

2 play music

Next, Bing dwen dwen's pygame library to play background music. This music is a song about ice pier.

#Play music
pygame.mixer.init()
pygame.mixer.music.load(r"F:\official account\46.Bing dwen dwen\Wang Yi,Sakagawa - 2022 Bing dwen dwen (mascot) in Winter Olympics.mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play()

3 draw Bing dwen dwen outside.

And then enter Bing dwen dwen's formal drawing process, first drawing the outer contour.

##Outline outside the painting
t.title('Bing Dwen Dwen')
t.pensize(2)
#t.color('gray')
t.color('lavender')
#Draw ears
t.penup()
t.goto(-50, 160)
t.pendown()
t.left(120)
t.circle(20, 180)
#Draw the left body
t.right(60)
t.circle(200, 20)
#Draw left hand
t.right(30)
t.circle(200,15)
#t.right(20)
t.circle(15, 230)
#Draw left leg
t.right(180)
t.circle(-150, 40)
t.left(80)
t.circle(50, 50)
#Draw the arc in the middle of the leg
t.left(70)
t.circle(-20, 200)
#Draw the right leg
t.left(90)
t.circle(70, 35)
t.left(90)
t.circle(-80, 25)
t.right(30)
t.circle(200, 28)
#Draw right hand
t.right(45)
t.circle(200, 15)
t.left(20)
t.circle(15, 190)
t.right(30)
t.circle(200, 5)
#Draw right ear
t.right(150)
t.circle(150, 25)
t.right(40)
t.circle(20, 180)
t.right(80)
t.circle(600, 5)

Detailed explanation of key codes:

t. Pen size (width): sets the size of the brush.

t. Color: sets the color of the brush.

t.penup(): lift the brush, which is generally used to draw in another place.

t.goto(x,y): the brush goes to a certain position. The parameter is (x,y), which corresponds to the abscissa and ordinate of the brush.

t.pendown(): put down the brush, which is generally used in combination with penup.

t.left(degree): how many degrees does the brush turn to the left? Degrees are indicated in brackets.

t.right(degree): how many degrees does the brush turn to the right? The bracket indicates the degree.

t.circle(radius,extent,steps): radius refers to the radius. If it is positive, the radius is far from the radius on the left side of the little turtle; if it is negative, the radius is far from the radius on the right side of the little turtle; Extent refers to radian; Steps is the order.

Bing dwen dwen is the key to drawing out the contour. It regulates the radian of the curve by adjusting the radius and radian of the circle function, so that the contour of the ice pier is smooth.

4 draw inner ear

After drawing the outer outline, you can draw the inner components in modules. This section draws the ear, which is divided into left ear and right ear.

##Draw inner ear
#Left
t.pensize(1)
t.penup()
t.goto(-53, 158)
t.pendown()
t.color('gray', 'black')
t.begin_fill()
t.right(50)
t.circle(16, 180)
t.end_fill()
#right
t.penup()
t.goto(35, 142)
t.pendown()
t.color('gray', 'black')
t.begin_fill()
t.left(120)
t.circle(15, 180)
t.end_fill()

Detailed explanation of key codes:

t.begin_fill(): start filling.

t.end_fill(): end filling.

Other codes have been introduced in Section 3, so this section only covers begin that was not mentioned before_ Fill and end_fill function.

5 draw eyes

This section introduces the code of drawing eyes. In order to look better, we need to pay attention to the symmetry of the eyes.

##Draw eyes
#Draw left eye
t.penup()
t.goto(-40, 100)
t.pendown()
t.color('black')
t.begin_fill()
t.circle(15, 170)
t.circle(100, 20)
t.circle(16, 160)
t.left(2)
t.circle(100, 19)
t.end_fill()
#Draw right eye
t.penup()
t.goto(-10, 100)
t.pendown()
t.color('black')
t.begin_fill()
t.left(50)
t.circle(-15, 170)
t.circle(-100, 20)
t.circle(-16, 160)
t.right(2)
t.circle(-100, 19)
t.end_fill()
t.goto(-8, 95)
t.color('white')
t.begin_fill()
t.circle(-10, 360)
t.end_fill()
t.goto(-6, 95)
t.color('#341c02')
t.begin_fill()
t.circle(-8, 360)
t.end_fill()
t.penup()
t.goto(-3, 93)
t.color('black')
t.begin_fill()
t.circle(-5, 360)
t.end_fill()
t.penup()
t.goto(-1, 100)
t.color('white')
t.begin_fill()
t.circle(-2.5, 360)
t.end_fill()
#Draw the eyeball part of the left eye
t.penup()
t.goto(-43, 95)
t.color('white')
t.begin_fill()
t.right(30)
t.circle(10, 360)
t.end_fill()
t.penup()
t.goto(-47, 91)
t.color('#341c02')
t.begin_fill()
t.right(30)
t.circle(8, 360)
t.end_fill()
t.penup()
t.goto(-50, 90)
t.color('black')
t.begin_fill()
t.circle(5, 360)
t.end_fill()
t.penup()
t.goto(-52, 98)
t.color('white')
t.begin_fill()
t.circle(2.3, 360)
t.end_fill()

6 draw mouth

This section introduces drawing mouth. I thought it was easy to draw mouth, but there were some bug s in the drawing process.

Experience suggests that places with filling should be drawn along one direction, otherwise there will be redundant filling.

#Draw mouth
t.penup()
t.goto(-41, 76)
t.pendown()
t.color('black')
t.begin_fill()
t.pensize(0.1)
t.right(50)
t.circle(26, 90)
t.penup()
t.goto(-8, 75)
t.pendown()
t.left(170)
t.circle(-25,86)
t.penup()
t.goto(-41, 77)
t.pendown()
t.penup()
t.goto(-41, 76)
t.pendown()
t.end_fill()
t.goto(-21, 70)
t.pendown()
t.pensize(3)
t.end_fill()
t.color('#ec2d01')
t.left(60)
t.circle(-10, 30)

The functions used in other codes are also roughly the same. Due to space reasons, this article will not show them one by one.

Bing dwen dwen can be used to restore the "ice pier" in the background of official account, and get the complete source code of Baidu SkyDrive link.

Added by priya_cks on Fri, 25 Feb 2022 05:01:13 +0200