Pygame setting volume when target is a list

Viewed 74

I'm in the middle of making a game and i have these explosions(expl) in a list so it isn't the same sound every time. I'm using a random.choice to "control" the output

expl_sound = []
for snd in ['expl.wav', 'expl1.wav', "expl2.wav"]:
    expl_sound.append(pygame.mixer.Sound(path.join(snd_folder, snd)))

But when i go to set the volume for that soundlist it spits this error message out

 Traceback (most recent call last):

  File "C:\Users\wep43\Desktop\Python Games\Pygames Tutorial\Pygame Shmup.py", line 311, in <module>
    expl_sound.set_volume(0.1)

AttributeError: 'list' object has no attribute 'set_volume'

I can't for the life of me make it not spit out this message.

I hope is makes sense what my problem is, otherwise just ask for more info

1 Answers

set_volume is an instance method of the class pygame.mixer.Sound, but expl_sound is a list of pygame.mixer.Sound objects. Hence, you have to set the volume separately for each (Sound) element in the list:

for snd in expl_sound:
    snd.set_volume(0.1)
Related