Python+Turtle繪制可愛的可達鴨_第1頁
Python+Turtle繪制可愛的可達鴨_第2頁
Python+Turtle繪制可愛的可達鴨_第3頁
Python+Turtle繪制可愛的可達鴨_第4頁
Python+Turtle繪制可愛的可達鴨_第5頁
已閱讀5頁,還剩10頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第Python+Turtle繪制可愛的可達鴨目錄一、效果展示二、代碼詳解1.導(dǎo)入庫2.播放音樂3.畫可達鴨的頭和身體外輪廓4.畫眼睛5.畫手6.畫嘴和腳7.畫頭發(fā)8.寫文字一年一度的六一兒童節(jié)又來了,祝大朋友小朋友節(jié)日快樂。

你有沒有一瞬間,特別想要回到童年?

童年是一盒水彩筆,五顏六色精彩紛呈。童年是一幅漫畫,新奇幻想思緒繽紛。童年是用水彩筆繪出的一幅漫畫,一個追風的少年。

童年的時光總是安靜又美好,我們總是盼望著長大,憧憬著未來的生活。

長大后,我們又總是在懷念,懷念過去簡單又純粹的自己。

本文主要介紹運用turtle庫控制函數(shù)繪制可達鴨,希望你會喜歡(希望不要被丑哭)。

一、效果展示

在介紹代碼之前,先來看下本文的實現(xiàn)效果。

本文繪制的可達鴨,是日本任天堂公司發(fā)行的掌機游戲系列《寶可夢》中的一種。

可達鴨雖然頭痛變劇烈時就能使出不可思議的力量,不過當時的記憶卻不會留下來。

由于怎樣也想不起來因此總歪著脖子。

二、代碼詳解

python繪制可達鴨的原理是:應(yīng)用turtle庫首先繪制頭和身體的外輪廓,然后繪制眼睛、手、腳、頭發(fā)等不同模塊。

1.導(dǎo)入庫

首先導(dǎo)入本文需要加載的庫,如果你有些庫還沒有安裝,導(dǎo)致運行代碼時報錯,可以在AnacondaPrompt中用pip方法安裝。

#-*-coding:UTF-8-*-

代碼用途:畫可達鴨

作者:阿黎逸陽

博客:/qq_32532663/article/details/106176609

importos

importpygame

importturtleast

fromtimeimportsleep

本文應(yīng)用到的庫較少,只應(yīng)用了os、pygame、turtle和time四個庫。

os庫可以設(shè)置文件讀取的位置。

pygame庫是為了繪制過程更有趣,在繪圖過程中添加了背景音樂。

turtle庫是繪圖庫,相當于給你一支畫筆,你可以在畫布上用數(shù)學(xué)邏輯控制的代碼完成繪圖。

time庫是時間管理庫,本文用來設(shè)置繪圖的暫停時間。

2.播放音樂

接著應(yīng)用pygame庫播放背景音樂,本文的音樂是《瞬間的永恒》。

#播放音樂

print('播放音樂')

pygame.mixer.init()

pygame.mixer.music.load(r"F:\公眾號\53.可達鴨\趙海洋-《瞬間的永恒》夜色鋼琴曲.mp3")

pygame.mixer.music.set_volume(0.5)

pygame.mixer.music.play(1,10)

這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。

如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的電腦本地存放地址填進去。

3.畫可達鴨的頭和身體外輪廓

然后進入可達鴨的正式繪制過程,先畫的是頭和身體外輪廓。

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)去到的橫坐標和縱坐標。

t.pendown():放下畫筆,一般和penup組合使用。

t.left(degree):畫筆向左轉(zhuǎn)多少度,括號里表示度數(shù)。

t.right(degree):畫筆向右轉(zhuǎn)多少度,括號里表示度數(shù)。

t.circle(radius,extent,steps):radius指半徑,若為正,半徑在小烏龜左側(cè)radius遠的地方,若為負,半徑在小烏龜右側(cè)radius遠的地方;extent指弧度;steps指階數(shù)。

畫外輪廓的關(guān)鍵是:通過調(diào)節(jié)circle函數(shù)中的半徑和弧度來調(diào)節(jié)曲線的弧度,從而使得可達鴨的輪廓比較流暢。

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('寫文字')

defwrite_1(x,y,ss):

t.hideturtle()

t.penup()

t.goto(x,y)

t.pendown()

t.pencolor('black')

#t.write('猜謎語',font=('TimesNewRoman',12,'normal'))

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論