2013-01-21 141 views
2

Pygame的混音器模块有pygame.mixer.get_busy,它返回一个简单的布尔值。
我的问题是,我经常播放声音,比如爆炸声和枪声,并且需要知道特定声音何时播放以防止游戏对话重叠。Python - Pygame - 获取特定声音播放

我考虑制作一个当前正在播放的对话列表,创建一个计时器,当每个声音都被触发时倒计时,但这需要我在主游戏循环中添加一个声音效果(处理声音的模块)更新。
这似乎凌乱,并像一个巨大的放缓。

有没有更干净的方法来做到这一点?

回答

5

您可以使用通道对象。

在通道中播放特定类型的音频并检查声音是否正在播放。

import pygame 

def main(filepath): 
    pygame.mixer.init() 

    # If you want more channels, change 8 to a desired number. 8 is the default number of channel 

    pygame.mixer.set_num_channels(8) 

    # This is the sound channel 
    voice = pygame.mixer.Channel(5) 

    sound = pygame.mixer.Sound(filepath) 

    voice.play(sound) 

    if voice.get_busy(): 
     #Do Something