1

I've written a game that includes two files of python, one for the main menu and the other for the main game. Both windows (main menu and the game itself) have a "mute sounds" button, which mutes the soundtrack. However, when the user mutes the sound and switches to another window, the sound starts playing again. For example, if the user mutes sounds in the game window, by returning to the main menu, the menu soundtrack starts playing. This might be annoying for a user that expects all sounds muted no matter to which window they switch. How can I fix this problem?

Assuming I have the main menu python file like this:

#menu.py

def main_menu():
        pygame.mixer.init()
        bg_music = pygame.mixer.music.load('/home/liana/Downloads/Pêtr Aleksänder - Triptych 1 for Piano (320).wav')
        pygame.mixer.music.play(-1)

        def stuff():
                #some code here corresponding to the menu design

        if mute_button_collide.collidepoint((mx, my)):
                if click:
                        click_cnt += 1
                        if click_cnt % 2 == 0:
                                pygame.mixer.init()
                                bg_music = pygame.mixer.music.load('/home/liana/Downloads/Pêtr Aleksänder - Triptych 1 for Piano (320).wav')

                                pygame.mixer.music.play(-1)

                        else:
                                pygame.mixer.quit()

        if play_button_collide.collidepoint((mx, my)):
                if click:
                        game()

I have done the same thing for my game.py file, only that if the user presses the 'back' button, the menu function will be called and the user will be taken back to the main menu.

Any help is appreciated in advance.

Melika
  • 99
  • 7
  • 1
    you need to have some variable like `muted = False` in either of the files and import it in the other, when the button is pressed, it should mute the sounds and `muted = True`, playing sound in both files should be allowed only `if not muted:`, then play sound – Matiiss Aug 29 '21 at 10:12

0 Answers0