简体   繁体   English

在 KEYDOWN 上的 PyGame 中暂停、取消暂停音乐

[英]Pause, unpause music in PyGame on KEYDOWN

I've created a game loop that pauses if the spacebar is pressed.我创建了一个游戏循环,如果按下空格键就会暂停。 The loop is pausing and unpausing as expected but the music playing in the background does not restart when clicking spacebar, only the game itself.循环按预期暂停和取消暂停,但单击空格键时后台播放的音乐不会重新开始,只有游戏本身。

My code looks like this:我的代码如下所示:

paused = False

while loop:

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                mixer.music.pause()
                paused = not paused
if paused == True:
    continue

The game itself pauses and unpauses as expected when pressing spacebar.按空格键时,游戏本身会按预期暂停和取消暂停。

How do I also catch the mixer.music.unpause() ?我如何捕捉mixer.music.unpause()

Have tried adding this before the continue but it does not work:continue之前尝试添加它,但它不起作用:

if mixer.music.pause() == True:
    mixer.music.unpause()
    continue

Also tried creating and calling a function that checks if mixer.music.pause() is True , but am unable to get it to work.还尝试创建并调用一个 function 检查mixer.music.pause()是否为True ,但我无法让它工作。 Can someone please point me in the right direction?有人可以指出我正确的方向吗?

Call mixer.music.pause() or mixer.music.unpause() depending on paused :根据paused调用mixer.music.pause()mixer.music.unpause()

while loop:

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                paused = not paused
                if paused:
                    mixer.music.pause()
                else
                    mixer.music.unpause()
                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM