Reputation: 99
I've tried to add background music but no sound comes out. I can think of 3 possible reasons but I don't know how to fix it.
First possible reason: I've placed the file at the wrong place on my computer (I placed it in my Windows(C:) under Users because that's where my pygame file is)
Second possible reason: I've placed the code at the wrong place. (I placed it under the main loop which I don't see others doing but I also have a start screen and I don't want the same music for the start screen and actual game.)
Third possible reason: The code is wrong/incomplete. (I only have three lines from a code I saw here but sometimes)
pygame.init()
pygame.mixer.init()
while run:
pygame.mixer.music.load('bgm.mp3')
pygame.mixer.music.play()
This is my entire code:
Code is removed for now. Will re-upload in 1 to 2 months.
Please also guide me on how I can add three different bgm. 1 for my start screen and 1 for my actual game and 1 for my end screen.
The start and end screen should have the same bgm but i'm not sure on how to transition it.
I have yet to add the end screen because I don't know how to but, for context, when enemy sprite touches player sprite, its game over. For now, when enemy sprite touches player sprite, the game just close as in run = False
.
Upvotes: 1
Views: 582
Reputation: 210948
The mp3 file is continuously laded and restarted in the main loop. If you want to play the background music continuously, then you've to start and restart the music only in cases when the music is not playing.
Check if the music stream is playing by pygame.mixer.music.get_busy()
. e.g.:
run = True
while run:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.load('bgm.mp3')
pygame.mixer.music.play()
Upvotes: 2