2

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

我考虑过列出当前正在播放的对话,创建一个计时器,在触发每个声音时倒计时,但这需要我在主游戏循环中添加声音效果(我处理声音的模块)更新。
这看起来很混乱,就像一个巨大的减速。

有没有更清洁的方法来做到这一点?

4

1 回答 1

7

您可以使用通道对象。

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

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
于 2013-01-21T07:44:45.540 回答