基于Python繪制520表白代碼_第1頁
基于Python繪制520表白代碼_第2頁
基于Python繪制520表白代碼_第3頁
基于Python繪制520表白代碼_第4頁
基于Python繪制520表白代碼_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

第基于Python繪制520表白代碼目錄一、繪制成品二、繪制代碼1.導(dǎo)入庫2.選擇背景音樂3.繪制心的外輪廓4.填充心并寫告白信5.畫心動線

一、繪制成品

二、繪制代碼

實現(xiàn)本文效果的整體思路是:加載庫選擇背景音樂繪制心的外輪廓填充心并寫告白信繪制心動線。

1.導(dǎo)入庫

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

代碼用途:情人節(jié)表白

作者:阿黎逸陽

博客:/qq_32532663/article/details/106176609

importos

importpygame

importturtleast

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

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

pygame庫是為了繪制過程更有趣,在繪圖過程中添加了背景音樂,如果無需背景音樂,不用加載該庫。

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

2.選擇背景音樂

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

#播放音樂

print('播放音樂')

pygame.mixer.init()

pygame.mixer.music.load(r"F:\公眾號\520\趙海洋-《瞬間的永恒》夜色鋼琴曲.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.hideturtle()

print('畫愛心')

defheart(x,y):

t.penup()

t.goto(x,y)

t.pendown()

t.color('pink')

t.setheading(50)

t.circle(-5,180)

t.circle(-45,12)

t.setheading(130)

t.circle(-45,12)

t.circle(-5,180)

heart(-30,155)

heart(-220,145)

heart(-210,60)

heart(-100,100)

heart(-20,20)

heart(-70,130)

heart(-140,-20)

heart(30,100)

heart(-60,-20)

heart(10,60)

heart(-100,-70)

heart(20,145)

heart(-140,-20)

heart(-130,130)

heart(-180,20)

heart(-170,155)

heart(-230,100)

關(guān)鍵代碼詳解:

t.penup():抬起畫筆,一般用于另起一個地方繪圖使用。

t.goto(x,y):畫筆去到某個位置,參數(shù)為(x,y),對應(yīng)去到的橫坐標(biāo)和縱坐標(biāo)。

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

t.color(color):設(shè)置畫筆的顏色。

t.setheading():設(shè)置海龜頭與橫坐標(biāo)偏離的度數(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.填充心并寫告白信

接下來邊填充心,邊寫告白信,代碼如下:

defwrite_mes(x,y,size,ss):

t.hideturtle()

t.penup()

t.goto(x,y)

t.pendown()

t.pencolor('black')

t.write(ss,font=('TimesNewRoman',size,'normal'))

print('畫紅心')

defheart_fill(x,y):

t.penup()

t.goto(x,y)

t.pendown()

t.color('red','red')

t.begin_fill()

t.setheading(50)

t.circle(-5,180)

t.circle(-45,12)

t.setheading(130)

t.circle(-45,12)

t.circle(-5,180)

t.end_fill()

x=90

y=110

#右邊愛心

write_mes(x,y,11,'喜歡你的每一天')

heart_fill(-100,100)

heart_fill(-70,130)

heart_fill(-30,155)

heart_fill(20,145)

heart_fill(30,100)

write_mes(x,y-30,11,'愛意不曾退減')

heart_fill(10,60)

heart_fill(-20,20)

heart_fill(-60,-20)

heart_fill(-100,-70)

#左邊愛心

write_mes(x,y-30*2,11,'時光不曾走遠')

heart_fill(-140,-20)

heart_fill(-180,20)

heart_fill(-210,60)

heart_fill(-230,100)

write_mes(x,y-30*3,11,'幸福延續(xù)到明天')

heart_fill(-220,145)

heart_fill(-170,155)

heart_fill(-130,130)

write_mes(x,y-30*4,11,'永遠不說再見')

5.畫心動線

最后是寫姓名并畫心動線,代碼如下:

t.speed(15)

print('畫心動線')

defheart_bit():

#畫心動線

t.penup()

t.goto(-170,40)

t.pendown()

t.pencolor('red')

t.setheading(0)

t.pensize(2)

t.forward(10)

#第一個小波浪

t.setheading(45)

t.circle(50,10)

t.setheading(0)

t.circle(-3,90)

t.circle(50,5)

t.setheading(0)

t.forward(10)

#第一個下尖峰

t.setheading(-80)

t.forward(7)

t.setheading(70)

t.forward(25)

t.setheading(-85)

t.forward(29)

t.setheading(70)

t.forward(13)

t.setheading(0)

t.forward(15)

t.setheading(150)

t.circle(-20,40)

t.circle(-10,170)

t.setheading(70)

t.circle(-10,170)

t.circle(-20,40)

t.setheading(0)

t.forward(15)

t.setheading(-80)

t.forward(7)

t.setheading(70)

t.forward(25)

t.setheading(-85)

t.forward(29)

t.setheading(70)

t.forward(13)

t.setheading(0)

t.forward(15)

t.setheading(0)

t.forward(10)

t.setheading(45)

t.circle(50,10)

t.setheading(0)

t.circle(-3,90)

t.circle(50,5)

t.setheading(0)

t.forward(10)

defwrite_name(x,y,size,ss):

t.hideturtle()

t.penup()

t.goto(x,y)

t.pendown()

t.pencolor('black')

t.write(ss,font=('TimesNewRoman',size,'normal'))

defundo_back():

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

t.undo()

defundo_back2():

t.undo()

t.undo()

defname_heart_bit():

#寫兩個人的姓名(需替換成真實姓名)

write_name(-180,70,11,'韓商言')

write_name(-180,70,11,'韓商言')

write_name(-180,70,11,'韓商言')

heart_bit()

write_name(-60,70,11,'佟年')

write_name(-60,70,11,'佟年')

write_name(-60,70,11,'佟年')

write

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論