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


i have problem pygame. know how can "on/off" button of song on game .

if event.type == mousebuttondown:    if event.pos[0] > 35 , event.pos[0] < 105 , event.pos[1] > 460 , 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 poor english.

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

instead keep state in variable:

music_playing = true pygame.mixer.music.play()  ... while ...:      events...:          if event.type == mousebuttondown:              if event.pos[0] > 35 , event.pos[0] < 105 , event.pos[1] > 460 , event.pos[1] < 565:                  if music_playing:                     pygame.mixer.music.pause()                    music_playing = false                 else:                    pygame.mixer.music.unpause()                    music_playing = true 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

android - IBM Worklight 6.1 [Application Error] There was a network error (file:///android_asset/www/index.html) -