1

I'm making a pygame game and I'm trying to add background music. It all works until I click on the space button, which causes annother sound effect (shooting sound), but it stops the background music and I don't know why and how to fix this.
pygame.mixer.music.load(background_music) pygame.mixer.music.play(-1,0.0)
and That is what the sound effect when pressing space looks like (it is inside the main loop):
if keys[pygame.K_SPACE]: if (len(bullets) < 4 and shootLoop == 0 and not (ship.isBolt)) or (len(bullets) < 8 and shootLoop == 0 and ship.isBolt): if not (ship.isBolt): bullets.add(Projectile(round(ship.rect.x + ship.width / 2 - 5), ship.rect.y - ship.height - 8)) else: bullets.add(Projectile(ship.rect.x + ship.width - 11, ship.rect.y - ship.height + 8)) bullets.add(Projectile(ship.rect.x - 4, ship.rect.y - ship.height + 8)) pygame.mixer.music.load(SOUND_SHOT) pygame.mixer.music.play()
This is the code I used to play the background music
How do I fix this?

Omer
  • 223
  • 1
  • 8

2 Answers2

0

You can use it like this:

pygame.mixer.music.load("background_music")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
0
pygame.mixer.music.load(os.path.join(os.getcwd(), 'sound', 'back-ground-music'))
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(loops=-1)  

pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\effects.wav'), maxtime=600)

Use the channels iff you want another effect sound use the pygame.mixer.Channel(1) #for example Can u try this