ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
一款人性化的游戏中缺少不了声音,比如角色挂时惨叫一声,或PK时武器交锋的声音,还有就是英雄出场时的背景音乐,无不涉及到声音,本节我们就来看一下pygame中如何控制声音,下面是一个例子,但博客上传不了多媒体程序,否则就可以听到加勒比海盗中最为经典的配乐《he's a pirate》了,程序实现了通过上下方向键来控制音量大小的功能。 ### 一、实例界面: 1、初始音量为10 ![](https://box.kancloud.cn/2016-06-08_5757935d5e2c7.png) 2、通过上下方向键实时调整音乐声音大小: ![](https://box.kancloud.cn/2016-06-08_5757935da9b7e.png) ### 二、实现代码: ~~~ #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os import pygame from pygame.locals import * def load_image(pic_name): ''' Function:图片加载函数 Input:pic_name 图片名称 Output: NONE author: dyx1024 blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #获取当前脚本文件所在目录的绝对路径 current_dir = os.path.split(os.path.abspath(__file__))[0] #指定图片目录 path = os.path.join(current_dir, 'image', pic_name) #加载图片 return pygame.image.load(path).convert() def load_sound(soundfile_name): ''' Function:背景音乐加载函数 Input:pic_name 音乐文件名称 Output: NONE author: dyx1024 blog:http://blog.csdn.net/dyx1024 date:2012-04-22 ''' #获取当前脚本文件所在目录的绝对路径 current_dir = os.path.split(os.path.abspath(__file__))[0] #指定声音目录 path = os.path.join(current_dir, 'sound', soundfile_name) return path def init_windows(): ''' Function:窗口初始化 Input:NONE Output: NONE author: dyx1024 blog:http://blog.csdn.net/dyx1024 date:2012-04-21 ''' pygame.init() display_surface = pygame.display.set_mode((382, 407)) pygame.display.set_caption('游戏中的音乐处理(http://blog.csdn.net/dyx1024)') return display_surface def exit_windows(): ''' Function:退出处理 Input:NONE Output: NONE author: dyx1024 blog:http://blog.csdn.net/dyx1024 date:2012-04-21 ''' pygame.quit() sys.exit() def main(): ''' Function:声音处理 Input:NONE Output: NONE author: dyx1024 blog:http://blog.csdn.net/dyx1024 date:2012-04-22 ''' screen_surface = init_windows() back_image = load_image('lession6_back.jpg') back_music_file = load_sound('he_is_a_pirate.mp3') color_red = (255, 0, 0) color_green = (0, 255, 0) color_blue = (0, 0, 255) music_volume = 10 #文字 fontObj = pygame.font.Font('simkai.ttf', 20) volume_text = u'当前音量:%d' % music_volume textSurfaceObj = fontObj.render(volume_text, True, color_red) textRectObj = textSurfaceObj.get_rect() #加载背景音乐 pygame.mixer.music.load(back_music_file) pygame.mixer.music.set_volume(music_volume/100.0) #循环播放,从音乐第30秒开始 pygame.mixer.music.play(-1, 30.0) while True: #绘图 screen_surface.blit(back_image, (0, 0)) screen_surface.blit(textSurfaceObj, textRectObj) for event in pygame.event.get(): if event.type == QUIT: #停止音乐播放 pygame.mixer.music.stop() exit_windows() if event.type == pygame.KEYDOWN: #通过上向键来控制音量 if event.key == pygame.K_UP: music_volume += 10 if (music_volume > 100): music_volume = 0 if event.key == pygame.K_DOWN: music_volume -= 10 if (music_volume < 0): music_volume = 100 #设置音量 pygame.mixer.music.set_volume(music_volume / 100.0) #显示音量 volume_text = u'当前音量:%d' % music_volume textSurfaceObj = fontObj.render(volume_text, True, color_red) textRectObj = textSurfaceObj.get_rect() pygame.display.update() if __name__ == '__main__': main() ~~~ ### 三、关键知识点介绍:  声音处理使用pygame.mixer.music模块,其提供丰富的方法,如下: ~~~ pygame.mixer.music.load 说明:加载音乐文件 原型:pygame.mixer.music.load(filename): return None pygame.mixer.music.play 说明:播放音乐 原型:pygame.mixer.music.play(loops=0, start=0.0): return None, 其中loops表示循环次数,如设置为-1,表示不停地循环播放;如loops = 5, 则播放5+1=6次,start参数表示从音乐文件的哪一秒开始播放,设置为0表示从开始完整播放 pygame.mixer.music.rewind 说明:重新播放 原型:pygame.mixer.music.rewind(): return None pygame.mixer.music.stop 说明:停止播放 原型:pygame.mixer.music.stop(): return None pygame.mixer.music.pause 说明:暂停 原型pygame.mixer.music.pause(): return None 可通过pygame.mixer.music.unpause恢复播放 pygame.mixer.music.unpause 说明:恢复播放 原型:pygame.mixer.music.unpause(): return None pygame.mixer.music.fadeout 说明:暂停指定的时间,然后接着播放 原型:pygame.mixer.music.fadeout(time): return None, 单位为毫秒 pygame.mixer.music.set_volume 说明:设置音量 原型:pygame.mixer.music.set_volume(value): return None 取值0.0~1.0 pygame.mixer.music.get_volume 说明:获取音量 原型:pygame.mixer.music.get_volume(): return value pygame.mixer.music.get_busy 说明:判断当前是否有音乐在播放 原型:pygame.mixer.music.get_busy(): return bool pygame.mixer.music.get_pos 说明:获取当前播放了多长时间 原型:pygame.mixer.music.get_pos(): return time pygame.mixer.music.queue 说明:将其他音乐文件放入播放队列,当前音乐播放完成后,自动播放队列中其他的音乐文件 pygame.mixer.music.set_endevent 说明:播放完成后的事件通知 原型:pygame.mixer.music.set_endevent(): return None pygame.mixer.music.set_endevent(type): return None pygame.mixer.music.get_endevent 说明:获取播放完成后的事件,如果没有,返回pygame.NOEVENT. 原型:pygame.mixer.music.get_endevent(): return type ~~~