<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 畫個(gè)哆啦A夢(mèng)

          共 1156字,需瀏覽 3分鐘

           ·

          2020-08-08 02:28



          文 |?豆豆

          來源:Python 技術(shù)「ID: pythonall」

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

          先來看看我們最終實(shí)現(xiàn)的繪制過程。

          頭部輪廓和圍巾

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

          import turtle as t
          t.speed(5)t.pensize(1)t.screensize(500, 500)t.bgcolor('white')

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

          #?頭部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()

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

          不急不急,我們?cè)佼嬕幌履槻考?xì)節(jié)。

          臉部

          眼睛

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

          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()

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

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

          簡(jiǎn)單起見,直接補(bǔ)兩個(gè)黑色的圓圈圈即可。

          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)

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

          鼻子 & 嘴巴

          鼻子也不難,在眼睛下面畫個(gè)小圈圈就行,嘴巴就類似一個(gè)倒 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)

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

          胡須 & 鈴鐺

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

          # 胡須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)

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

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

          最終效果如下:

          是不是還挺簡(jiǎn)單的?興趣的朋友趕快自己動(dòng)手試試吧,還可以嘗試把身體給加上去。

          總結(jié)

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

          其實(shí) turtle 使用不難,主要是要理清烏龜?shù)奈恢靡约俺?,然后就是其運(yùn)動(dòng)模式,直線運(yùn)動(dòng),還是曲線運(yùn)動(dòng)以及怎調(diào)換運(yùn)動(dòng)方向。其中烏龜?shù)淖鴺?biāo)計(jì)算比較麻煩,尤其是做曲線運(yùn)動(dòng)的時(shí)候,大家可以結(jié)合 pos() 函數(shù)來獲取查看烏龜?shù)淖鴺?biāo),有助于你理清畫圖思路。

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

          完整代碼:

          https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-03-27-duo-la-a-meng


          _往期文章推薦_

          【編程課堂】海龜作圖




          瀏覽 108
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  久七黄色视频可看 | 手机A V在线 | 青青草视频手机华人 | a级黄片免费在线观看 | 天天操人人色 |