Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pause, unpause music pygame on KEYDOWN

Sorry if silly question.

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
I’m pretty sure I’m doing something wrong.
Can someone please point me in the right direction?

Thanks in advance,

>Solution :

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()
                
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading