Today is the Dragon Boat Festival, first of all, I wish you a healthy Dragon Boat Festival, when it comes to the Dragon Boat Festival, zongzi is essential, now the kinds of zongzi are also multifarious, but I still like the traditional white brown, what kind of zongzi do you like? While you are eating delicious zongzi, we will draw a plate of zongzi for you.
A sneak peek
Let’s take a look at the final result:
From the figure, we can see that the whole is composed of three parts: plate, zongzi and text. Let’s expand the corresponding implementation.
The plate to achieve
First of all, let’s draw a plate, the composition of the plate is relatively simple, is an ellipse and fill color, code implementation is as follows:
# drawing plate
def plate(a, b, angle, steps, rotateAngle) :
minAngle = (2 * math.pi / 360) * angle / steps
rotateAngle = rotateAngle / 360 * 2 * math.pi
penup() # a pen
setpos(b * math.sin(rotateAngle), -b * math.cos(rotateAngle))
pendown() # put pen to paper
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)
Copy the code
The effect is as follows:
Zongzi implementation
Then, we look at the core part of this article – the implementation of zongzi, the implementation code is as follows:
# painting zongzi
def rice_dumpling() :
pensize(2) # brush width
pencolor(2.51.12) # Brush color
fillcolor(4.77.19) # fill color
begin_fill()
fd(200) # forward
circle(15.120) # draw arc
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()
Copy the code
The effect is as follows:
Text to achieve
We then look at how to add text, such as I want to add text is: I wish you all Dragon Boat Festival ankang, add text is easy, just a line of code, the code is as follows:
write("Wish you all a healthy Dragon Boat Festival.", move=False, align="center", font=("Comic Sans".18."bold"))
Copy the code
All the code in the article has been sorted out for you, there is a need in the public number Python small two background reply Dragon Boat Festival can be obtained.