天天看點

【案例】用 turtle 繪制一個月餅

文章目錄

  • ​​環境準備​​
  • ​​turtle 庫​​
  • ​​源代碼​​
  • ​​封裝一個自己的函數​​
  • ​​月餅主體​​
  • ​​寫一段文字​​
  • ​​最終效果​​

2022.9.10 既是中秋節又是教師節,Jia ming 在這裡祝大家阖家團圓,中秋快樂,也祝天下的老師桃李滿園,教師節快樂!這裡為大家呈現上一個 Python 小項目 —— 使用 turtle 繪制月餅。

月。光輝,皎潔。耀乾坤,靜空闊。圓滿中秋,玩争詩哲。玉兔镝難穿,桂枝人共折。萬象照乃無私,瓊台豈遮君谒。抱琴對彈别鶴聲,不得知音聲不切。——【唐】李紳《賦月》

環境準備

macbook 運作環境準備

mac 預設 Python 版本為 3.8.x,在運作 turtle 時,會出現黑屏閃爍的情況。為了正常運作 turtle,我們需要安裝更新的 Python 版本,這裡以 Python 3.10.6-macos 11.pkg(提取碼: yimf)為例。

安裝過程很簡單,輕按兩下安裝包

【案例】用 turtle 繪制一個月餅

一直點選繼續即可。

【案例】用 turtle 繪制一個月餅

然後設定 ​

​python​

​​ 和 ​

​pip​

​​ 兩個指令指向 ​

​python3.10.6​

​​ 和 ​

​pip3.10​

​​,這樣做的目的就是改變系統上主要使用的 Python 的版本,這樣在指令行中輸入 ​

​Python​

​ 就會顯示如下内容:

【案例】用 turtle 繪制一個月餅

在指令行中檢視已經安裝的 Pyhton 包:

【案例】用 turtle 繪制一個月餅

那麼,到底是怎麼實作的呢?在家目錄下(比如:​

​/Users/zhangjiaming​

​​),建立兩個檔案(​

​touch .zshrc​

​​、​

​touch bash_profile​

​),内容如下:

【案例】用 turtle 繪制一個月餅

turtle 庫

turtle 官網:​​https://pythonturtle.org​​

PythonTurtle緻力于提供學習(或教授)Python程式設計語言軟體開發的最低門檻方法。學生使用互動式Python shell(類似于空閑開發環境)并使用Python函數移動螢幕上顯示的海龜。一個帶插圖的幫助螢幕向學生介紹Python程式設計的基礎知識,同時示範如何移動海龜。

turtle 是一個十分友善的 Python 繪圖庫,隻需要使用簡單的過程方法,就可以繪制出許多有趣的圖形。

turtle 模拟了用筆繪圖的過程:

  • 落筆 —— turtle.pendown()
  • 起筆 —— turtle.penup()
  • 畫直線 —— turtle.forward(xxx)
  • 筆的尺寸 —— turtle.pensize(xxx)
  • 向左轉多少度 —— turtle.left(xxx)

​​讓可愛的海龜動起來!Turtle庫做出的簡單動畫,使Python無所不能​​ 裡面詳細說了坐标系的問題。

源代碼

封裝一個自己的函數

就像是畫畫的時候一樣,總有一個起筆——移動——落筆的過程,我們使用下面的函數來實作這三個步驟,否則,畫筆會顯示出畫筆移動的軌迹。

def goto(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()      

月餅主體

我們的月餅分為以下幾個部分:

  1. 最外側花邊
  2. 【案例】用 turtle 繪制一個月餅
  3. 内側花邊
  4. 【案例】用 turtle 繪制一個月餅
  5. 圓形
  6. 【案例】用 turtle 繪制一個月餅
  7. 矩形
  8. 【案例】用 turtle 繪制一個月餅
  9. 中心花瓣
  10. 【案例】用 turtle 繪制一個月餅
  11. 文字
def draw():
    
    # 最外側花邊
    turtle.pensize(20)
    goto(0, 0)
    turtle.color("#83572F") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(200)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    # 内側花邊
    goto(0, 0)
    turtle.color("#E1A43B") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(180)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    # 圓形
    goto(0, -180)
    turtle.color("#F2C54D") 
    turtle.begin_fill()
    turtle.circle(180)
    turtle.end_fill()
    
    # 矩形
    x = 125
    y = 2*x
    turtle.pensize(15)
    turtle.color("#C97E2E") 
    goto(-x, x)
    turtle.pensize(10)
    turtle.forward(y)
    goto(-x, x-y)
    turtle.forward(y)
    goto(-x, x)
    turtle.right(90)
    turtle.forward(y)
    goto(x, x)
    turtle.forward(y)
    
    # 中心花瓣
    goto(0, 0)
    turtle.pensize(10)
    turtle.color("#36210E") 
    for _ in range(30):
        turtle.left(12)
        # turtle.begin_fill()
        turtle.forward(100)
        turtle.circle(25)
        turtle.goto(0, 0)      

寫一段文字

turtle 實作了繪制文字的函數,我們可以直接調用以實作文字效果。

def wirte():
    goto(200, -270) # 從哪裡開始寫
    turtle.color("black") # 畫筆顔色
    turtle.write("祝:\n\n各位中秋節快樂,\n吉祥如意!\n\n    Jia ming", font=("Time",20,"bold")) # 書寫内容、文字樣式      

完整代碼:

import turtle

def goto(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def draw():
    
    turtle.pensize(20)
    goto(0, 0)
    turtle.color("#83572F") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(200)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    goto(0, 0)
    turtle.color("#E1A43B") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(180)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    goto(0, -180)
    turtle.color("#F2C54D") 
    turtle.begin_fill()
    turtle.circle(180)
    turtle.end_fill()
    
    x = 125
    y = 2*x
    turtle.pensize(15)
    turtle.color("#C97E2E") 
    goto(-x, x)
    turtle.pensize(10)
    turtle.forward(y)
    goto(-x, x-y)
    turtle.forward(y)
    goto(-x, x)
    turtle.right(90)
    turtle.forward(y)
    goto(x, x)
    turtle.forward(y)
        
    goto(0, 0)
    turtle.pensize(10)
    turtle.color("#36210E") 
    for _ in range(30):
        turtle.left(12)
        # turtle.begin_fill()
        turtle.forward(100)
        turtle.circle(25)
        turtle.goto(0, 0)
        # turtle.left(180)
        # turtle.end_fill()
    
    
        
def wirte():
    goto(200, -270)
    turtle.color("black")
    turtle.write("祝:\n\n各位中秋節快樂,\n吉祥如意!\n\n    Jia ming", font=("Time",20,"bold"))


if __name__ == '__main__':
    turtle.speed(9000)
    turtle.hideturtle() # 隐藏筆頭
    
    draw()
    wirte()
    turtle.done()      

最終效果

【案例】用 turtle 繪制一個月餅