'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()
?
Have tried adding this before the continue
but it does not work:
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. Can someone please point me in the right direction?
Solution 1:[1]
Call mixer.music.pause()
or mixer.music.unpause()
depending on paused
:
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rabbid76 |