<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用 Python 畫哆啦 A 夢

          共 1167字,需瀏覽 3分鐘

           ·

          2021-02-09 03:06


          導讀:相信大家童年的時候都看過哆啦 A 夢,他的口袋簡直是無所不能,里面裝滿了各種神奇的道具。曾經的我也幻想過如果自己也有一個這樣的口袋多好。今天我們就用 Python 來畫一個哆啦 A 夢,懷念下我們的童年。


          作者:豆豆
          來源:Python 技術(ID: pythonall)


          先來看看我們最終實現(xiàn)的效果圖。



          01 頭部輪廓和圍巾


          老規(guī)矩,先引入今天要用到的庫 turtle 并做一些基礎設置。

          import?turtle?as?t

          t.speed(5)
          t.pensize(1)
          t.screensize(500,?500)
          t.bgcolor('white')

          首先,我們先畫下多啦 A 夢頭部外輪廓,頭部輪廓主要是一個多半圓,圍巾就是一個小的長方形。

          #?頭部
          def?head():
          ????t.up()
          ????t.circle(150,?45)
          ????t.down()
          ????t.fillcolor(head_color)
          ????t.begin_fill()
          ????t.circle(150,?270)
          ????t.end_fill()


          #?圍巾
          def?scarf():
          ????t.fillcolor(scarf_color)
          ????t.begin_fill()
          ????t.seth(0)
          ????t.fd(216)
          ????t.circle(-5,?90)
          ????t.fd(10)
          ????t.circle(-5,?90)
          ????t.fd(220)
          ????t.circle(-5,?90)
          ????t.fd(10)
          ????t.circle(-5,?90)
          ????t.end_fill()

          來看下效果如何,呃呃呃,怎么感覺有點像大馬路上的石墩兒呢,除了顏色有點像之外。


          不急不急,我們再畫一下臉部細節(jié)。


          02 臉部


          1. 眼睛


          俗話說眼睛是心靈的窗戶,我們先把眼睛畫上。

          def?face():
          ????t.fd(186)
          ????t.lt(45)
          ????t.fillcolor(color_white)
          ????t.begin_fill()
          ????t.circle(120,?100)
          ????t.seth(180)
          ????t.fd(120)
          ????t.seth(215)
          ????t.circle(120,?100)
          ????t.end_fill()

          def?draw_eyes():
          ????t.fillcolor(color_white)
          ????t.begin_fill()
          ????a?=?2.5
          ????for?i?in?range(120):
          ????????if?0?<=?i?30?or?60?<=?i?90:
          ????????????a?-=?0.05
          ????????else:
          ????????????a?+=?0.05
          ????????t.lt(3)
          ????????t.fd(a)
          ????t.end_fill()

          def?eyes():
          ????go_to(0,?227)
          ????t.seth(90)
          ????draw_eyes()
          ????go_to(0,?227)
          ????t.seth(270)
          ????draw_eyes()

          畫眼睛不能直接畫一個正圓,那樣看起來會比較奇怪,要畫一個橢圓才行,來看下效果。


          眼睛畫上去之后明顯感覺精神多了,也有點多啦 A 夢的模樣了??墒强雌饋磉€是有點不太對,是的,眼睛還沒有畫瞳孔。

          簡單起見,直接補兩個黑色的圓圈圈即可。

          def?fill_eyes():
          ????#?填充眼睛
          ????go_to(-15,?220)
          ????t.pensize(12)
          ????t.color('black')
          ????for?i?in?range(30):
          ????????t.forward(2)
          ????????t.right(12)
          ????go_to(15,?220)
          ????for?i?in?range(30):
          ????????t.forward(2)
          ????????t.left(12)
          ????t.pensize(1)


          不錯不錯,越來越有樣了。下面我們把鼻子和嘴巴加上。

          2. 鼻子 & 嘴巴


          鼻子也不難,在眼睛下面畫個小圈圈就行,嘴巴就類似一個倒 T 字。

          #?鼻子
          def?nose():
          ????go_to(-13,?166)
          ????t.seth(315)
          ????t.fillcolor(nose_color)
          ????t.begin_fill()
          ????t.circle(20)
          ????t.end_fill()


          #?嘴巴
          def?mouth():
          ????go_to(0,?156)
          ????t.seth(270)
          ????t.fd(100)
          ????pos?=?t.pos()
          ????t.seth(0)
          ????t.circle(110,?60)
          ????go_to(pos[0],?pos[1])
          ????t.seth(180)
          ????t.circle(-110,?60)


          離成功就剩下最后一步了,把胡須和鈴鐺畫上就大功告成了。

          3. 胡須 & 鈴鐺


          胡須就類似畫貓的胡須就好,鈴鐺表復雜些,大圓套小圓,還有裝飾。

          #?胡須
          def?mustache():
          ????h?=?70
          ????go_to(30,?140)
          ????t.seth(15)
          ????t.fd(h)

          ????go_to(30,?130)
          ????t.seth(0)
          ????t.fd(h)

          ????go_to(30,?120)
          ????t.seth(-15)
          ????t.fd(h)

          ????go_to(-30,?140)
          ????t.seth(150)
          ????t.fd(h)

          ????go_to(-30,?130)
          ????t.seth(180)
          ????t.fd(h)

          ????go_to(-30,?120)
          ????t.seth(195)
          ????t.fd(h)

          #?鈴鐺
          def?bell():
          ????#?大圓
          ????go_to(0,?33)
          ????t.pensize(1)
          ????t.fillcolor("#FCE341")
          ????t.begin_fill()
          ????t.circle(25)
          ????t.end_fill()

          ????#?橫條紋
          ????go_to(-15,?22)
          ????t.seth(0)
          ????t.forward(42)
          ????go_to(-18,?17)
          ????t.seth(0)
          ????t.forward(47)

          ????#?小圓
          ????go_to(5,?0)
          ????t.pensize(1)
          ????t.color("black",?'#79675D')
          ????t.begin_fill()
          ????t.circle(5)
          ????t.end_fill()
          ????t.seth(270)
          ????t.pensize(1)
          ????t.forward(15)

          最后我們寫一個入口函數(shù),將這些畫身體不同部位的函數(shù)給封裝起來。代碼如下:

          if?__name__?==?'__main__':
          ????head()
          ????scarf()
          ????face()
          ????eyes()
          ????fill_eyes()
          ????nose()
          ????mouth()
          ????mustache()
          ????bell()
          ????go_to()
          ????t.hideturtle()
          ????t.done()

          最終效果如下:


          是不是覺得很簡單,快去后臺獲取源碼 run 起來吧。有興趣的讀者還可以把身體給加上去。


          03 總結


          本文我們使用 Python 的 turtle 庫畫了一下哆啦 A 夢,不知道和大家記憶中的哆啦 A 夢是否一樣呢。

          其實 turtle 使用不難,主要是要理清烏龜?shù)奈恢靡约俺?,然后就是其運動模式,直線運動,還是曲線運動以及怎調換運動方向。其中烏龜?shù)淖鴺擞嬎惚容^麻煩,尤其是做曲線運動的時候,大家可以結合 pos() 函數(shù)來獲取查看烏龜?shù)淖鴺?,有助于你理清畫圖思路。

          大家多加練習,肯定都可以畫出自己想畫的,有趣且好玩的圖像。


          延伸閱讀??

          利用Python進行數(shù)據(jù)分析(原書第2版)

          推薦語:Python數(shù)據(jù)分析經典暢銷書全新升級,第1版中文版累計銷售100000冊。針對Python 3.6進行全面修訂和更新,涵蓋新版的pandas、NumPy、IPython和Jupyter。


          干貨直達??



          更多精彩??

          在公眾號對話框輸入以下關鍵詞
          查看更多優(yōu)質內容!

          PPT?|?讀書?|?書單?|?硬核?|?干貨?|?講明白?|?神操作
          大數(shù)據(jù)?|?云計算?|?數(shù)據(jù)庫?|?Python?|?可視化
          AI?|?人工智能?|?機器學習?|?深度學習?|?NLP
          5G?|?中臺?|?用戶畫像?|?1024?|?數(shù)學?|?算法?|?數(shù)字孿生

          據(jù)統(tǒng)計,99%的大咖都完成了這個神操作
          ??


          瀏覽 22
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  盗摄—AV国产盗摄 | 激情青青QV| 免费在线观看亚洲天堂 | 美女被大鸡吧操视频网站在线播放 | 久久人人爽人人爽人人av东京热 |