您现在的位置是:首页 > 技术教程 正文

Python 一步一步教你用pyglet制作可播放音乐的扬声器类

admin 阅读: 2024-03-25
后台-插件-广告管理-内容页头部广告(手机)

目录

扬声器类

1. 绘制喇叭

2. 扬声器类

3. 禁音状态 

4. 设置状态

5. 切换状态

6. 播放音乐


扬声器类

1. 绘制喇叭

本篇将教你用pyglet画一个小喇叭,如上图。这里要用到pyglety库shapes模块中的圆弧Arc和多边形Pylygon画出这个扬声器的图片:

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 是圆弧的圆心坐标;radius 是半径;

angle是圆心角的弧度数;

start_angle是圆弧起始的弧度数,以水平线起始时,值为0;

圆弧控件没有表示粗细的参数,只能多画几个同心圆弧来加粗。

Polygon(*coordinates, color=(255, 255, 255, 255), batch=None, group=None)

coordinates是多边形的各个端点的坐标列表,也可以写成元组方式;

多边形控件是填充形状,没有粗细参数也不能只画边线。

代码如下:

  1. import pyglet
  2. window = pyglet.window.Window(800,500)
  3. batch = pyglet.graphics.Batch()
  4. color = (255, 255, 255)
  5. pi = 3.141592653589793
  6. arc = []
  7. x, y = 380, 250
  8. for i in [*range(6),*range(18,24),*range(36,42)]:
  9. arc.append(pyglet.shapes.Arc(x=x, y=y, radius=50-i/2, angle=pi/2, start_angle=-pi/4, color=color, batch=batch))
  10. coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
  11. polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
  12. @window.event
  13. def on_draw():
  14. window.clear()
  15. batch.draw()
  16. pyglet.app.run()

2. 扬声器类

改写为一个类便于调用,可以画在任意坐标处:

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)

调用代码:

  1. import pyglet
  2. window = pyglet.window.Window(800,500)
  3. batch = pyglet.graphics.Batch()
  4. class Speaker:
  5. def __init__(self, x, y, color=(255, 255, 255)):
  6. self.arc = []
  7. pi = 3.141592653589793
  8. for i in [*range(6),*range(18,24),*range(36,42)]:
  9. 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))
  10. coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
  11. self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
  12. @window.event
  13. def on_draw():
  14. window.clear()
  15. batch.draw()
  16. speaker1 = Speaker(380, 250)
  17. speaker2 = Speaker(600, 360)
  18. pyglet.app.run()

运行效果:

3. 禁音状态 

再加两条红色直线表示禁音状态,shapes.Line用法:

Line(x, y, x2, y2, width=1, color=(255, 255, 255, 255), batch=None, group=None)

x,y, x2,y2 为直线两端点的坐标;

width为直线粗细,缺省默认值为1,直线控件有粗细的。

代码如下:

  1. import pyglet
  2. window = pyglet.window.Window(800,500)
  3. batch = pyglet.graphics.Batch()
  4. class Speaker:
  5. def __init__(self, x, y, color=(255, 255, 255)):
  6. self.arc = []
  7. pi = 3.141592653589793
  8. for i in [*range(6),*range(18,24),*range(36,42)]:
  9. 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))
  10. coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
  11. self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
  12. self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
  13. self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
  14. @window.event
  15. def on_draw():
  16. window.clear()
  17. batch.draw()
  18. speaker1 = Speaker(380, 250)
  19. speaker2 = Speaker(600, 360)
  20. pyglet.app.run()

运行效果:

4. 设置状态

再为Speaker类增加两个属性和一个方法,用于设置状态:

        self.line1.visible = Flase
        self.line2.visible = Flase

    def enabled(self, enabled=True):
        self.line1.visible = self.line2.visible = not enabled

调用代码:

  1. import pyglet
  2. window = pyglet.window.Window(800,500)
  3. batch = pyglet.graphics.Batch()
  4. class Speaker:
  5. def __init__(self, x, y, color=(255, 255, 255)):
  6. self.arc = []
  7. pi = 3.141592653589793
  8. for i in [*range(6),*range(18,24),*range(36,42)]:
  9. 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))
  10. coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
  11. self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
  12. self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
  13. self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
  14. self.line1.visible = self.line2.visible = False
  15. def set_enabled(self, enabled=True):
  16. self.line1.visible = self.line2.visible = not enabled
  17. @window.event
  18. def on_draw():
  19. window.clear()
  20. batch.draw()
  21. speaker1 = Speaker(380, 250)
  22. speaker2 = Speaker(600, 360)
  23. speaker2.set_enabled(False)
  24. pyglet.app.run()

运行效果:

5. 切换状态

继续增加鼠标点击切换状态的功能,增加属性和方法:

属性:

        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_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)

运行效果:分别点击两个图标,就能各自切换状态

6. 播放音乐

使用 media 模块调入mp3音乐,配合Speaker类播放

media = pyglet.media.load('voice1.mp3')
sound = pyglet.media.Player()
sound.queue(media)
sound.loop = True
sound.play()

鼠标事件中增加音乐播放和暂停的代码:

@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() 

完整代码:

  1. import pyglet
  2. window = pyglet.window.Window(800,500)
  3. batch = pyglet.graphics.Batch()
  4. class Speaker:
  5. def __init__(self, x, y, color=(255, 255, 255)):
  6. self.arc = []
  7. pi = 3.141592653589793
  8. for i in [*range(6),*range(18,24),*range(36,42)]:
  9. 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))
  10. coordinates = [x+10, y+8], [x, y+8], [x, y-8], [x+10, y-8], [x+16, y-14], [x+16, y+14]
  11. self.polygon = pyglet.shapes.Polygon(*coordinates, color=color, batch=batch)
  12. self.line1 = pyglet.shapes.Line(x, y-24, x+48, y+24, width=3, color=(255, 0, 0), batch=batch)
  13. self.line2 = pyglet.shapes.Line(x, y+24, x+48, y-24, width=3, color=(255, 0, 0), batch=batch)
  14. self.line1.visible = self.line2.visible = False
  15. self.x = x
  16. self.y = y
  17. self.enabled = True
  18. def set_enabled(self, enabled=True):
  19. self.enabled = enabled
  20. self.line1.visible = self.line2.visible = not enabled
  21. def on_mouse_over(self, x, y):
  22. return self.x <= x <= self.x+50 and self.y-35 <= y <= self.y+35
  23. @window.event
  24. def on_draw():
  25. window.clear()
  26. batch.draw()
  27. @window.event
  28. def on_mouse_press(x, y, button, modifier):
  29. if speaker.on_mouse_over(x,y):
  30. speaker.enabled = not speaker.enabled
  31. speaker.set_enabled(speaker.enabled)
  32. if speaker.enabled:
  33. sound.play()
  34. else:
  35. sound.pause()
  36. speaker = Speaker(720, 450)
  37. media = pyglet.media.load('voice1.mp3')
  38. sound = pyglet.media.Player()
  39. sound.queue(media)
  40. sound.loop = True
  41. sound.play()
  42. pyglet.app.run()

运行代码后,就能播放音乐了,点击扬声器图标可以切换音乐的播放和暂停状态。


标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索