一款人性化的游戲中缺少不了聲音,比如角色掛時慘叫一聲,或PK時武器交鋒的聲音,還有就是英雄出場時的背景音樂,無不涉及到聲音,本節我們就來看一下pygame中如何控制聲音,下面是一個例子,但博客上傳不了多媒體程序,否則就可以聽到加勒比海盜中最為經典的配樂《he's a pirate》了,程序實現了通過上下方向鍵來控制音量大小的功能。
### 一、實例界面:
1、初始音量為10

2、通過上下方向鍵實時調整音樂聲音大小:

### 二、實現代碼:
~~~
#!/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
~~~
- 前言
- Python:實現文件歸檔
- Pyhon:按行輸出文件內容
- Python:讀文件和寫文件
- Python:實現一個小算法
- Python:通過命令行發送新浪微博
- Python:通過攝像頭實現的監控功能
- Python:通過攝像頭抓取圖像并自動上傳至新浪微博
- Python:簡單的攝像頭程序實現
- Python:日志模塊logging的應用
- Python:操作嵌入式數據庫SQLite
- Python:將句子中的單詞全部倒排過來,但單詞的字母順序不變
- Python:語音處理,實現在線朗讀RFC文檔或本地文本文件
- Python:通過計算階乘來學習lambda和reduce這兩個函數的使用
- Python:通過執行100萬次打印來比較C和python的性能,以及用C和python結合來解決性能問題的方法
- Python:使用matplotlib繪制圖表
- Python:使用pycha快速繪制辦公常用圖(餅圖、垂直直方圖、水平直方圖、散點圖等七種圖形)
- Python:使用pycha快速繪制辦公常用圖二(使用樣式定制個性化圖表)
- Python:監控鍵盤輸入、鼠標操作,并將捕獲到的信息記錄到文件中
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性(二)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(一)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(二)
- Python:通過自定義系統級快捷鍵來控制程序運行
- Python:通過自定義系統級快捷鍵來控制程序開始或停止記錄日志(使用小技巧解決一個貌似無解的問題)
- Python:一個多功能的抓圖工具開發(附源碼)
- Python:程序發布方式簡介一(打包為可執行文件EXE)
- Python:新浪微博應用開發簡介(認證及授權部分)
- Python:程序最小化到托盤功能實現
- Python:實用抓圖工具開發介紹(含需求分析、設計、編碼、單元測試、打包、系統測試、發布各環節)
- Python:桌面氣泡提示功能實現
- Python:未來三個月的python學習計劃
- Python:pygame模塊及SDL庫簡介
- Python:獲取新浪微博用戶的收聽列表和粉絲列表
- Python:pygame游戲編程之旅一(Hello World)
- Python:pygame游戲編程之旅二(自由移動的小球)
- Python:pygame游戲編程之旅三(玩家控制的小球)
- Python:pygame游戲編程之旅四(游戲界面文字處理)
- Python:pygame游戲編程之旅五(游戲界面文字處理詳解)
- Python:pygame游戲編程之旅六(游戲中的聲音處理)
- Python:pygame游戲編程之旅七(pygame基礎知識講解1)
- Python:編程“八榮八恥”之我見
- Python:腳本的幾種執行方式
- wxPython:簡單的wxPython程序
- wxPython:簡單的wxPython程序的另一種寫法
- wxPython:應用程序對象介紹
- wxPython:輸出重定向
- wxPython:關閉wxPython程序
- wxPython:Frame類介紹
- wxPython:面板Panel的使用
- wxPython:工具欄、狀態欄、菜單實現
- wxPython:消息對話框MessageDialog
- wxPython:文本對話框TextEntryDialog
- wxPython:列表選擇框SingleChoiceDialog
- wxPython:事件處理介紹一
- wxPython:事件處理介紹二
- wxPython: 簡單的繪圖例子
- wxPython:狀態欄介紹
- wxPython:菜單介紹
- wxPython:文件對話框wx.FileDialog
- wxPython:顏色選擇對話框wx.ColourDialog
- wxPython:布局管理器sizer介紹
- wxPython:啟動畫面SplashScreen介紹
- wxPython:繪畫按鈕BitmapButton介紹
- wxPython:進度條Gauge介紹
- Python: 發送新浪微博(使用oauth2)
- Python:讀取新浪微博收聽列表
- Python:DNS客戶端實現