Python+Turtle繪制可愛的小蜜蜂詳解
公眾號中有個朋友私信我,想要幫忙畫下小蜜蜂。
答應(yīng)了有時間就幫忙畫下,趁著五一休息,今天就一起來實現(xiàn)一下吧。
一、效果展示
在正式進(jìn)入代碼講解之前,先來看下本文的實現(xiàn)效果。
感興趣的朋友可以根據(jù)自己的需要調(diào)整函數(shù),繪制別的形狀的小蜜蜂。
二、代碼詳解
本小節(jié)會詳細(xì)解鎖如何通過Python中的turtle庫繪制小蜜蜂。
1.導(dǎo)入庫
首先導(dǎo)入本文需要加載的庫,如果你有些庫還沒有安裝,導(dǎo)致運(yùn)行代碼時報錯,可以在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.小蜜蜂\樂進(jìn)大將軍 - 照耀星河.mp3") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(1, 10) print('播放音樂') ###############不需要播放音樂的話這一段可以直接刪除##################
這一部分的代碼和整體代碼是剝離的,可以選澤在最開始放上該代碼,也可以直接刪除。
如果選擇播放音樂,需要在代碼music.load函數(shù)中把你想放音樂的地址填進(jìn)去。
3.畫小蜜蜂的頭
然后進(jìn)入小蜜蜂的正式繪制過程,先畫的是頭部外輪廓。
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)去到的橫坐標(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é)曲線的弧度,從而使得小蜜蜂的輪廓比較流暢。
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)文章
python分析實現(xiàn)微信釘釘?shù)溶浖嚅_分身
我發(fā)現(xiàn)壇友分享的很多都是通過cmd?去start?多個微信,雖然能實現(xiàn)多開,但不夠靈活,比如我上午登錄了一個微信,下午在登錄就不太好用了,當(dāng)然也可能是我start的姿勢不對。于是我就搜了下單實例原理,自己動手實現(xiàn)了個隨用隨開的2022-02-02Python中出現(xiàn)"No?module?named?'requests'"
這篇文章主要給大家介紹了關(guān)于Python中出現(xiàn)"No?module?named?'requests'"的解決辦法,"No?module?named?requests"是Python報錯提示,意味著你在使用某個Python程序或腳本時,沒有找到名為requests的模塊,需要的朋友可以參考下2023-11-11解決python DataFrame 打印結(jié)果不換行問題
這篇文章主要介紹了解決python DataFrame 打印結(jié)果不換行問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python Pygame實戰(zhàn)之趣味籃球游戲的實現(xiàn)
這篇文章主要為大家分享了一個基于Python和Pygame實現(xiàn)的一個趣味籃球游戲,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-04-04python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞的實例文章,需要的朋友們可以跟著操作下。2019-06-06Python:type、object、class與內(nèi)置類型實例
今天小編就為大家分享一篇Python:type、object、class與內(nèi)置類型實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12