如何在后台运行pygame?

发布于 2025-01-18 11:53:49 字数 283 浏览 4 评论 0原文

我制作了一个小型 pygame 应用程序,它使用 pygame.mixer 在按键中播放某些 wav 文件 一切似乎都工作得很好,除了以下事实:如果您最小化 pygame 窗口,程序将停止工作,直到您再次打开它。有没有办法解决这个问题或者用Python实现声音播放的替代方法?

这是我的存储库: https://github.com/Souvlaki42/HighPlayer

I made a small pygame app that plays certain wav files in keypress using pygame.mixer
All seem to work just fine except the fact that if you minimize the pygame window the program stops working until you open it again. Is there a way to solve this issue or an alternative way to implement sound playing in python?

This is my repository: https://github.com/Souvlaki42/HighPlayer

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

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

发布评论

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

评论(2

盗心人 2025-01-25 11:53:49

尝试使用 pygame.mixer.music() ,但您需要将所有 wav 文件转换为 mp3 文件。

Try using pygame.mixer.music() but you'll need to convert all your wav files into mp3 files.

谁许谁一生繁华 2025-01-25 11:53:49

只需修补我从第34行的所有错误修补我的所有错误,现在应该是:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    event2 = keyboard.read_event()
    if event2.name == pygame.K_0:
        stopSounds()
        sound0.play()
    if event2.name == pygame.K_1:
        stopSounds()
        sound1.play()
    if event2.name == pygame.K_2:
        stopSounds()
        sound2.play()
    if event2.name == pygame.K_3:
        stopSounds()
        sound3.play()
    if event2.name == pygame.K_4:
        stopSounds()
        sound4.play()
    if event2.name == pygame.K_5:
        stopSounds()
        sound5.play()
    if event2.name == pygame.K_6:
        stopSounds()
        sound6.play()
    if event2.name == pygame.K_7:
        stopSounds()
        sound7.play()
    if event2.name == pygame.K_8:
        stopSounds()
        sound8.play()
    if event2.name == pygame.K_9:
        stopSounds()
        sound9.play()

Just patch up all my mistakes your code from line 34 onwards should now be:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    event2 = keyboard.read_event()
    if event2.name == pygame.K_0:
        stopSounds()
        sound0.play()
    if event2.name == pygame.K_1:
        stopSounds()
        sound1.play()
    if event2.name == pygame.K_2:
        stopSounds()
        sound2.play()
    if event2.name == pygame.K_3:
        stopSounds()
        sound3.play()
    if event2.name == pygame.K_4:
        stopSounds()
        sound4.play()
    if event2.name == pygame.K_5:
        stopSounds()
        sound5.play()
    if event2.name == pygame.K_6:
        stopSounds()
        sound6.play()
    if event2.name == pygame.K_7:
        stopSounds()
        sound7.play()
    if event2.name == pygame.K_8:
        stopSounds()
        sound8.play()
    if event2.name == pygame.K_9:
        stopSounds()
        sound9.play()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文