端午了,我用 Python 畫了一盤粽子!
大家好,我是杰哥。
今天是端午節(jié)了,用 Python 畫一盤粽子送給大家,用到的 Python 庫還是大家比較熟悉的 turtle,在這里祝大家端午安康。
首先,我們來畫一個盤子,代碼實現如下:
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)
看一下效果:

接著,我們再來畫粽子,代碼實現如下:
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 繪制桑基圖!| 用戶行為路徑分析
用 Python 批量提取 PDF 的圖片,并存儲到指定文件夾
用 Python 批量提取 PDF 的表格數據,保存為 Excel
在公眾號后臺回復:JGNB,可獲取杰哥原創(chuàng)的 PDF 手冊。

評論
圖片
表情
