mercredi 20 janvier 2021

Pygame: If music is on do this, else do this

I'm currently finishing off making a text based adventure game with a working GUI.

I have a button up top to start a new game. When it is pressed it, the user is sent back to the menu screen. In the menu, the user can select to:

  • Delete Inventory
  • Turn music on or off
  • Change the default background music
  • Resume where they have left off

Here is an image to better explain

My issue is:

When the new game button is pressed and the user enters the menu screen, the button to switch off music is in it's 'Music Off' position, even if music is already off. The user has to press the 'Music Off' button to change the button to 'Music On' and then press the 'Music On' to turn on the music.

I have an idea on how to fix this, but I'm not sure if it is possible or how to code it.

My idea would theoretically go something like this:

#Run this if new game button is pressed
def musicChecker():
    #This checks if the music is on
    if music is true
        #Runs the menu
        menuWithMusicOn()
    else
        #If music is off, it runs an edited version of the code where the music button is switched to on
        menuWithMusicOff()

Is there anyway to check if music is currently on? And would something like this work?

I'm using pygame.mixer.music BTW.

Aucun commentaire:

Enregistrer un commentaire