Part 18: Add some music and add some spice!

Now for some music! Feel it! No pictures here and in the free version no pictures allowed.

Before the while loop, add

#Play music infintely till the game runs
MUSICFILE = "background_music.mp3"
INFINITE = -1
pygame.mixer.music.load(MUSICFILE)
pygame.mixer.music.play(INFINITE)

 

The code loads the music file and runs it again and again in a loop, till we stop it.
In the game over part of the code.

    GAMEOVERMUSIC = "gameover.mp3"
    PLAYONCE = 0
    if(len(wall_group.sprites()) == 0):
        print("game over")
        pygame.mixer.music.stop()
        pygame.mixer.music.load(GAMEOVERMUSIC)
        pygame.mixer.music.play(PLAYONCE)

 

The code stops the background music, loads the game over music and plays it only once this time (PLAYONCE = 0).

Now, what if we need to add levels to our game, by making the wall structure more difficult to break?
Ready for the last challenge?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s