Home >Backend Development >Python Tutorial >How to write the code for drawing Bingdundun in python
Code detailed steps: 1. Import the necessary libraries and modules; 2. Create a canvas and brush; 3. Set the properties of the brush; 4. Draw each part of the ice and set the corresponding parameters. ; 5. Use the statement "turtle.done()" to end the drawing; 6. Copy these codes to the Python development environment. After running, you can see the drawn ice shapes and so on.
Operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.
Detailed steps of the code for drawing Bingdundun in Python:
1. Import the necessary libraries and modules:
import turtle
2. Create A canvas and brush:
window = turtle.Screen() window.bgcolor("white") pen = turtle.Turtle()
3. Set the properties of the brush:
pen.speed(5) # 设置画笔的速度 pen.pensize(3) # 设置画笔的粗细 pen.color("black") # 设置画笔的颜色
4. Draw Bingdundun’s body:
pen.penup() pen.goto(0, -100) # 将画笔移动到指定位置 pen.pendown() pen.begin_fill() # 开始填充形状 pen.circle(100) # 画一个半径为100的圆形 pen.end_fill() # 结束填充形状
Draw Bing Dundun’s eyes:
pen.penup() pen.goto(-40, 60) pen.pendown() pen.begin_fill() pen.circle(20) pen.end_fill() pen.penup() pen.goto(40, 60) pen.pendown() pen.begin_fill() pen.circle(20) pen.end_fill()
Draw Bing Dundun’s mouth:
pen.penup() pen.goto(-40, 20) pen.pendown() pen.setheading(-60) # 设置画笔的角度 pen.circle(40, 120) # 画一个半径为40的圆弧,弧长为120度 pen.setheading(0) # 将画笔的角度恢复到初始状态
Draw Bing Dundun’s arms:
pen.penup() pen.goto(-80, -20) pen.pendown() pen.setheading(-30) pen.forward(80) pen.backward(80) pen.setheading(30) pen.forward(80) pen.backward(80)
Draw Bing Dundun’s hat:
pen.penup() pen.goto(-100, 120) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(200) pen.left(90) pen.forward(50) pen.left(90) pen.forward(50) pen.right(90) pen.forward(100) pen.right(90) pen.forward(50) pen.left(90) pen.forward(50) pen.left(90) pen.forward(150) pen.end_fill()
5. End the drawing:
turtle.done()
6, Copy these codes to the Python development environment. After running, you can see the drawn ice shape.
The above are the code steps for drawing ice cubes in Python. You can copy these codes into the Python development environment, and after running them, you can see the drawn ice shapes.
The above is the detailed content of How to write the code for drawing Bingdundun in python. For more information, please follow other related articles on the PHP Chinese website!