• pygame-KidsCanCode系列jumpy-part14-背景音乐及音效


    没有音乐和音效的游戏是没有灵魂的,这回讲解如何处理背景音乐及跳跃音效。加载music及sound的方法,之前已经写过,见:pygame 笔记-8 背景音乐&子弹音效 。

    先介绍一个很棒的生成各种音效的网站:https://www.bfxr.net/,该网站提供了一个音效生成器,界面如下:

    利用该工具,可以生成各种跳跃、爆炸之类的音效wav文件。

    然后就是背景音乐了,pygame支持wav, mp3, ogg等这种格式,但是考虑到背景音乐通常比较大,不建议用wav做背景音乐,最好是mp3或ogg格式,mp3格式有专利,而且pygame对mp3的兼容性不太好,最佳推荐是ogg格式。

    提供2个在线转换成ogg格式的网址:

    https://cloudconvert.com/wav-to-ogg

    https://cloudconvert.com/mp3-to-ogg

    另外,再送一波福利,可以在opengameart.org上找到很多游戏的常用背景音乐:

    https://opengameart.org/art-search-advanced?keys=&field_art_type_tid%5B%5D=12&sort_by=count&sort_order=DESC

    有了这些素材后,就该写代码了:

    将准备好的声音素材,放到指定的目录,参考上图。

    然后在main.py的load_data中,加载跳跃的音效:

     1     def load_data(self):
     2         file_path = path.join(self.dir, HIGH_SCORE_FILE)
     3         if path.exists(file_path):
     4             with open(file_path, "r") as f:
     5                 try:
     6                     self.high_score = int(f.read())
     7                 except:
     8                     self.high_score = 0
     9         self.spritesheet = Spritesheet(path.join(self.dir, SPRITE_SHEET_PNG_FILE),
    10                                        path.join(self.dir, SPRITE_SHEET_XML_FILE))
    11 
    12         # 设置声音目录
    13         # 声音素材,可通过https://www.bfxr.net/获取
    14         self.snd_dir = path.join(self.dir, "../snd")
    15         self.jump_sound = pg.mixer.Sound(path.join(self.snd_dir, "Jump.wav"))
    View Code

    然后在new函数中,加载背景音乐

    1     def new(self):
    2         self.score = 0
    3         ...
    4         # 加载背景音乐
    5         pg.mixer.music.load(path.join(self.snd_dir, "bgm.mp3"))
    6         self.run()
    View Code

    run函数中,循环播放背景音乐:

     1     def run(self):
     2         # 循环播放背景音乐
     3         pg.mixer.music.play(-1)
     4         self.playing = True
     5         while self.playing:
     6             self.clock.tick(FPS)
     7             self.events()
     8             self.update()
     9             self.draw()
    10         # game over时背景音乐淡出
    11         pg.mixer.music.fadeout(500)
    View Code

    注:这里用了一个小技巧,GameOver的时候,如果硬生生把背景音乐关掉,有点突兀,用fadeout淡出方法,会友好一些。

    如果start界面和game over界面,如果希望放另一种背景音乐,也依葫芦画瓢:

    1     def show_start_screen(self):
    2         # 启动界面播放背景音乐
    3         pg.mixer.music.load(path.join(self.snd_dir, "start_and_go.ogg"))
    4         pg.mixer.music.play(-1)
    5         self.screen.fill(BG_COLOR)
    6         ...
    7         self.wait_for_key()
    8         # 有按键开始时,淡出背景音
    9         pg.mixer.music.fadeout(500)
    View Code 
     1     def show_go_screen(self):
     2         # 启动界面播放背景音乐
     3         pg.mixer.music.load(path.join(self.snd_dir, "start_and_go.ogg"))
     4         pg.mixer.music.play(-1)
     5         self.screen.fill(BG_COLOR)
     6         ...
     7         pg.display.update()
     8         self.wait_for_key()
     9         # 有按键开始时,淡出背景音
    10         pg.mixer.music.fadeout(500)
    View Code

    目前为止,跳跃的音效还没使用到,可以要Player类的jump函数中,播放该音效:

    1     def jump(self):
    2         hits = pg.sprite.spritecollide(self, self.game.platforms, False)
    3         if hits and not self.jumping:
    4             # 播放声音
    5             self.game.jump_sound.play()
    6            ...
    View Code

    博客无法直接上传视频文件,最终带声音效果的视频如下:

    链接: https://pan.baidu.com/s/1DTalKLFfYBOLw3MQpLIsig 提取码: wnhs 

    源码:https://github.com/yjmyzz/kids-can-code/tree/master/part_14

  • 相关阅读:
    Centos下,Docker部署Yapi接口管理平台(详细得令人发指)
    Centos7下安装Docker(详细的新手装逼教程)
    性能测试之nmon对linux服务器的监控
    真爱至上,谈谈我的测试经历(二)
    linux问题点滴,给普通用户添加sudo权限
    浅谈软件测试流程(一)
    面试如何谈笑风生,软件测试基础理论整理
    测试与开发如何有效沟通,QC11(HP ALM 11)的简单使用记录
    解决github访问过慢问题
    Python语言下图像的操作方法总结
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/pygame-kidscancode-part14-sound-and-music.html
Copyright © 2020-2023  润新知