本文介绍了Python-Pygame-获取是否正在播放特定声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Pygame's mixer module has pygame.mixer.get_busy which returns a simple boolean.
My problem is that I have sound playing constantly, like explosions and gunshots, and need to know when a specific sound is playing to prevent game dialogue from overlapping.

我考虑列出当前正在播放的对话,创建一个计时器,该计时器会在触发每种声音时进行递减计数,但这需要我在主游戏循环中添加声音效果(处理声音的模块). br>这似乎很混乱,并且就像一个巨大的减速.

I considered making a list of currently playing dialogue, creating a timer that counts down as each sound is triggered,but that would require me to add an sound Effects (my module that handles sounds) update in the main game loop.
This seems messy, and like a giant slowdown.

是否有一种更清洁的方法?

Is there a cleaner way to do this?

推荐答案

您可以使用通道对象.

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

Play a specific type of audio in a channel and check to see if the sound is playing.

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-获取是否正在播放特定声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:00