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

使用Python pyglet庫編寫一個(gè)可播放音樂的揚(yáng)聲器類流程詳解

 更新時(shí)間:2024年03月23日 10:19:59   作者:Hann Yang  
這篇文章主要介紹了使用Python pyglet庫編寫一個(gè)可播放音樂的揚(yáng)聲器類,Pyglet主要用于創(chuàng)建視頻游戲、獨(dú)立游戲和多媒體應(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)文章

最新評論