Drawing rice dumplings using Python’s turtle module

Introduction: In this blog, we will use Python's turtle module to draw the traditional Chinese food "zongzi". Zongzi are dumplings made of glutinous rice wrapped in bamboo leaves and are usually eaten during the Dragon Boat Festival. We'll walk through the code step by step and explain each component to understand how to draw rice dumplings.

Preparation: First, we need to import the turtle module, which is a Python library specifically used for drawing graphics. After importing using from turtle import *, we can use various methods and commands to control the drawing turtle.

from turtle import *

Hiding turtles and color mode settings: To make the drawing clearer, we can hide the visualization of the turtles. By calling the hideturtle() method, we can hide the turtle so that we can observe the drawing results. In addition, we also need to set the color mode to 255 colors. This way, we can use RGB values ​​to specify colors.

colormode(255)

'''
参数说明:
pensize:画笔粗细
pencolor:画笔颜色
fillcolor:填充颜色
begin_fill:开始填充
fd:前进
circle:画圆
right:右转
end_fill():结束填充
'''

Drawing Zongzi: Next, we define a method called Zongz, which accepts two parameters x and y to specify the starting position of the zongzi. Comments within methods describe the role of each variable and method.


def Zongz(x, y):
    # 根据输入的坐标调整起点
    penup()
    home()
    goto(x, y)
    pendown()

    # 设置颜色及画笔属性
    pensize(2)
    pencolor(0, 0, 0)
    fillcolor(4, 77, 19)

    # 绘制粽子
    begin_fill()
    fd(200)
    circle(15, 120)
    fd(200)
    circle(15, 120)
    fd(200)
    circle(15, 120)
    end_fill()
    fillcolor(4, 77, 19)
    begin_fill()
    fd(200)
    circle(15, 60)
    fd(100)
    circle(15, 90)
    fd(173)
    circle(1, 150)
    end_fill()
    fd(205)
    circle(-15, 120)
    fd(197)
    circle(-15, 120)
    fd(200)

First, we use the penup() and pendown() methods to lift and lower the turtle's pen

down to adjust the drawing starting point. Then, we use the home() method to move the turtle to the original position, and then use the goto() method to move the turtle to the specified coordinates (x, y).

Then, we set the thickness and color of the brush, using the pensize() and pencolor() methods to set the thickness and color of the brush respectively. Use the fillcolor() method to set the fill color. These properties will determine the appearance of our rice dumplings.

Next, we start filling the color using the begin_fill() method. Then, we use the fd() and circle() methods to draw the shape of the rice dumpling. These methods are used to move forward a certain distance and draw arcs.

After drawing the main part of the rice dumpling, we use the end_fill() method to end the filling. Then, we adjust the position and start drawing the knot of the rice dumpling. We use the circle(), fd(), and right() methods to adjust the position, and the color() and fillcolor() methods to set the color.

# 调整位置绘制粽子结
    penup()
    circle(-12, 120)
    circle(-1, 60)
    fd(100)
    right(60)
    back(105)
    a = pos()
    pendown()

    # 绘制粽子结
    color(60, 67, 0)
    fillcolor(85, 97, 9)
    begin_fill()
    fd(122)
    goto(a)
    left(120)
    fd(24)
    right(120)
    fd(150)
    right(120)
    fd(24)
    right(60)
    fd(124)
    end_fill()
    right(60)
    fd(24)
    begin_fill()
    left(110)
    fd(67)
    left(100)
    fd(24)
    left(80)
    fd(52)
    end_fill()

    # 回到远点(朝向东)
    penup()
    goto(0, 0)
    home()
    pendown()


Zongz(50, 0)
Zongz(-100, 0)
Zongz(-250, 0)
mainloop()

Finally, we move the turtle's position back to its original position and use the mainloop() method to keep the drawing window open so that we can observe the drawing results.

 

Summary: By using the turtle module, we can draw the traditional Chinese food Zongzi using Python code. By controlling the movement of the turtle and setting the color, we can draw realistic zongzi shapes and colors. I hope this blog can help readers learn to use the turtle module for graphics drawing. If you are interested in drawing other graphics or learning more about the turtle module, you can delve further. Let's explore the beauty of the combination of programming and art!

Guess you like

Origin blog.csdn.net/m0_55813592/article/details/131387199