Python:pygame游戏编程之旅六(游戏中的声音处理)

来源:互联网 发布:配置管理数据库 cmdb 编辑:程序博客网 时间:2024/05/29 16:28

      一款人性化的游戏中缺少不了声音,比如角色挂时惨叫一声,或PK时武器交锋的声音,还有就是英雄出场时的背景音乐,无不涉及到声音,本节我们就来看一下pygame中如何控制声音,下面是一个例子,但博客上传不了多媒体程序,否则就可以听到加勒比海盗中最为经典的配乐《he's a pirate》了,程序实现了通过上下方向键来控制音量大小的功能。

一、实例界面:

1、初始音量为10

 

2、通过上下方向键实时调整音乐声音大小:


二、实现代码:

#!/usr/bin/env python# -*- coding: utf-8 -*-import sysimport osimport pygamefrom 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 pathdef 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_surfacedef 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 Nonepygame.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 Nonepygame.mixer.music.stop说明:停止播放原型:pygame.mixer.music.stop(): return Nonepygame.mixer.music.pause说明:暂停原型pygame.mixer.music.pause(): return None可通过pygame.mixer.music.unpause恢复播放pygame.mixer.music.unpause说明:恢复播放原型:pygame.mixer.music.unpause(): return Nonepygame.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.0pygame.mixer.music.get_volume 说明:获取音量原型:pygame.mixer.music.get_volume(): return valuepygame.mixer.music.get_busy说明:判断当前是否有音乐在播放原型:pygame.mixer.music.get_busy(): return boolpygame.mixer.music.get_pos说明:获取当前播放了多长时间原型:pygame.mixer.music.get_pos(): return timepygame.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