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

     def load_data(self):
file_path = path.join(self.dir, HIGH_SCORE_FILE)
if path.exists(file_path):
with open(file_path, "r") as f:
try:
self.high_score = int(f.read())
except:
self.high_score = 0
self.spritesheet = Spritesheet(path.join(self.dir, SPRITE_SHEET_PNG_FILE),
path.join(self.dir, SPRITE_SHEET_XML_FILE)) # 设置声音目录
# 声音素材,可通过https://www.bfxr.net/获取
self.snd_dir = path.join(self.dir, "../snd")
self.jump_sound = pg.mixer.Sound(path.join(self.snd_dir, "Jump.wav"))

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

     def new(self):
self.score = 0
...
# 加载背景音乐
pg.mixer.music.load(path.join(self.snd_dir, "bgm.mp3"))
self.run()

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

     def run(self):
# 循环播放背景音乐
pg.mixer.music.play(-1)
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
# game over时背景音乐淡出
pg.mixer.music.fadeout(500)

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

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

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

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

     def jump(self):
hits = pg.sprite.spritecollide(self, self.game.platforms, False)
if hits and not self.jumping:
# 播放声音
self.game.jump_sound.play()
...

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

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

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

pygame-KidsCanCode系列jumpy-part14-背景音乐及音效的更多相关文章

  1. Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现

    为了进一步了解背景音乐和音效播放的,我们通过一个实例给大家介绍一下.如下图所示有两个场景:HelloWorld和Setting.在HelloWorld场景点击“游戏设置”菜单可以切换到Setting场 ...

  2. android开发之背景音乐与音效

    android开发之背景音乐与音效 一:添加背景音乐(MediaPlayer) MediaPlayer class can be used to control playback of audio/v ...

  3. Cocos2d-x使用UserDefault数据持久化实例:保存背景音乐和音效设置

    UserDefault可以实现数据的存储,但是它的使用不能泛滥,具体讲一般情况下不会使用它保存大量的数据,它没有SQL语句那样的灵活.UserDefault除了保存游戏设置外,还有可以长期保持游戏精灵 ...

  4. Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)

    游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...

  5. 关于Cocos2d-x中背景音乐和音效的添加

    1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...

  6. pygame 笔记-8 背景音乐&子弹音效

    游戏哪能没有音效?这节我们研究下如何加背景音乐,其实也很简单: # 加载背景音乐 pygame.mixer.music.load(music_base_path + "music.mp3&q ...

  7. Pygame播放背景音乐与音效

    1.播放背景音乐 pygame.mixer.music.load() 加载MP3格式 加入pygame.mixer.init()即可 第十一行第一个参数:播放次数(n>0),n=0时播放1次,- ...

  8. Cocos2d-x实例:设置背景音乐与音效-设置场景实现

    设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...

  9. Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现

    HelloWorld场景就是游戏中的主菜单场景.HelloWorld.h文件代码如下: #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h& ...

随机推荐

  1. bzoj 5099: [POI2018]Pionek

    题解: 还是比较简单的一道题 考虑现在有一个向量,当且仅当下一个向量与它夹角<90度这个向量的模长才会增加 接下来怎么做呢 如果我们去枚举初始向量,向量方向会随着新增向量而变化 随着不断顺时针的 ...

  2. mysql数据库授权

    mysql数据库授权所有人 grant all privileges on *.* to 'root'@'%' identified by 'password'; flush privileges; ...

  3. DataFrames和Kudu

    Kudu为Kudu表提供了一个自定义的原生数据源.可以和DataFrame API紧密集成: 使用DataFrame的好处就是可以从很多的数据源创建dataframe,包括现有的RDD,Hive表或S ...

  4. HDU3488 Tour KM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8284304.html 题目传送门 - HDU3488 题意概括 给一个n的点m条边的有向图. 然后让你把这个图分 ...

  5. Trident的过滤操作

    1.过滤操作 只是判断某个tuple是否保留 无需跨网络,无需跨分区 不会改变tuple的结构,只是改变tuple的数量 2.需求 过滤掉不是订单的tuple. 其中订单中包含“IBEIfeng.gi ...

  6. Python3 序列解包

    转载自:https://blog.csdn.net/yilovexing/article/details/80576788 序列解包是 Python 3.0 之后才有的语法 什么是序列解包呢?先看一个 ...

  7. mybatis的xml处理大于和小于号问题

    https://blog.csdn.net/u022812849/article/details/42123007

  8. python网站开发准备ubuntu14.04安装mysql实现windows管理

    sudo apt-get install mysql-server mysql-client 输入root密码 然后确认安装tab选定确认 输入数据库密码 重复输入 启动 sudo service m ...

  9. js函数和变量的声明与执行顺序

    一.函数执行顺序 1.正常顺序 function f(){ alert(2); } f(); //alert 2 所有浏览器都能测试通过. 2.倒序调用 f(); //alert 2 function ...

  10. ContextRefreshedEvent事件使用注意事项(Spring)

    0 概述ContextRefreshedEvent 事件会在Spring容器初始化完成会触发该事件.我们在实际工作也可以能会监听该事件去做一些事情,但是有时候使用不当也会带来一些问题. 1 防止重复触 ...