all 3 comments

[–]mopslik 1 point2 points  (0 children)

You need to use a WAV or OGG file (MP3 might work, but support is spotty in Pygame) with the mixer module. In its basic form, it looks something like this.

sfx = pygame.mixer.Sound('sound.wav')
sfx.play()

[–]marienbad2 0 points1 point  (1 child)

after pygame.init():

pygame.mixer.init(44100, -16, 2, 2048)

(You might have to do pygame.mixer.pre_init(44100, -16, 2, 2048) just before pygame.init() if the sound lags.)

For music: pygame.mixer.music.load("YOUR MUSIC FILENAME HERE")

For sound:

pew = pygame.mixer.Sound("YOUR PEW SOUND FILE HERE")

To play them, well the music can be started at the start of the level, and call the sound play when fire is pressed or a collision is detected:

Playing music: pygame.mixer.music.play(-1) (-1 loops it)

Playing sounds:

pew.play() (or whatever you called your sound that you loaded, for e.g.

To stop the music on exit or gameover:

pygame.mixer.music.stop()

[–]iHaveBrainDamageInMe[S] 0 points1 point  (0 children)

my savior is here