Dragon Boat Festival Zongzi (python)

Table of contents

foreword

text

 Zongye drawing

Zongye rope drawing

Blessing drawing

source code

 Summarize


foreword

Today is the Dragon Boat Festival, and then yesterday I also learned about drawing, and then I wanted to see if I could draw it, but it turned out to be a bit difficult, and it is hard to describe it with CharAI. Later, I found one and changed it.

Dragon Boat Festival Ankang, use python to draw rice dumplings for you~jiujiu_python code for drawing rice dumplings_they call me the blog of the technical director-CSDN blog icon-default.png?t=N5K3https://blog.csdn.net/qq_29061315/article/details/125092068

text

I still encountered a few problems when I changed it. First, I misunderstood the goto function. It used the middle of the screen as the origin. When I was learning other languages, I remembered that I used the upper left corner of the screen as the origin. Python's turtle library recognizes and learns (4) the position of the turtle (turtle.goto())icon-default.png?t=N5K3

 Zongye drawing

In this case, it is necessary to reflect the three-dimensional effect by first drawing the main surface and then the side. I feel that this drawing either requires a particularly clear mind, that is, where it moves, and which direction the angle is facing, or it can only be like me. After debugging, if the angle is wrong or the moving distance is wrong, it takes a long time to modify each time.

The color of my brush here is still black, and the color of the zong leaves is pure green. If the brush is also green, the painting will not feel very beautiful, and the lines will not be reflected. The front side can be written with a loop, but the sides cannot be written together if the angles are different. Here we need to save a point for the convenience of drawing encapsulation lines later.

# 画笔宽度
pensize(2)
# 画笔颜色
pencolor("black")
# 粽子大体的填充色
fillcolor("green")
#开始填充
begin_fill()
#绘制粽子的正面
for i in range(3):
    #forward,在当前位置方向移动一定的距离
    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)
#向右旋转60
right(60)
#向后移动105
back(105)
#表存当前的坐标点
a = pos()
pendown()

Zongye rope drawing

The color of the zongye rope used here is dark khaki. You can modify it according to your own aesthetics. You can find it directly on the Internet or use RGB to refine it. Finally, you can use hideturtle() to hide the arrow to make it more beautiful. When the arrow is turned At this time, it is best to raise the brush and close the fill. Penup() and end_fill() adjust the position and angle before turning it on, unless your program deliberately sets it this way. Of course, it is necessary to see the angle and position of the brush more intuitively. to adjust.

#画笔颜色
color("black")
#带子的颜色(深卡其色)
fillcolor("darkkhaki")
#绘制正面的带子
begin_fill()
fd(120)
goto(a)
#pen up调整位置
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()

#画下面的那条带子
#绘制正面的带子
#摆正他的方向
right(50)
#得到a点的坐标
x,y=a
#让a点坐标向右下靠
x=x+30
y=y-50
b=x,y
#提起画笔把初始的位置什么设置好(角度和准备),此时不能填充
penup()
fd(120)
goto(b)
back(15)
left(90)
fd(20)
right(90)
#配置好了之后就可以填充了
begin_fill()
pendown()
#先画下面的直线
fd(210)
right(120)
#右边的直线
fd(24)
right(60)
#上面的直线
fd(180)
right(60)
#左边的直线
fd(24)
end_fill()
#侧面的带子
begin_fill()
left(110)
fd(90)
left(100)
fd(24)
left(80)
fd(75)
#结束填充
end_fill()

# 隐藏turtle图形(箭头)
hideturtle()

Blessing drawing

This is what should be mentioned before goto, otherwise it will leave traces along the way, call the write function to input blessings and font related information.

#输出祝福语
#将海龟笔尖提起
penup()
goto(-80,-160)
pendown()
write("端午安康",font=('楷体', 30, 'bold'))

If you want to keep the interface at the end, you still have to do it, otherwise it will be closed directly, and debugging is not easy to debug.

# 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭pythonTurtle图形化窗口为止
done()

source code

from turtle import *
# 画粽子
#将海龟笔尖提起
penup()
#将海龟图形移动到画布上指定的位置(算是为了居中绘画吧)
goto(-100,-50)
#将海龟笔尖落下
pendown()

# 画笔宽度
pensize(2)
# 画笔颜色
pencolor("black")
# 粽子大体的填充色
fillcolor("green")
#开始填充
begin_fill()
#绘制粽子的正面
for i in range(3):
    #forward,在当前位置方向移动一定的距离
    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)
#向右旋转60
right(60)
#向后移动105
back(105)
#表存当前的坐标点
a = pos()
pendown()

#画笔颜色
color("black")
#带子的颜色(深卡其色)
fillcolor("darkkhaki")
#绘制正面的带子
begin_fill()
fd(120)
goto(a)
#pen up调整位置
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()

#画下面的那条带子
#绘制正面的带子
#摆正他的方向
right(50)
#得到a点的坐标
x,y=a
#让a点坐标向右下靠
x=x+30
y=y-50
b=x,y
#提起画笔把初始的位置什么设置好(角度和准备),此时不能填充
penup()
fd(120)
goto(b)
back(15)
left(90)
fd(20)
right(90)
#配置好了之后就可以填充了
begin_fill()
pendown()
#先画下面的直线
fd(210)
right(120)
#右边的直线
fd(24)
right(60)
#上面的直线
fd(180)
right(60)
#左边的直线
fd(24)
end_fill()
#侧面的带子
begin_fill()
left(110)
fd(90)
left(100)
fd(24)
left(80)
fd(75)
#结束填充
end_fill()

# 隐藏turtle图形(箭头)
hideturtle()

#输出祝福语
#将海龟笔尖提起
penup()
goto(-80,-160)
pendown()
write("端午安康",font=('楷体', 30, 'bold'))
# 暂停程序,停止画笔绘制,但绘图窗体不关闭,直到用户关闭pythonTurtle图形化窗口为止
done()

 Summarize

It's a bit crude, but the blessing is sincere:

May your mood be full like a rice dumpling, your career and family closely connected like a rice dumpling leaf, and be happy! ! !

peace and joy

Guess you like

Origin blog.csdn.net/weixin_64066303/article/details/131338766