当前位置:编程学习 > 网站相关 >>

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

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

二、实现代码:
 

[python]
#!/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('游戏中的音乐处理
    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 
              

补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,