溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

通過Turtle庫在Python中繪制一個鼠年福鼠

發(fā)布時間:2020-08-28 16:32:06 來源:腳本之家 閱讀:168 作者:州的先生 欄目:開發(fā)技術

turtle庫是一個很經(jīng)典的繪圖庫,其最初來自于1967年創(chuàng)造的logo編程語言,之后被Python編寫放到了Python的內(nèi)置模塊中。網(wǎng)絡上有很多借助于turtle繪制精美圖像的案例。比如小豬佩奇、皮卡丘、柯基犬等等。趁著新年假期還未結束,今天州的先生(https://zmister.com)為大家?guī)硪粋€福鼠的繪制。

一、繪制鼠頭

首先,咱們(https://zmister.com)把鼠的頭給繪制了。鼠頭主要是由圓來構成,臉龐是一個大圓,耳朵、眉毛、眼睛、嘴角和鼻子也都是由不同弧度的圓構成。鼠頭的繪制代碼如下所示:

def head():
 turtle.color('black')

 # 臉輪廓
 turtle.pd() # 落筆
 turtle.circle(50) # 畫一個半徑為50的圓
 turtle.pu() # 提筆

 # 右耳輪廓
 turtle.goto(50,60) # 移動到x=50,y=60的位置
 turtle.pd() # 落筆
 turtle.circle(30,260) # 畫一個半徑為30,角度為245的圓弧
 turtle.pu() # 提筆
 # 右耳耳紋
 turtle.goto(30,90)
 turtle.pd()
 turtle.seth(65)
 turtle.circle(-30,70)
 turtle.pu()

 # 左耳輪廓
 turtle.goto(-50,60)
 turtle.pd()
 turtle.seth(180) # 設置方向為西,
 turtle.circle(-30,260)
 turtle.pu()
 # 左耳耳紋
 turtle.goto(-30,90)
 turtle.pd()
 turtle.seth(120)
 turtle.circle(30,70)
 turtle.pu()

 # 面部五官
 # 右側眉毛
 turtle.goto(5,80)
 turtle.seth(20)
 turtle.pd()
 turtle.circle(-25,40)
 turtle.pu()
 # 左側眉毛
 turtle.goto(-5,80)
 turtle.seth(160)
 turtle.pd()
 turtle.circle(25,40)
 turtle.pu()

 # 右側眼睛
 turtle.begin_poly()
 turtle.goto(8,60)
 turtle.seth(45)
 turtle.pd()
 turtle.circle(-15,120)
 turtle.pu()
 turtle.goto(8,60)
 turtle.seth(40)
 turtle.pd()
 turtle.circle(-15,100)
 turtle.pu()
 turtle.end_poly()

 # 左側眼睛
 turtle.goto(-8,60)
 turtle.seth(135)
 turtle.pd()
 turtle.circle(15,120)
 turtle.pu()
 turtle.goto(-8,60)
 turtle.seth(140)
 turtle.pd()
 turtle.circle(15,100)
 turtle.pu()

 # 鼻子
 # 鼻子上瓣
 turtle.goto(-6,45)
 turtle.seth(70)
 turtle.pd()
 turtle.circle(-6,150)
 turtle.pu()
 # 鼻子下瓣
 turtle.goto(-6,45)
 turtle.seth(-70)
 turtle.pd()
 turtle.circle(6,150)
 turtle.pu()

 # 鼻線
 turtle.goto(0,40)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(7)
 turtle.pu()

 # 上嘴線
 turtle.seth(200)
 turtle.pd()
 turtle.circle(-15,60)
 turtle.pu()

 turtle.goto(0,33)
 turtle.seth(-20)
 turtle.pd()
 turtle.circle(15,60)
 turtle.pu()

 # 下嘴線
 turtle.goto(10,33)
 turtle.seth(260)
 turtle.pd()
 turtle.circle(-15,65)
 turtle.pu()

 turtle.goto(-10,33)
 turtle.seth(280)
 turtle.pd()
 turtle.circle(15,65)
 turtle.pu()

 # 牙齒
 turtle.goto(4,33)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(4)
 turtle.seth(180)
 turtle.forward(8)
 turtle.seth(90)
 turtle.forward(4)
 turtle.pu()

 # 胡須
 turtle.pensize(2)
 turtle.goto(30,30)
 turtle.seth(8)
 turtle.pd()
 turtle.circle(-60,40)
 turtle.pu()

 turtle.goto(30,25)
 turtle.seth(-5)
 turtle.pd()
 turtle.circle(-60,40)
 turtle.pu()


 turtle.goto(-30,30)
 turtle.seth(172)
 turtle.pd()
 turtle.circle(60,40)
 turtle.pu()

 turtle.goto(-30,25)
 turtle.seth(188)
 turtle.pd()
 turtle.circle(60,40)
 turtle.pu()

 # 睫毛
 turtle.pensize(1)
 turtle.goto(30,58)
 turtle.seth(20)
 turtle.pd()
 turtle.circle(20,20)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(28,62)
 turtle.seth(25)
 turtle.pd()
 turtle.circle(20,12)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(-30,58)
 turtle.seth(160)
 turtle.pd()
 turtle.circle(-20,20)
 turtle.pu()

 turtle.pensize(1)
 turtle.goto(-28,62)
 turtle.seth(165)
 turtle.pd()
 turtle.circle(-20,12)
 turtle.pu()

運行上述代碼,我們可以看到鼠頭可以完整地繪制出來了,如下動圖所示:

通過Turtle庫在Python中繪制一個鼠年福鼠

二、繪制身體

接著,咱們來繪制老鼠的身體。我們(https://zmister.com)畫的這個老鼠是一個穿著財神服站立拱手的老鼠,所以它的身體需要重點突出的是服裝:

def body():
 # 左手
 turtle.goto(-25,8)
 turtle.seth(240)
 turtle.pd()
 turtle.circle(150,15)
 turtle.seth(270)
 turtle.circle(40,15)
 turtle.circle(15,65)
 turtle.seth(0)
 turtle.forward(10)
 turtle.circle(10,100)
 turtle.seth(90)
 turtle.forward(5)
 turtle.circle(10,100)
 turtle.seth(180)
 turtle.forward(10)
 turtle.pu()
 # 右手
 turtle.goto(25,8)
 turtle.seth(-60)
 turtle.pd()
 turtle.circle(-150,15)
 turtle.seth(270)
 turtle.circle(-40,15)
 turtle.circle(-15,65)
 turtle.seth(180)
 turtle.forward(10)
 turtle.circle(-10,100)
 turtle.seth(90)
 turtle.forward(5)
 turtle.circle(-10,100)
 turtle.seth(0)
 turtle.forward(10)
 turtle.pu()

 # 袍子
 turtle.goto(-30,-48)
 turtle.seth(270)
 turtle.pd()
 turtle.forward(30)
 turtle.circle(10,100)
 turtle.seth(0)
 turtle.forward(38)
 turtle.circle(10,100)
 turtle.seth(90)
 turtle.forward(30)
 turtle.pu()

 # 領口
 turtle.goto(-20,4)
 turtle.pd()
 turtle.seth(300)
 turtle.circle(30,20)
 turtle.seth(0)
 turtle.forward(25)
 turtle.seth(30)
 turtle.circle(30,20)
 turtle.pu()

 # 官帶
 turtle.goto(-7,-38)
 turtle.seth(0)
 turtle.pd()
 turtle.forward(15)
 turtle.pu()
 turtle.goto(-30,-54)
 turtle.pd()
 turtle.forward(60)
 turtle.pu()

 # 袍子上的波浪
 turtle.goto(-30,-80)
 turtle.pd()
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.pu()

 turtle.goto(-25,-85)
 turtle.pd()
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.seth(90)
 turtle.circle(-5,180)
 turtle.pu()

運行上述代碼,我們可以看到老鼠身體的繪制過程,如下動圖所示:

通過Turtle庫在Python中繪制一個鼠年福鼠

這里先不將其匯合在一起,待幾個部件都完成之后,我們再將其組成一個完整地老鼠。

三、繪制手

上面繪制的身體還缺了兩只手,對了,還有袍子上的一個大金錢,我們將其補上:

def hands():
 turtle.goto(-8, -25)
 turtle.pd()
 turtle.seth(30)
 turtle.forward(10)
 turtle.seth(0)
 turtle.circle(-10, 50)
 turtle.seth(210)
 turtle.forward(18)

 turtle.back(10)
 turtle.seth(-45)
 turtle.forward(10)
 turtle.back(10)
 turtle.seth(30)
 turtle.forward(8)
 turtle.seth(300)
 turtle.forward(5)
 turtle.pu()

 turtle.goto(0, -75)
 turtle.pd()
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10, 90)
 turtle.seth(0)
 turtle.circle(10, 90)
 turtle.seth(270)
 turtle.circle(10, 90)
 turtle.seth(180)
 turtle.circle(10, 90)
 turtle.pu()

拱手和金錢沒有和身體結合在一起時,暫時看不出上面效果來,如下動圖所示:

通過Turtle庫在Python中繪制一個鼠年福鼠

四、繪制帽子

老鼠還戴了一頂金錢帽,咱們(zmister.com)現(xiàn)在給它加上:

def hat():
 # 畫帽子
 turtle.goto(-20,98)
 turtle.pd()
 turtle.seth(80)
 turtle.forward(20)
 turtle.seth(60)
 turtle.circle(-20,140)
 turtle.seth(-85)
 turtle.forward(18)
 turtle.pu()

 turtle.goto(-20,98)
 turtle.pd()
 turtle.seth(80)
 turtle.forward(5)
 turtle.seth(30)
 turtle.forward(22)
 turtle.seth(-25)
 turtle.forward(24)
 turtle.pu()

 turtle.goto(0,127)
 turtle.pd()
 turtle.seth(0)
 turtle.circle(5)
 turtle.pu()

 turtle.goto(0,125)
 turtle.pd()
 turtle.seth(270)
 turtle.forward(10)
 turtle.pu()

 # 右邊抖帶
 turtle.goto(19,110)
 turtle.pd()
 turtle.seth(30)
 turtle.circle(40,50)
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10,90)
 turtle.seth(0)
 turtle.circle(10,90)
 turtle.seth(270)
 turtle.circle(10,90)
 turtle.seth(180)
 turtle.circle(10,90)
 turtle.pu()

 # 左邊抖帶
 turtle.goto(-19,110)
 turtle.pd()
 turtle.seth(150)
 turtle.circle(-40,50)
 turtle.seth(0)
 turtle.circle(10)
 turtle.seth(90)
 turtle.circle(10,90)
 turtle.seth(0)
 turtle.circle(10,90)
 turtle.seth(270)
 turtle.circle(10,90)
 turtle.seth(180)
 turtle.circle(10,90)
 turtle.pu()

帽子主要都是由圓構成,其繪制過程如下動圖所示:

通過Turtle庫在Python中繪制一個鼠年福鼠

五、繪制尾巴

先不著急為老鼠帶上金錢帽,我們還忘記了老鼠有一根長長的尾巴,為它補上吧:

def tail():
 turtle.goto(30, -60)
 turtle.pd()
 turtle.seth(20)
 turtle.circle(40, 80)
 turtle.circle(-20, 180)
 turtle.circle(-10, 90)

尾巴就是兩個方向相反弧度不同的圓,效果我們就不演示了。最后將其結合在一起:

if __name__ == '__main__':
 head()
 body()
 hands()
 hat()
 tail()
 turtle.done()

我們就可以看到一個完整的鼠年福鼠繪制過程,如下動圖所示:

通過Turtle庫在Python中繪制一個鼠年福鼠

總結

以上所述是小編給大家介紹的通過Turtle庫在Python中繪制一個鼠年福鼠,希望對大家有幫助!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI