Pygame 的混合器模块有 pygame.mixer.get_busy
返回一个简单的 bool 值。
我的问题是我不断播放声音,例如爆炸声和枪声,需要知道何时播放特定声音以防止游戏对话重叠。
我考虑过制作一个当前正在播放的对话列表,创建一个计时器,在每个声音被触发时倒计时,但这需要我在主游戏循环中添加音效(我的处理声音的模块)更新。< br/> 这看起来很困惑,就像一个巨大的减速。
有没有更简洁的方法来做到这一点?
最佳答案
您可以使用 channel 对象。
在 channel 中播放特定类型的音频并检查声音是否正在播放。
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
关于Python - Pygame - 获取特定声音是否正在播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432851/