user3456154
user3456154

Reputation: 3

how to make an "on-off" music button?

I have a problem with pygame. I would like to know how can i do like a "on/off" button of a song on my game .

if event.type == MOUSEBUTTONDOWN: 
  if event.pos[0] > 35 and event.pos[0] < 105 and event.pos[1] > 460 and event.pos[1] < 565: 
    if pygame.mixer.music.play(): 
       pygame.mixer.music.pause() 
    elif pygame.mixer.music.pause():
       pygame.mixer.music.unpause()

Thanks in advance, sorry for my poor english.

Upvotes: 0

Views: 678

Answers (1)

pmoleri
pmoleri

Reputation: 4451

You shouldn't ask for pygame.mixer.music.play() in the if condition, because that's the play function not a state.

Instead keep the state in a variable:

music_playing = True
pygame.mixer.music.play()

...
while ...:

    for events...:

        if event.type == MOUSEBUTTONDOWN: 
            if event.pos[0] > 35 and event.pos[0] < 105 and event.pos[1] > 460 and event.pos[1] < 565: 
                if music_playing: 
                   pygame.mixer.music.pause()
                   music_playing = False
                else:
                   pygame.mixer.music.unpause()
                   music_playing = True

Upvotes: 4

Related Questions