使用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 創(chuàng)建一個(gè)保留重復(fù)值的列表的補(bǔ)碼
這篇文章主要介紹了python 創(chuàng)建一個(gè)保留重復(fù)值的列表的補(bǔ)碼的相關(guān)資料,需要的朋友可以參考下2018-10-10python實(shí)現(xiàn)去掉字符串中的\xa0、\t、\n
這篇文章主要介紹了python實(shí)現(xiàn)去掉字符串中的\xa0、\t、\n方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08python查找指定文件夾下所有文件并按修改時(shí)間倒序排列的方法
今天小編就為大家分享一篇python查找指定文件夾下所有文件并按修改時(shí)間倒序排列的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python實(shí)現(xiàn)多路視頻多窗口播放功能
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)多路視頻多窗口播放功能的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02python3實(shí)現(xiàn)全角和半角字符轉(zhuǎn)換的方法示例
在自然語言處理過程中,全角、半角的的不一致會導(dǎo)致信息抽取不一致,因此需要統(tǒng)一,下面這篇文章主要給大家介紹了關(guān)于python3中全角和半角字符轉(zhuǎn)換的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09解決Pycharm調(diào)用Turtle時(shí) 窗口一閃而過的問題
今天小編就為大家分享一篇解決Pycharm調(diào)用Turtle時(shí) 窗口一閃而過的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python?中如何使用requests模塊發(fā)布表單數(shù)據(jù)
requests 庫是 Python 的主要方面之一,用于創(chuàng)建對已定義 URL 的 HTTP 請求,本篇文章介紹了 Python requests 模塊,并說明了我們?nèi)绾问褂迷撃K在 Python 中發(fā)布表單數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2023-06-06