端午節,我用 Python 畫了一盤糉子送給你們

今天是端午節,首先祝你們端午安康,說到端午節,糉子則是必不可少的,如今糉子的種類也是五花八門,但我仍是喜歡傳統的白棕子,你喜歡哪一種糉子呢?在你們吃着美味糉子的同時,本文咱們畫一盤糉子送給你們。python

先睹爲快

咱們先來欣賞一下最終的效果圖:spa

從圖中咱們能夠看出總體分三部分組成:盤子、糉子、文字,下面咱們展開來講一下相應實現。code

盤子實現

首先,咱們來畫一個盤子,盤子的組成比較簡單,就是一個橢圓再加上填充色,代碼實現以下:ci

# 畫盤子
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)

效果以下:it

糉子實現

接着,咱們看一下本文最核心的部分-糉子的實現,實現代碼以下:class

# 畫糉子
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()

效果以下:後臺

文字實現

咱們再接着看一下如何添加文字,好比我要添加的文字是:祝你們端午安康,添加文字實現很容易,只需一行代碼便可,代碼實現以下:im

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

文中所有代碼已經爲你們整理好了,有須要的在公衆號Python小二後臺回覆端午便可獲取。next

相關文章
相關標籤/搜索