Python+Turtle繪制可愛的可達(dá)鴨
一年一度的六一兒童節(jié)又來了,祝大朋友小朋友節(jié)日快樂。
你有沒有一瞬間,特別想要回到童年?
童年是一盒水彩筆,五顏六色精彩紛呈。童年是一幅漫畫,新奇幻想思緒繽紛。童年是用水彩筆繪出的一幅漫畫,一個追風(fēng)的少年。
童年的時光總是安靜又美好,我們總是盼望著長大,憧憬著未來的生活。
長大后,我們又總是在懷念,懷念過去簡單又純粹的自己。
本文主要介紹運用turtle庫控制函數(shù)繪制可達(dá)鴨,希望你會喜歡(希望不要被丑哭)。
一、效果展示

在介紹代碼之前,先來看下本文的實現(xiàn)效果。
本文繪制的可達(dá)鴨,是日本任天堂公司發(fā)行的掌機游戲系列《寶可夢》中的一種。
可達(dá)鴨雖然頭痛變劇烈時就能使出不可思議的力量,不過當(dāng)時的記憶卻不會留下來。
由于怎樣也想不起來因此總歪著脖子。
二、代碼詳解
python繪制可達(dá)鴨的原理是:應(yīng)用turtle庫首先繪制頭和身體的外輪廓,然后繪制眼睛、手、腳、頭發(fā)等不同模塊。
1.導(dǎo)入庫
首先導(dǎo)入本文需要加載的庫,如果你有些庫還沒有安裝,導(dǎo)致運行代碼時報錯,可以在Anaconda Prompt中用pip方法安裝。
# -*- coding: UTF-8 -*- ''' 代碼用途 :畫可達(dá)鴨 作者 :阿黎逸陽 博客 : https://blog.csdn.net/qq_32532663/article/details/106176609 ''' import os import pygame import turtle as t from time import sleep
本文應(yīng)用到的庫較少,只應(yīng)用了os、pygame、turtle和time四個庫。
os庫可以設(shè)置文件讀取的位置。
pygame庫是為了繪制過程更有趣,在繪圖過程中添加了背景音樂。
turtle庫是繪圖庫,相當(dāng)于給你一支畫筆,你可以在畫布上用數(shù)學(xué)邏輯控制的代碼完成繪圖。
time庫是時間管理庫,本文用來設(shè)置繪圖的暫停時間。
2.播放音樂
接著應(yīng)用pygame庫播放背景音樂,本文的音樂是《 瞬間的永恒》。
#播放音樂
print('播放音樂')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公眾號\53.可達(dá)鴨\趙海洋 - 《瞬間的永恒》夜色鋼琴曲.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。
如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的電腦本地存放地址填進(jìn)去。
3.畫可達(dá)鴨的頭和身體外輪廓
然后進(jìn)入可達(dá)鴨的正式繪制過程,先畫的是頭和身體外輪廓。
t.title('阿黎逸陽的代碼公眾號')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.penup()
t.goto(150, 50)
t.pendown()
t.color('#FFCC00')
t.pendsize(1)
t.color('yellow')
t.begin_fill()
t.setheading(30)
#畫頭
print('畫頭')
t.circle(40, 70)
t.circle(60, 160)
t.circle(40, 70)
#畫身體
print('畫身體')
t.setheading(-150)
t.circle(100, 30)
t.circle(70, 90)
t.circle(100, 60)
t.circle(70, 90)
t.circle(100, 30)
t.end_fill()關(guān)鍵代碼詳解:
t.pensize(width):設(shè)置畫筆的尺寸。
t.color(color):設(shè)置畫筆的顏色。
t.penup():抬起畫筆,一般用于另起一個地方繪圖使用。
t.goto(x,y):畫筆去到某個位置,參數(shù)為(x,y),對應(yīng)去到的橫坐標(biāo)和縱坐標(biāo)。
t.pendown():放下畫筆,一般和penup組合使用。
t.left(degree):畫筆向左轉(zhuǎn)多少度,括號里表示度數(shù)。
t.right(degree):畫筆向右轉(zhuǎn)多少度,括號里表示度數(shù)。
t.circle(radius,extent,steps):radius指半徑,若為正,半徑在小烏龜左側(cè)radius遠(yuǎn)的地方,若為負(fù),半徑在小烏龜右側(cè)radius遠(yuǎn)的地方;extent指弧度;steps指階數(shù)。
畫外輪廓的關(guān)鍵是:通過調(diào)節(jié)circle函數(shù)中的半徑和弧度來調(diào)節(jié)曲線的弧度,從而使得可達(dá)鴨的輪廓比較流暢。
4.畫眼睛
畫完頭和身體外輪廓后就可以分模塊畫其它組成部分了,本小節(jié)畫眼睛。
#畫左眼睛
print('畫左眼睛')
t.penup()
t.goto(85, 95)
t.pendown()
t.color('white')
t.begin_fill()
t.setheading(20)
t.pensize(3)
t.circle(15, 70)
t.setheading(120)
t.circle(15, 50)
t.left(10)
t.circle(20, 100)
t.left(10)
t.circle(8, 90)
t.circle(40, 30)
t.end_fill()
t.penup()
t.goto(80, 102)
t.pendown()
t.color('black')
t.begin_fill()
t.setheading(0)
t.pensize(2)
t.circle(1.5, 360)
t.end_fill()
#畫右眼睛
print('畫右眼睛')
t.penup()
t.goto(135, 95)
t.pendown()
t.color('white')
t.begin_fill()
t.setheading(160)
t.pensize(3)
t.circle(-15, 70)
t.setheading(60)
t.circle(-15, 50)
t.right(10)
t.circle(-20, 100)
t.right(10)
t.circle(-8, 90)
t.circle(-40, 30)
t.end_fill()
t.penup()
t.goto(140, 102)
t.pendown()
t.color('black')
t.begin_fill()
t.setheading(0)
t.pensize(2)
t.circle(1.5, 360)
t.end_fill()5.畫手
本小節(jié)介紹畫手代碼,為了看起來效果更好,需要注意的是手的對稱。
#畫左手
print('畫左手')
t.penup()
t.goto(60, 42)
t.pendown()
t.color('#FFCC00')
#t.color('yellow')
t.begin_fill()
t.setheading(160)
t.circle(20, 50)
t.right(30)
t.circle(90, 50)
t.forward(5)
t.circle(5, 180)
t.setheading(-120)
t.forward(5)
t.circle(6, 180)
t.right(20)
t.circle(-90, 50)
t.end_fill()
#畫右手
print('畫右手')
t.penup()
t.goto(160, 42)
t.pendown()
t.color('#FFCC00')
#t.color('yellow')
t.begin_fill()
t.setheading(-15)
t.circle(10, 80)
t.right(18)
t.circle(80, 42)
t.circle(-5, 180)
t.setheading(80)
t.forward(1)
t.circle(-6, 180)
t.setheading(80)
t.circle(-5, 180)
t.left(10)
t.circle(-90, 40)
t.circle(-80, 30)
t.end_fill()6.畫嘴和腳
本小節(jié)介紹畫嘴和腳代碼。
#畫嘴巴
print('畫嘴巴')
t.penup()
t.goto(114, 85)
t.pendown()
t.color('#FFFF99')
#t.color('#FFCC00')
#t.color('black')
t.begin_fill()
t.setheading(180)
t.forward(6)
t.setheading(-130)
t.circle(-50, 40)
t.setheading(-70)
t.circle(-50, 50)
t.circle(20, 90)
t.circle(50, 60)
t.circle(20, 90)
t.right(15)
t.circle(-50, 50)
t.left(120)
t.circle(-50, 47)
t.end_fill()
#畫左腳
print('畫左腳')
t.penup()
t.goto(55, -78)
t.pendown()
t.color('#FFFF99')
t.begin_fill()
t.setheading(-115)
t.forward(40)
t.setheading(50)
t.circle(-12, 160)
t.setheading(70)
t.circle(-10, 160)
t.setheading(85)
t.forward(39)
t.setheading(170)
t.circle(-100, 18)
t.end_fill()
#畫右腳
print('畫右腳')
t.penup()
t.goto(113, -97)
t.pendown()
t.color('#FFFF99')
t.begin_fill()
t.setheading(-100)
t.forward(35)
t.setheading(60)
t.circle(-10, 160)
t.setheading(80)
t.circle(-10, 160)
t.setheading(90)
t.forward(47)
t.setheading(197)
t.circle(-100, 18)
t.end_fill()7.畫頭發(fā)
本小節(jié)介紹畫頭發(fā)代碼。
#畫頭發(fā)
print('畫頭發(fā)')
#第一根頭發(fā)
t.penup()
t.goto(92, 138)
t.pendown()
t.color('black')
t.begin_fill()
t.pensize(1)
t.setheading(115)
t.circle(-100, 15)
t.setheading(20)
t.circle(80, 6)
t.setheading(-76)
t.circle(100, 16)
t.setheading(180)
t.circle(30, 20)
t.end_fill()
#第二根頭發(fā)
t.penup()
t.goto(106, 140)
t.pendown()
t.color('black')
t.begin_fill()
t.pensize(1)
t.setheading(105)
t.circle(-100, 18)
t.setheading(10)
t.circle(80, 7)
t.setheading(-92)
t.circle(100, 20)
t.setheading(160)
t.circle(30, 16)
t.end_fill()
#第三根頭發(fā)
t.penup()
t.goto(123, 138)
t.pendown()
t.color('black')
t.begin_fill()
t.pensize(1)
t.setheading(75)
t.circle(100, 17)
t.setheading(-5)
t.circle(80, 7)
t.setheading(-100)
t.circle(120, 15)
t.setheading(160)
t.circle(30, 20)
t.end_fill()8.寫文字
最后介紹寫會動文字的代碼。
#寫文字
print('寫文字')
def write_1(x, y, ss):
t.hideturtle()
t.penup()
t.goto(x, y)
t.pendown()
t.pencolor('black')
#t.write('猜謎語', font=('Times New Roman', 12, 'normal'))
t.write(ss, font=('Times New Roman', 20, 'normal'))
def write_all():
write_1(-130, 120, '節(jié)')
write_1(-130, 80, '日')
write_1(-130, 40, '快')
write_1(-130, 0, '樂')
write_1(-130, -40, '!')
def undos():
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
while 1:
write_all()
sleep(1)
undos()以上就是Python+Turtle繪制可愛的可達(dá)鴨的詳細(xì)內(nèi)容,更多關(guān)于Python Turtle繪制可達(dá)鴨的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實現(xiàn)requests發(fā)送/上傳多個文件的示例
今天小編就為大家分享一篇python實現(xiàn)requests發(fā)送/上傳多個文件的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Pycharm打開.py文件和項目的幾種實現(xiàn)方式
這篇文章主要介紹了Pycharm打開.py文件和項目的幾種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
利用python實現(xiàn)flappy bird 游戲(完整代碼)
python 中 pygame模塊能讓我們很方便的編寫游戲,16年我用python 仿制了flappy bird 游戲,下面是游戲的完整代碼以及素材,分享給大家2021-11-11
Python中出現(xiàn)IndentationError:unindent does not match any outer
今天在網(wǎng)上copy的一段代碼,代碼很簡單,每行看起來該縮進(jìn)的都縮進(jìn)了,運行的時候出現(xiàn)了如下錯誤,IndentationError: unindent does not match any outer indentation level,如果看起來縮進(jìn)正常所有tab與空格混用就會出現(xiàn)這個問題2019-01-01
windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解
這篇文章主要介紹了windows下python 3.9 Numpy scipy和matlabplot的安裝教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

