Pygame搅拌机声音叠加

发布于 2025-02-13 03:17:18 字数 642 浏览 1 评论 0原文

当按下箭头键时,我正在尝试使用混音器来播放脚步。但是,每当我这样做时,显然都会不断检测到按钮并不断覆盖声音。

def walking():
    walk = pygame.mixer.Channel(2)
    walk_sound = mixer.Sound("Music/Walking.mp3")
    walk.play(walk_sound)
    if walk.get_busy():
        # Don't play anything

这是步行声的功能。如果有声音播放,有没有办法不玩任何东西?

if keypress[K_RIGHT]:
            self.rect.move_ip(x, 0)
            self.image = walk_right[walk_count // frame_count]
            display.blit(self.image, (self.rect.x, self.rect.y))
            walk_count += 1
            self.last = "right"
            walking()

这是我的播放器课程的一部分,也是移动方法,以显示我称之为步行声功能的位置。

I'm trying to use mixer to play footsteps when the arrow keys are pressed. But whenever I do it obviously keeps detecting the button and continuously overlays the sound.

def walking():
    walk = pygame.mixer.Channel(2)
    walk_sound = mixer.Sound("Music/Walking.mp3")
    walk.play(walk_sound)
    if walk.get_busy():
        # Don't play anything

This is the function for the walking sound. Is there a way to not play anything if there is a sound playing?

if keypress[K_RIGHT]:
            self.rect.move_ip(x, 0)
            self.image = walk_right[walk_count // frame_count]
            display.blit(self.image, (self.rect.x, self.rect.y))
            walk_count += 1
            self.last = "right"
            walking()

This is part of my Player class and Move method to show where I call the walking sound function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2025-02-20 03:17:18

创建通道并在初始化时循环循环之前加载声音。只需在Walling函数中启动声音,如果频道不是忙>

walk_channel = pygame.mixer.Channel(2)
walk_sound = mixer.Sound("Music/Walking.mp3")
def walking():
    if not walk_channel.get_busy():
        walk_channel.play(walk_sound)

Create the channel and load the sound before the application loop at initialisation. Just start the sound in the walking function if the channel is not busy:

walk_channel = pygame.mixer.Channel(2)
walk_sound = mixer.Sound("Music/Walking.mp3")
def walking():
    if not walk_channel.get_busy():
        walk_channel.play(walk_sound)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文