使用Python pyglet庫編寫一個(gè)可播放音樂的揚(yáng)聲器類流程詳解
一、繪制喇叭
本篇將教你用pyglet畫一個(gè)小喇叭,如下圖。這里要用到pyglet庫shapes模塊中的圓弧Arc和多邊形Pylygon畫出這個(gè)揚(yáng)聲器的圖片:

Arc(x, y, radius, segments=None, angle=6.283185307179586, start_angle=0, closed=False, color=(255, 255, 255, 255), batch=None, group=None)
x,y 是圓弧的圓心坐標(biāo);
radius 是半徑;
angle是圓心角的弧度數(shù);
start_angle是圓弧起始的弧度數(shù),以水平線起始時(shí),值為0;
圓弧控件沒有表示粗細(xì)的參數(shù),只能多畫幾個(gè)同心圓弧來加粗。
Polygon(*coordinates, color=(255, 255, 255, 255), batch=None, group=None)
coordinates是多邊形的各個(gè)端點(diǎn)的坐標(biāo)列表,也可以寫成元組方式;
多邊形控件是填充形狀,沒有粗細(xì)參數(shù)也不能只畫邊線。
代碼如下:
import pyglet
window = pyglet.window.Window(800,500)
batch = pyglet.graphics.Batch()
color = (255, 255, 255)
pi = 3.141592653589793
arc = []
x, y = 380, 250
for i in [*range(6),*range(18,24),*range(36,42)]:
arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
@window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()二、揚(yáng)聲器類
改寫為一個(gè)類便于調(diào)用,可以畫在任意坐標(biāo)處:
class Speaker:
class Speaker:
def __init__(self, x, y, color=(255, 255, 255)):
self.arc = []
pi = 3.141592653589793
for i in [*range(6),*range(18,24),*range(36,42)]:
self.arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)調(diào)用代碼:
import pyglet
window = pyglet.window.Window(800,500)
batch = pyglet.graphics.Batch()
class Speaker:
def __init__(self, x, y, color=(255, 255, 255)):
self.arc = []
pi = 3.141592653589793
for i in [*range(6),*range(18,24),*range(36,42)]:
self.arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
@window.event
def on_draw():
window.clear()
batch.draw()
speaker1 = Speaker(380, 250)
speaker2 = Speaker(600, 360)
pyglet.app.run()運(yùn)行效果:

三、禁音狀態(tài)
再加兩條紅色直線表示禁音狀態(tài),shapes.Line用法:
Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)
x,y, x2,y2 為直線兩端點(diǎn)的坐標(biāo);
width為直線粗細(xì),缺省默認(rèn)值為1,直線控件有粗細(xì)的。
代碼如下:
import pyglet
window = pyglet.window.Window(800,500)
batch = pyglet.graphics.Batch()
class Speaker:
def __init__(self, x, y, color=(255, 255, 255)):
self.arc = []
pi = 3.141592653589793
for i in [*range(6),*range(18,24),*range(36,42)]:
self.arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
@window.event
def on_draw():
window.clear()
batch.draw()
speaker1 = Speaker(380, 250)
speaker2 = Speaker(600, 360)
pyglet.app.run()運(yùn)行效果:

四、設(shè)置狀態(tài)
再為Speaker類增加兩個(gè)屬性和一個(gè)方法,用于設(shè)置狀態(tài):
self.line1.visible =Flase
self.line2.visible = Flase
def enabled(self, enabled=True):
self.line1.visible = self.line2.visible = not enabled
調(diào)用代碼:
import pyglet
window = pyglet.window.Window(800,500)
batch = pyglet.graphics.Batch()
class Speaker:
def __init__(self, x, y, color=(255, 255, 255)):
self.arc = []
pi = 3.141592653589793
for i in [*range(6),*range(18,24),*range(36,42)]:
self.arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
self.line1.visible = self.line2.visible = False
def set_enabled(self, enabled=True):
self.line1.visible = self.line2.visible = not enabled
@window.event
def on_draw():
window.clear()
batch.draw()
speaker1 = Speaker(380, 250)
speaker2 = Speaker(600, 360)
speaker2.set_enabled(False)
pyglet.app.run()運(yùn)行效果:

五、切換狀態(tài)
繼續(xù)增加鼠標(biāo)點(diǎn)擊切換狀態(tài)的功能,增加屬性和方法:
屬性:
self.x = x
self.y = y
self.enabled = True
方法:
def set_enabled(self, enabled=True):
self.enabled = enabled
self.line1.visible = self.line2.visible = not enabled
def on_mouse_over(self, x, y):
return self.x <= x <= self.x+50 and self.y-35 <= y <= self.y+35
增加鼠標(biāo)點(diǎn)擊事件:
@window.event
def on_mouse_press(x, y, button, modifier):
if speaker1.on_mouse_over(x,y):
speaker1.enabled = not speaker1.enabled
speaker1.set_enabled(speaker1.enabled)
if speaker2.on_mouse_over(x,y):
speaker2.enabled = not speaker2.enabled
speaker2.set_enabled(speaker2.enabled)
運(yùn)行效果:分別點(diǎn)擊兩個(gè)圖標(biāo),就能各自切換狀態(tài)

六、播放音樂
使用 media 模塊調(diào)入mp3音樂,配合Speaker類播放
media = pyglet.media.load('voice1.mp3')
sound = pyglet.media.Player()
sound.queue(media)
sound.loop = True
sound.play()
鼠標(biāo)事件中增加音樂播放和暫停的代碼:
@window.event
def on_mouse_press(x, y, button, modifier):
if speaker.on_mouse_over(x,y):
speaker.enabled = not speaker.enabled
speaker.set_enabled(speaker.enabled)
if speaker.enabled:
sound.play()
else:
sound.pause()
完整代碼:
import pyglet
window = pyglet.window.Window(800,500)
batch = pyglet.graphics.Batch()
class Speaker:
def __init__(self, x, y, color=(255, 255, 255)):
self.arc = []
pi = 3.141592653589793
for i in [*range(6),*range(18,24),*range(36,42)]:
self.arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
self.line1.visible = self.line2.visible = False
self.x = x
self.y = y
self.enabled = True
def set_enabled(self, enabled=True):
self.enabled = enabled
self.line1.visible = self.line2.visible = not enabled
def on_mouse_over(self, x, y):
return self.x <= x <= self.x+50 and self.y-35 <= y <= self.y+35
@window.event
def on_draw():
window.clear()
batch.draw()
@window.event
def on_mouse_press(x, y, button, modifier):
if speaker.on_mouse_over(x,y):
speaker.enabled = not speaker.enabled
speaker.set_enabled(speaker.enabled)
if speaker.enabled:
sound.play()
else:
sound.pause()
speaker = Speaker(720, 450)
media = pyglet.media.load('voice1.mp3')
sound = pyglet.media.Player()
sound.queue(media)
sound.loop = True
sound.play()
pyglet.app.run()運(yùn)行代碼后,就能播放音樂了,點(diǎn)擊揚(yáng)聲器圖標(biāo)可以切換音樂的播放和暫停狀態(tài)。
以上就是使用Python pyglet庫編寫一個(gè)可播放音樂的揚(yáng)聲器類流程詳解的詳細(xì)內(nèi)容,更多關(guān)于Python pyglet的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python運(yùn)維自動化之paramiko模塊應(yīng)用實(shí)例
paramiko是一個(gè)基于SSH用于連接遠(yuǎn)程服務(wù)器并執(zhí)行相關(guān)操作,使用該模塊可以對遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,這篇文章主要給大家介紹了關(guān)于Python運(yùn)維自動化之paramiko模塊應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-09-09
python實(shí)現(xiàn)MD5進(jìn)行文件去重的示例代碼
工作中偶爾會收到一大堆文件,名稱各不相同,分析文件的時(shí)候發(fā)現(xiàn)有不少重復(fù)的文件,導(dǎo)致工作效率低下,那么,這里就寫了一個(gè)python腳本實(shí)現(xiàn)文件去重功能,感興趣的就一起來了解一下2021-07-07
基于Python實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖
這篇文章主要介紹了如何利用Python中的pandas庫和matplotlib庫,實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖,文中的示例代碼簡潔易懂,需要的可以參考一下2022-03-03
Python數(shù)據(jù)處理之Excel報(bào)表自動化生成與分析
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)一個(gè)完整的Excel報(bào)表自動化系統(tǒng),涵蓋從數(shù)據(jù)清洗、分析到可視化報(bào)表生成的全流程,希望對大家有所幫助2025-07-07
Python + selenium + crontab實(shí)現(xiàn)每日定時(shí)自動打卡功能
這篇文章主要介紹了Python + selenium + crontab實(shí)現(xiàn)每日定時(shí)自動打卡功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
基于Python實(shí)現(xiàn)在線二維碼生成工具
這篇文章將為大家展示如何通過純Python編程的方式,開發(fā)出一個(gè)網(wǎng)頁應(yīng)用—基于輸入的網(wǎng)址等文字內(nèi)容實(shí)現(xiàn)二維碼的生成,感興趣的可以學(xué)習(xí)一下2022-05-05
Python使用PyQt實(shí)現(xiàn)網(wǎng)頁加載過程
這篇文章主要介紹了Python使用PyQt實(shí)現(xiàn)網(wǎng)頁加載過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06

