首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pygame-KidsCanCode系列jumpy-part14-背景音乐及音效

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

作者头像
菩提树下的杨过
发布2019-09-12 09:35:14
1.3K0
发布2019-09-12 09:35:14
举报

没有音乐和音效的游戏是没有灵魂的,这回讲解如何处理背景音乐及跳跃音效。加载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"))

然后在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()

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)

注:这里用了一个小技巧,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)
 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)

目前为止,跳跃的音效还没使用到,可以要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            ...

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

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档