目錄
揚(yáng)聲器類
1. 繪制喇叭
2. 揚(yáng)聲器類
3. 禁音狀態(tài)?
4. 設(shè)置狀態(tài)
5. 切換狀態(tài)
6. 播放音樂
揚(yáng)聲器類
1. 繪制喇叭
本篇將教你用pyglet畫一個小喇叭,如上圖。這里要用到pyglety庫shapes模塊中的圓弧Arc和多邊形Pylygon畫出這個揚(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ù),以水平線起始時,值為0;
圓弧控件沒有表示粗細(xì)的參數(shù),只能多畫幾個同心圓弧來加粗。
Polygon(*coordinates, color=(255, 255, 255, 255), batch=None, group=None)
coordinates是多邊形的各個端點(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()
2. 揚(yáng)聲器類
改寫為一個類便于調(diào)用,可以畫在任意坐標(biāo)處:
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)行效果:
3. 禁音狀態(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)行效果:
4. 設(shè)置狀態(tài)
再為Speaker類增加兩個屬性和一個方法,用于設(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)行效果:
5. 切換狀態(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)擊兩個圖標(biāo),就能各自切換狀態(tài)
6. 播放音樂
使用 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)。文章來源:http://www.zghlxwxcb.cn/news/detail-838194.html
完文章來源地址http://www.zghlxwxcb.cn/news/detail-838194.html
到了這里,關(guān)于Python 一步一步教你用pyglet制作可播放音樂的揚(yáng)聲器類的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!