内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

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

时间:2015-02-22 23:43:16 来源: 作者: 人气: 评论:

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

一、实例界面:

1、初始音量为10

 [URL]3e15e1527ccd3a76d7a2754e9e44a56b.png

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

[URL]f029c2b7ff9b46bb076c86c234fe1676.png

二、实现代码:

 
  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3.   
  4. import sys  
  5. import os  
  6. import pygame  
  7. from pygame.locals import *  
  8.   
  9. def load_image(pic_name):  
  10.     ''''' 
  11.     Function:图片加载函数 
  12.     Input:pic_name 图片名称 
  13.     Output: NONE 
  14.     author: dyx1024 
  15.     blog:http://blog.csdn.net/dyx1024 
  16.     date:2012-04-15 
  17.     '''  
  18.     #获取当前脚本文件所在目录的绝对路径  
  19.     current_dir = os.path.split(os.path.abspath(__file__))[0]  
  20.       
  21.     #指定图片目录  
  22.     path = os.path.join(current_dir, 'image', pic_name)  
  23.       
  24.     #加载图片  
  25.     return pygame.image.load(path).convert()  
  26.   
  27. def load_sound(soundfile_name):  
  28.     ''''' 
  29.     Function:背景音乐加载函数 
  30.     Input:pic_name 音乐文件名称 
  31.     Output: NONE 
  32.     author: dyx1024 
  33.     blog:http://blog.csdn.net/dyx1024 
  34.     date:2012-04-22 
  35.     '''  
  36.     #获取当前脚本文件所在目录的绝对路径  
  37.     current_dir = os.path.split(os.path.abspath(__file__))[0]  
  38.       
  39.     #指定声音目录  
  40.     path = os.path.join(current_dir, 'sound', soundfile_name)  
  41.       
  42.     return path  
  43.   
  44. def init_windows():  
  45.     ''''' 
  46.     Function:窗口初始化 
  47.     Input:NONE 
  48.     Output: NONE 
  49.     author: dyx1024 
  50.     blog:http://blog.csdn.net/dyx1024 
  51.     date:2012-04-21 
  52.     '''      
  53.     pygame.init()  
  54.     display_surface = pygame.display.set_mode((382407))  
  55.     pygame.display.set_caption('游戏中的音乐处理(http://blog.csdn.net/dyx1024)')  
  56.     return display_surface  
  57.   
  58. def exit_windows():  
  59.     ''''' 
  60.     Function:退出处理 
  61.     Input:NONE 
  62.     Output: NONE 
  63.     author: dyx1024 
  64.     blog:http://blog.csdn.net/dyx1024 
  65.     date:2012-04-21 
  66.     '''        
  67.     pygame.quit()  
  68.     sys.exit()  
  69.   
  70. def main():  
  71.     ''''' 
  72.     Function:声音处理 
  73.     Input:NONE 
  74.     Output: NONE 
  75.     author: dyx1024 
  76.     blog:http://blog.csdn.net/dyx1024 
  77.     date:2012-04-22 
  78.     '''          
  79.       
  80.     screen_surface = init_windows()  
  81.     back_image = load_image('lession6_back.jpg')  
  82.     back_music_file = load_sound('he_is_a_pirate.mp3')  
  83.         
  84.     color_red = (25500)  
  85.     color_green = (02550)  
  86.     color_blue  = (00255)  
  87.   
  88.     music_volume = 10  
  89.       
  90.     #文字  
  91.     fontObj = pygame.font.Font('simkai.ttf'20)  
  92.     volume_text = u'当前音量:%d' % music_volume  
  93.     textSurfaceObj = fontObj.render(volume_text, True, color_red)  
  94.     textRectObj = textSurfaceObj.get_rect()     
  95.       
  96.     #加载背景音乐  
  97.     pygame.mixer.music.load(back_music_file)  
  98.     pygame.mixer.music.set_volume(music_volume/100.0)  
  99.       
  100.     #循环播放,从音乐第30秒开始  
  101.     pygame.mixer.music.play(-130.0)  
  102.       
  103.     while True:  
  104.         #绘图  
  105.         screen_surface.blit(back_image, (00))  
  106.         screen_surface.blit(textSurfaceObj, textRectObj)  
  107.         for event in pygame.event.get():  
  108.             if event.type == QUIT:  
  109.                   
  110.                 #停止音乐播放  
  111.                 pygame.mixer.music.stop()  
  112.                 exit_windows()  
  113.                   
  114.             if event.type == pygame.KEYDOWN:  
  115.                 #通过上向键来控制音量      
  116.                 if event.key == pygame.K_UP:  
  117.                     music_volume += 10  
  118.                     if (music_volume > 100):  
  119.                         music_volume = 0  
  120.                 if event.key == pygame.K_DOWN:  
  121.                     music_volume -= 10  
  122.                     if (music_volume < 0):  
  123.                         music_volume = 100      
  124.                           
  125.                  #设置音量             
  126.                 pygame.mixer.music.set_volume(music_volume / 100.0)  
  127.                   
  128.         #显示音量  
  129.         volume_text = u'当前音量:%d' % music_volume  
  130.         textSurfaceObj = fontObj.render(volume_text, True, color_red)  
  131.         textRectObj = textSurfaceObj.get_rect()       
  132.            
  133.         pygame.display.update()  
  134.   
  135. if __name__ == '__main__':  
  136.     main()  

三、关键知识点介绍:

   声音处理使用pygame.mixer.music模块,其提供丰富的方法,如下:

 
  1. pygame.mixer.music.load  
  2. 说明:加载音乐文件  
  3. 原型:pygame.mixer.music.load(filename): return None  
  4.   
  5. pygame.mixer.music.play  
  6. 说明:播放音乐  
  7. 原型:pygame.mixer.music.play(loops=0, start=0.0): return None,  
  8. 其中loops表示循环次数,如设置为-1,表示不停地循环播放;如loops = 5,  
  9. 则播放5+1=6次,start参数表示从音乐文件的哪一秒开始播放,设置为0表示从开始完整播放  
  10.   
  11. pygame.mixer.music.rewind  
  12. 说明:重新播放  
  13. 原型:pygame.mixer.music.rewind(): return None  
  14.   
  15. pygame.mixer.music.stop  
  16. 说明:停止播放  
  17. 原型:pygame.mixer.music.stop(): return None  
  18.   
  19. pygame.mixer.music.pause  
  20. 说明:暂停  
  21. 原型pygame.mixer.music.pause(): return None  
  22. 可通过pygame.mixer.music.unpause恢复播放  
  23.   
  24. pygame.mixer.music.unpause  
  25. 说明:恢复播放  
  26. 原型:pygame.mixer.music.unpause(): return None  
  27.   
  28. pygame.mixer.music.fadeout  
  29. 说明:暂停指定的时间,然后接着播放  
  30. 原型:pygame.mixer.music.fadeout(time): return None,  
  31. 单位为毫秒  
  32.   
  33. pygame.mixer.music.set_volume  
  34. 说明:设置音量  
  35. 原型:pygame.mixer.music.set_volume(value): return None  
  36. 取值0.0~1.0  
  37.   
  38. pygame.mixer.music.get_volume   
  39. 说明:获取音量  
  40. 原型:pygame.mixer.music.get_volume(): return value  
  41.   
  42. pygame.mixer.music.get_busy  
  43. 说明:判断当前是否有音乐在播放  
  44. 原型:pygame.mixer.music.get_busy(): return bool  
  45.   
  46. pygame.mixer.music.get_pos  
  47. 说明:获取当前播放了多长时间  
  48. 原型:pygame.mixer.music.get_pos(): return time  
  49.   
  50. pygame.mixer.music.queue  
  51. 说明:将其他音乐文件放入播放队列,当前音乐播放完成后,自动播放队列中其他的音乐文件  
  52.   
  53. pygame.mixer.music.set_endevent  
  54. 说明:播放完成后的事件通知  
  55. 原型:pygame.mixer.music.set_endevent(): return None  
  56.       pygame.mixer.music.set_endevent(type): return None  
  57.         
  58. pygame.mixer.music.get_endevent  
  59. 说明:获取播放完成后的事件,如果没有,返回pygame.NOEVENT.  
  60. 原型:pygame.mixer.music.get_endevent(): return type  
  61.  
分享给小伙伴们:
本文标签:Python游戏  

相关文章

    无相关信息

发表评论共有条评论

友情链接1 百悠游站点 |

友情链接2: 帝国CMS |

百悠游站点,电脑学习,游戏网赚分享,游戏工作室门户网站。

Copyright (C) buyzd.cn, All Rights Reserved.

百悠游站点 版权所有