<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??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 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看