头图

Today is the Dragon Boat Festival. First of all, I wish everyone a good Dragon Boat Festival. Speaking of the Dragon Boat Festival, Zongzi is essential. Now there are many types of Zongzi, but I still like the traditional white palm. Which kind of zongzi do you like? While everyone is eating delicious rice dumplings, in this article we will draw a plate of rice dumplings for everyone.

A sneak peek

Let's first appreciate the final renderings:

From the figure, we can see that the whole is composed of three parts: plate, rice dumpling, and text. Let's expand on the corresponding realization below.

Plate realization

First, let's draw a plate. The composition of the plate is relatively simple. It is an ellipse plus a fill color. The code is implemented as follows:

# 画盘子
def plate(a, b, angle, steps, rotateAngle):
    minAngle = (2 * math.pi / 360) * angle / steps
    rotateAngle = rotateAngle / 360 * 2 * math.pi
    penup() # 起笔
    setpos(b * math.sin(rotateAngle), -b * math.cos(rotateAngle))
    pendown() # 落笔
    for i in range(steps):
        nextPoint = [a * math.sin((i + 1) * minAngle), -b * math.cos((i + 1) * minAngle)]
        nextPoint = [nextPoint[0] * math.cos(rotateAngle) - nextPoint[1] * math.sin(rotateAngle),
                     nextPoint[0] * math.sin(rotateAngle) + nextPoint[1] * math.cos(rotateAngle)]
        setpos(nextPoint)

The effect is as follows:

Zongzi Realization

Next, let's take a look at the core part of this article-the implementation of zongzi, the implementation code is as follows:

# 画粽子
def rice_dumpling():
    pensize(2) # 画笔宽度
    pencolor(2, 51, 12) # 画笔颜色
    fillcolor(4, 77, 19) # 填充色
    begin_fill()
    fd(200) # 向前
    circle(15, 120) #画圆弧
    fd(200)
    circle(15, 120)
    fd(200)
    circle(15, 120)
    fd(200)
    circle(15, 60)
    fd(100)
    circle(15, 90)
    fd(173)
    circle(1, 90)
    end_fill()
    penup()
    fd(100)
    right(60)
    back(105)
    a = pos()
    pendown()
    color(60, 67, 0)
    fillcolor(85, 97, 9)
    begin_fill()
    fd(120)
    goto(a)
    penup()
    back(15)
    left(90)
    fd(20)
    right(90)
    pendown()
    fd(150)
    right(120)
    fd(24)
    right(60)
    fd(120)
    right(60)
    fd(24)
    end_fill()
    begin_fill()
    left(110)
    fd(65)
    left(100)
    fd(24)
    left(80)
    fd(50)
    end_fill()

The effect is as follows:

Text realization

Let's take a look at how to add text again. For example, the text I want to add is: I wish you all a good Dragon Boat Festival. Adding text is easy to implement. It only needs one line of code. The code implementation is as follows:

write("祝大家端午安康", move=False, align="center", font=("Comic Sans", 18, "bold"))

This paper has all the code for everyone organized, there is a need in the public No. Python small two reply back Dragon can get.


Python小二
180 声望414 粉丝