kip
kip

Reputation: 145

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?

Upvotes: 1

Views: 249

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

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()
                

Upvotes: 2

Related Questions