
今天是端午節,首先祝大家端午安康,說到端午節,粽子則是必不可少的,現在粽子的種類也是五花八門,但我還是喜歡傳統的白棕子,你喜歡哪種粽子呢?在大家吃著美味粽子的同時,本文我們畫一盤粽子送給大家,
先睹為快
我們先來欣賞一下最終的效果圖:

從圖中我們可以看出整體分三部分組成:盤子、粽子、文字,下面我們展開來說一下相應實作,
盤子實作
首先,我們來畫一個盤子,盤子的組成比較簡單,就是一個橢圓再加上填充色,代碼實作如下:
# 畫盤子
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)
效果如下:

粽子實作
接著,我們看一下本文最核心的部分-粽子的實作,實作代碼如下:
# 畫粽子
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()
效果如下:

文字實作
我們再接著看一下如何添加文字,比如我要添加的文字是:祝大家端午安康,添加文字實作很容易,只需一行代碼即可,代碼實作如下:
write("祝大家端午安康", move=False, align="center", font=("Comic Sans", 18, "bold"))
文中全部代碼已經為大家整理好了,有需要的在公眾號Python小二后臺回復端午即可獲取,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/287616.html
標籤:python
上一篇:去攜程實習了!半年時間,從機械轉行 Java,二哥的讀者真牛逼!
下一篇:性能優化案例之app卡頓優化
