Hansi Schmidt
Hansi Schmidt

Reputation: 95

Why Pygame only plays the last sound?

i got this code:

a_0 = pygame.mixer.sound("a.mp3",)
a = pygame.mixer.music

b_0 = pygame.mixer.music.load("b.mp3")
b = pygame.mixer.music

c_0 = pygame.mixer.music.load("c.mp3",)
c   = pygame.mixer.music

d_0 = pygame.mixer.music.load("d.mp3")
d   = pygame.mixer.music

e_0 = pygame.mixer.music.load("e.mp3",)
e = pygame.mixer.music

When i wanna use to play sound d (that emans d.mp3) with:

d.play()

it only plays the last sound of the list, that means e.mp3 instead of d.mp3. What i am doing wrong here?

Upvotes: 0

Views: 101

Answers (1)

The4thIceman
The4thIceman

Reputation: 3889

pygame.music.mixer.load() does not return anything. So you can't assign it to a variable. It loads is into the mixer and prepares it for playback. Documentation

You need to have a list or dictionary of songs you want to play, then whenever you want to play one, you need to load it into the mixer and play it:

def play_song(name):
    pygame.mixer.music.load(name)
    pygame.mixer.music.set_volume(0.3)  # you can also set the volume
    pygame.mixer.music.play(loops=-1)

This is specifically for mixer.music.

pygame.mixer.Sound returns a Sound object which you can save for later: documentation. So you can load a directory of sounds into a python list.

sounds_list = []
sounds_list.append(pygame.mixer.Sound('gun_fire.wav'))
sounds_list.append(pygame.mixer.Sound('coin_pickup.wav'))
# add more sounds

Upvotes: 1

Related Questions