Python+Turtle繪制可愛的小蜜蜂詳解
公眾號中有個朋友私信我,想要幫忙畫下小蜜蜂。
答應(yīng)了有時間就幫忙畫下,趁著五一休息,今天就一起來實現(xiàn)一下吧。
一、效果展示

在正式進入代碼講解之前,先來看下本文的實現(xiàn)效果。
感興趣的朋友可以根據(jù)自己的需要調(diào)整函數(shù),繪制別的形狀的小蜜蜂。
二、代碼詳解
本小節(jié)會詳細解鎖如何通過Python中的turtle庫繪制小蜜蜂。
1.導(dǎo)入庫
首先導(dǎo)入本文需要加載的庫,如果你有些庫還沒有安裝,導(dǎo)致運行代碼時報錯,可以在Anaconda Prompt中用pip方法安裝。
import os import pygame import turtle as t
本文應(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:\公眾號\51.小蜜蜂\樂進大將軍 - 照耀星河.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)
print('播放音樂')
###############不需要播放音樂的話這一段可以直接刪除##################這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。
如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的地址填進去。
3.畫小蜜蜂的頭
然后進入小蜜蜂的正式繪制過程,先畫的是頭部外輪廓。
t.title('阿黎逸陽的代碼公眾號')
t.speed(1)
t.setup(startx=0, starty = 0, width=800, height = 600)
#畫身體
#畫頭
print('畫頭')
t.penup()
t.goto(-50, 50)
t.pendown()
t.begin_fill()
t.left(40)
t.pendown()
t.pensize(2)
t.color('black', 'yellow')
t.circle(100, 5)
t.circle(25, 130)
t.circle(30, 70)
t.left(60)
t.circle(90, 32)
t.end_fill()
print('畫脖子')關(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é)畫脖子和腹部。
#畫脖子
t.left(180)
t.begin_fill()
t.fillcolor('brown')
t.circle(-90, 32)
t.left(110)
t.circle(50, 20)
t.setheading(-60)
t.circle(80, 28)
t.setheading(23)
t.circle(60, 22)
t.end_fill()
print('畫腹部')
#畫腹部
t.left(180)
t.circle(-60, 20)
t.begin_fill()
t.fillcolor('yellow')
t.circle(-50, 25)
t.setheading(140)
t.circle(-40, 33)
t.setheading(80)
t.circle(-40, 20)
t.setheading(-60)
t.circle(80, 28)
t.end_fill()
print('畫尾巴')其余代碼用到的函數(shù)也大致相同,由于篇幅原因,只在最后放上不帶背景音樂的全量代碼。
三、完整代碼
本小節(jié)放上全量繪制小蜜蜂的代碼,感興趣的朋友可以自行調(diào)整,繪制別的飛行狀態(tài)的小蜜蜂。
import os
import pygame
import turtle as t
t.title('阿黎逸陽的代碼公眾號')
t.speed(1)
t.setup(startx=0, starty = 0, width=800, height = 600)
#畫身體
#畫頭
print('畫頭')
t.penup()
t.goto(-50, 50)
t.pendown()
t.begin_fill()
t.left(40)
t.pendown()
t.pensize(2)
t.color('black', 'yellow')
t.circle(100, 5)
t.circle(25, 130)
t.circle(30, 70)
t.left(60)
t.circle(90, 32)
t.end_fill()
print('畫脖子')
#畫脖子
t.left(180)
t.begin_fill()
t.fillcolor('brown')
t.circle(-90, 32)
t.left(110)
t.circle(50, 20)
t.setheading(-60)
t.circle(80, 28)
t.setheading(23)
t.circle(60, 22)
t.end_fill()
print('畫腹部')
#畫腹部
t.left(180)
t.circle(-60, 20)
t.begin_fill()
t.fillcolor('yellow')
t.circle(-50, 25)
t.setheading(140)
t.circle(-40, 33)
t.setheading(80)
t.circle(-40, 20)
t.setheading(-60)
t.circle(80, 28)
t.end_fill()
print('畫尾巴')
#畫尾巴
t.penup()
t.goto(-89, 36)
t.pendown()
t.begin_fill()
t.fillcolor('brown')
t.setheading(140)
t.circle(-40, 31)
t.setheading(-120)
t.circle(20, 40)
t.setheading(-150)
t.circle(-50, 14)
t.setheading(-45)
t.circle(9, 90)
t.setheading(-40)
t.circle(10, 70)
t.end_fill()
print('畫翅膀')
#畫翅膀
t.penup()
t.goto(-88, 83)
t.pendown()
t.begin_fill()
t.fillcolor('LightSkyBlue')
t.setheading(100)
t.circle(22, 80)
t.setheading(-150)
t.circle(30, 20)
t.setheading(-100)
t.circle(20, 107)
t.end_fill()
t.setheading(-180)
t.begin_fill()
t.circle(50, 20)
t.circle(8, 180)
t.circle(30, 15)
t.end_fill()
print('畫眼睛')
#畫眼睛
#左眼睛
t.penup()
t.goto(-70, 85)
t.pendown()
t.setheading(30)
t.circle(-8, 90)
#右眼睛
t.penup()
t.goto(-52, 82)
t.pendown()
t.setheading(40)
t.circle(-7, 90)
print('畫嘴巴')
#畫嘴巴
t.penup()
t.goto(-62, 75)
t.pendown()
t.setheading(-50)
t.circle(6, 90)
print('畫耳朵')
#畫耳朵
t.penup()
t.goto(-68, 98)
t.pendown()
t.setheading(110)
t.circle(20, 60)
t.penup()
t.goto(-58, 98)
t.pendown()
t.setheading(80)
t.circle(-18, 80)
print('畫飛行的陰影')
#畫飛行的陰影
#線1
t.penup()
t.goto(-100, 105)
t.pendown()
t.pensize(1.3)
t.setheading(50)
t.circle(-18, 35)
#線2
t.penup()
t.goto(-97, 104)
t.pendown()
t.pensize(1.3)
t.setheading(40)
t.circle(-18, 25)
#線3
t.penup()
t.goto(-120, 70)
t.pendown()
t.setheading(92)
t.circle(-18, 23)
#線4
t.penup()
t.goto(-118, 70)
t.pendown()
t.setheading(92)
t.circle(-18, 10)
#線5
t.penup()
t.goto(-115, 43)
t.pendown()
t.setheading(115)
t.circle(-18, 23)
#線6
t.penup()
t.goto(-113, 46)
t.pendown()
t.setheading(115)
t.circle(-18, 13)
t.hideturtle()
print('寫詩')
def write_1(x, y, size, 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', size, 'normal'))
def write_2():
x = 120
y = 150
write_1(x, y, 14, '詠蜂 (王錦)')
write_1(x-17, y-40, 13, '紛 紛 穿 飛 萬 花 間,')
write_1(x-17, y-40*2, 13, '終 生 未 得 半 日 閑。')
write_1(x-17, y-40*3, 13, '世 人 都 夸 蜜 味 好,')
write_1(x-17, y-40*4, 13, '釜 底 添 薪 有 誰 憐。')
write_2()
write_2()
write_2()
write_2()
write_2()
write_2()
write_2()
write_2()
write_2()
write_2()
print('繪圖完畢')到此這篇關(guān)于Python+Turtle繪制可愛的小蜜蜂詳解的文章就介紹到這了,更多相關(guān)Python Turtle蜜蜂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談selenium如何應(yīng)對網(wǎng)頁內(nèi)容需要鼠標滾動加載的問題
這篇文章主要介紹了淺談selenium如何應(yīng)對網(wǎng)頁內(nèi)容需要鼠標滾動加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
使用Mac時psycopg2導(dǎo)入PyCharm失敗的解決
這篇文章主要介紹了使用Mac時psycopg2導(dǎo)入PyCharm失敗的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
解決多個@Scheduled定時任務(wù)執(zhí)行時個別不執(zhí)行問題
這篇文章主要介紹了解決多個@Scheduled定時任務(wù)執(zhí)行時個別不執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Python 把兩層列表展開平鋪成一層(5種實現(xiàn)方式)
這篇文章主要介紹了Python 把兩層列表展開平鋪成一層(5種實現(xiàn)方式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Python調(diào)用.net動態(tài)庫實現(xiàn)過程解析
這篇文章主要介紹了Python調(diào)用.net動態(tài)庫實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
python網(wǎng)絡(luò)爬蟲實現(xiàn)個性化音樂播放器示例解析
這篇文章主要為大家介紹了使用python網(wǎng)絡(luò)爬蟲實現(xiàn)個性化音樂播放器的詳細示例代碼以及內(nèi)容解析,有需要的朋友?可以借鑒參考下希望能夠有所幫助2022-03-03

