剛剛出爐的冬奧會吉祥物:冰墩墩,附源碼...


在抖音上面看到了有人畫的冬奧會的冰墩墩,自己也想做一個。當然,圖案的繪制還是得使用我們熟悉的turtle框架。原因很簡單,它是一種基於canvas畫布的UI框架。

文末附完整源代碼,可直接運行。

閱讀全文

file

首先,將這個turtle庫安裝好。

pip install turtle

將turtle導入我們的模塊使用即可。

import turtle as tle

設置畫筆的全局屬性,先設置畫筆的基本速度和UI界面的標題吧

tle.speed(50)  # 速度設置為100
tle.title('冬奧會:冰墩墩! 公眾號:[Python 集中營]')  # 設置好UI界面的標題
tle.bgcolor('white')  # 將背景顏色設置為白色,有冬季的感覺...
tle.pencolor("deep sky blue")
tle.fillcolor("deep sky blue")

設置好畫筆的全局屬性以后,接下來就是圖形繪制的部分。思路就是拿一只畫筆在畫布上面畫圖就好了。

在開始繪制之前,先來說明一下幾個主要函數的使用方法。代碼量比較多,但是用到的函數基本都是下面這幾個。

turtle.goto(x,y)  將畫筆移動到坐標為x,y的位置
turtle.penup()  提起筆移動,不繪制圖形,用於另起一個地方繪制
turtle.circle()  畫圓,半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
setheading(angle)  設置當前朝向為angle角度
turtle.pendown()  移動時繪制圖形,缺省時也為繪制
turtle.begin_fill()  准備開始填充圖形
turtle.end_fill()  填充完成
turtle.left(degree) 逆時針移動degree°
turtle.forward(distance) 向當前畫筆方向移動distance像素長度

畫出冰墩墩的兩個耳朵,注意在畫布上把握好坐標,盡量計划將冰墩墩放在畫布的正中間。

# 冰墩墩左耳朵
tle.penup()
tle.goto(-120, 200)
tle.setheading(160)
tle.begin_fill()
tle.pendown()
tle.circle(-30, 230)
tle.setheading(180)
tle.circle(37, 90)
tle.end_fill()
# 冰墩墩右耳朵
tle.penup()
tle.goto(90, 200)
tle.setheading(20)
tle.begin_fill()
tle.pendown()
tle.circle(30, 230)
tle.setheading(0)
tle.circle(-37, 90)
tle.end_fill()

繪制冰墩墩的頭部,頭部主要是通過弧線構成的。

# 冰墩墩頭部
tle.pensize(5)
tle.penup()
tle.goto(-83, 237)
tle.setheading(30)
tle.pendown()
tle.circle(-134, 60)

tle.penup()
tle.goto(-120, 200)
tle.setheading(-120)
tle.pendown()
tle.circle(200, 80)

tle.penup()
tle.goto(90, 200)
tle.setheading(-60)
tle.pendown()
tle.circle(-200, 80)

tle.penup()
tle.setheading(210)
tle.pendown()
tle.circle(-120, 60)

繪制冰墩墩的雙眼情況,雙眼主要由眼圈、眼眶、眼珠構成的。

# 冰墩墩左眼
tle.penup()
tle.goto(-110, 100)
tle.setheading(-45)
tle.begin_fill()
tle.pendown()
agle = 0.2
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.1
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.1
        tle.left(3)
        tle.forward(agle)
tle.end_fill()

tle.fillcolor("white")
tle.penup()
tle.goto(-73, 125)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(14, 360)
tle.end_fill()

tle.penup()
tle.goto(-72, 133)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(6, 360)
tle.end_fill()

# 冰墩墩右眼
tle.penup()
tle.goto(80, 100)
tle.setheading(45)
tle.begin_fill()
tle.fillcolor("deep sky blue")
tle.pendown()
agle = 0.2
for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        agle = agle + 0.1
        tle.left(3)
        tle.forward(agle)
    else:
        agle = agle - 0.1
        tle.left(3)
        tle.forward(agle)
tle.end_fill()

tle.fillcolor("white")
tle.penup()
tle.goto(43, 125)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(14, 360)
tle.end_fill()

tle.penup()
tle.goto(42, 133)
tle.setheading(0)
tle.begin_fill()
tle.pendown()
tle.circle(6, 360)
tle.end_fill()

公眾號內回復"冰墩墩"獲取完整源代碼。

file

我是 [Python 集中營]、很高興您看到了最后, 我是一個專注於 Python 知識分享的公眾號,希望可以得到您的關注~

【往期精彩】

最優美的表格查看插件:tabulate

抖音同款課堂點名系統,PyQt5寫起來很簡單...

開工啦!批量向PDF文件添加中文水印...

大年初二、做了一個windows通知管理器!

百度圖片下載器2.0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM