欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+Turtle繪制可愛(ài)的小蜜蜂詳解

 更新時(shí)間:2022年05月05日 10:52:34   作者:阿黎逸陽(yáng)  
turtle庫(kù)是一個(gè)點(diǎn)線(xiàn)面的簡(jiǎn)單圖像庫(kù),在Python2.6之后被引入進(jìn)來(lái),能夠完成一些比較簡(jiǎn)單的幾何圖像可視化。本文將利用turtle繪制一個(gè)可愛(ài)的小蜜蜂,感興趣的可以試一試

公眾號(hào)中有個(gè)朋友私信我,想要幫忙畫(huà)下小蜜蜂。

答應(yīng)了有時(shí)間就幫忙畫(huà)下,趁著五一休息,今天就一起來(lái)實(shí)現(xiàn)一下吧。

一、效果展示

在正式進(jìn)入代碼講解之前,先來(lái)看下本文的實(shí)現(xiàn)效果。

感興趣的朋友可以根據(jù)自己的需要調(diào)整函數(shù),繪制別的形狀的小蜜蜂。

二、代碼詳解

本小節(jié)會(huì)詳細(xì)解鎖如何通過(guò)Python中的turtle庫(kù)繪制小蜜蜂。

1.導(dǎo)入庫(kù)

首先導(dǎo)入本文需要加載的庫(kù),如果你有些庫(kù)還沒(méi)有安裝,導(dǎo)致運(yùn)行代碼時(shí)報(bào)錯(cuò),可以在Anaconda Prompt中用pip方法安裝。

import os
import pygame
import turtle as t

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

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

pygame庫(kù)是為了繪制過(guò)程更有趣,在繪圖過(guò)程中添加了背景音樂(lè),如果無(wú)需背景音樂(lè),不用加載該庫(kù)。

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

2.播放音樂(lè)

接著應(yīng)用pygame庫(kù)播放背景音樂(lè),本文的音樂(lè)是《 照耀星河》。

###############不需要播放音樂(lè)的話(huà)這一段可以直接刪除##################
#播放音樂(lè)
print('播放音樂(lè)')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公眾號(hào)\51.小蜜蜂\樂(lè)進(jìn)大將軍 - 照耀星河.mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play(1, 10)
print('播放音樂(lè)')
###############不需要播放音樂(lè)的話(huà)這一段可以直接刪除##################

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

如果選擇播放音樂(lè),需要在代碼music.load函數(shù)中把你想放音樂(lè)的地址填進(jìn)去。

3.畫(huà)小蜜蜂的頭

然后進(jìn)入小蜜蜂的正式繪制過(guò)程,先畫(huà)的是頭部外輪廓。

t.title('阿黎逸陽(yáng)的代碼公眾號(hào)')
t.speed(1)
t.setup(startx=0, starty = 0, width=800, height = 600)
#畫(huà)身體
#畫(huà)頭
print('畫(huà)頭')
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('畫(huà)脖子')

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

t.pensize(width):設(shè)置畫(huà)筆的尺寸。

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

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

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

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

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

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

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

畫(huà)外輪廓的關(guān)鍵是:通過(guò)調(diào)節(jié)circle函數(shù)中的半徑和弧度來(lái)調(diào)節(jié)曲線(xiàn)的弧度,從而使得小蜜蜂的輪廓比較流暢。

4.畫(huà)脖子和腹部

畫(huà)完頭部外輪廓后就可以分模塊畫(huà)其它組成部分了,本小節(jié)畫(huà)脖子和腹部。

#畫(huà)脖子
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('畫(huà)腹部')
#畫(huà)腹部
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('畫(huà)尾巴')

其余代碼用到的函數(shù)也大致相同,由于篇幅原因,只在最后放上不帶背景音樂(lè)的全量代碼。

三、完整代碼

本小節(jié)放上全量繪制小蜜蜂的代碼,感興趣的朋友可以自行調(diào)整,繪制別的飛行狀態(tài)的小蜜蜂。

import os
import pygame
import turtle as t 

t.title('阿黎逸陽(yáng)的代碼公眾號(hào)')
t.speed(1)
t.setup(startx=0, starty = 0, width=800, height = 600)
#畫(huà)身體
#畫(huà)頭
print('畫(huà)頭')
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('畫(huà)脖子')
#畫(huà)脖子
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('畫(huà)腹部')
#畫(huà)腹部
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('畫(huà)尾巴')
#畫(huà)尾巴
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('畫(huà)翅膀')
#畫(huà)翅膀
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('畫(huà)眼睛')
#畫(huà)眼睛
#左眼睛
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('畫(huà)嘴巴')
#畫(huà)嘴巴
t.penup()
t.goto(-62, 75)
t.pendown()
t.setheading(-50)
t.circle(6, 90)
print('畫(huà)耳朵')
#畫(huà)耳朵
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('畫(huà)飛行的陰影')
#畫(huà)飛行的陰影
#線(xiàn)1
t.penup()
t.goto(-100, 105)
t.pendown()
t.pensize(1.3)
t.setheading(50)
t.circle(-18, 35)
#線(xiàn)2
t.penup()
t.goto(-97, 104)
t.pendown()
t.pensize(1.3)
t.setheading(40)
t.circle(-18, 25)
#線(xiàn)3
t.penup()
t.goto(-120, 70)
t.pendown()
t.setheading(92)
t.circle(-18, 23)
#線(xiàn)4
t.penup()
t.goto(-118, 70)
t.pendown()
t.setheading(92)
t.circle(-18, 10)
#線(xiàn)5
t.penup()
t.goto(-115, 43)
t.pendown()
t.setheading(115)
t.circle(-18, 23)
#線(xiàn)6
t.penup()
t.goto(-113, 46)
t.pendown()
t.setheading(115)
t.circle(-18, 13)
t.hideturtle()
print('寫(xiě)詩(shī)')
def write_1(x, y, size, ss):
    t.hideturtle()
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    #t.write('猜謎語(yǔ)', 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, '紛 紛 穿 飛 萬(wàn) 花 間,')
    write_1(x-17, y-40*2, 13, '終 生 未 得 半 日 閑。')
    write_1(x-17, y-40*3, 13, '世 人 都 夸 蜜 味 好,')
    write_1(x-17, y-40*4, 13, '釜 底 添 薪 有 誰(shuí) 憐。')
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繪制可愛(ài)的小蜜蜂詳解的文章就介紹到這了,更多相關(guān)Python Turtle蜜蜂內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python分析實(shí)現(xiàn)微信釘釘?shù)溶浖嚅_(kāi)分身

    python分析實(shí)現(xiàn)微信釘釘?shù)溶浖嚅_(kāi)分身

    我發(fā)現(xiàn)壇友分享的很多都是通過(guò)cmd?去start?多個(gè)微信,雖然能實(shí)現(xiàn)多開(kāi),但不夠靈活,比如我上午登錄了一個(gè)微信,下午在登錄就不太好用了,當(dāng)然也可能是我start的姿勢(shì)不對(duì)。于是我就搜了下單實(shí)例原理,自己動(dòng)手實(shí)現(xiàn)了個(gè)隨用隨開(kāi)的
    2022-02-02
  • Python3 處理JSON的實(shí)例詳解

    Python3 處理JSON的實(shí)例詳解

    這篇文章主要介紹了Python3 處理JSON的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • Python中出現(xiàn)"No?module?named?'requests'"的圖文解決辦法

    Python中出現(xiàn)"No?module?named?'requests'"

    這篇文章主要給大家介紹了關(guān)于Python中出現(xiàn)"No?module?named?'requests'"的解決辦法,"No?module?named?requests"是Python報(bào)錯(cuò)提示,意味著你在使用某個(gè)Python程序或腳本時(shí),沒(méi)有找到名為requests的模塊,需要的朋友可以參考下
    2023-11-11
  • 解決python DataFrame 打印結(jié)果不換行問(wèn)題

    解決python DataFrame 打印結(jié)果不換行問(wèn)題

    這篇文章主要介紹了解決python DataFrame 打印結(jié)果不換行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python Pygame實(shí)戰(zhàn)之趣味籃球游戲的實(shí)現(xiàn)

    Python Pygame實(shí)戰(zhàn)之趣味籃球游戲的實(shí)現(xiàn)

    這篇文章主要為大家分享了一個(gè)基于Python和Pygame實(shí)現(xiàn)的一個(gè)趣味籃球游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-04-04
  • TensorFlow如何指定GPU訓(xùn)練模型

    TensorFlow如何指定GPU訓(xùn)練模型

    這篇文章主要介紹了TensorFlow如何指定GPU訓(xùn)練模型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞

    python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞

    在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞的實(shí)例文章,需要的朋友們可以跟著操作下。
    2019-06-06
  • Python:type、object、class與內(nèi)置類(lèi)型實(shí)例

    Python:type、object、class與內(nèi)置類(lèi)型實(shí)例

    今天小編就為大家分享一篇Python:type、object、class與內(nèi)置類(lèi)型實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Django實(shí)現(xiàn)將一個(gè)字典傳到前端顯示出來(lái)

    Django實(shí)現(xiàn)將一個(gè)字典傳到前端顯示出來(lái)

    這篇文章主要介紹了Django實(shí)現(xiàn)將一個(gè)字典傳到前端顯示出來(lái),具有很好的參考價(jià)值,希望
    2020-04-04
  • python中的sys模塊詳解

    python中的sys模塊詳解

    sys模塊是與python解釋器交互的一個(gè)接口,sys 模塊提供了許多函數(shù)和變量來(lái)處理 Python 運(yùn)行時(shí)環(huán)境的不同部分,這篇文章主要介紹了python之sys模塊詳解,需要的朋友可以參考下
    2022-11-11

最新評(píng)論