In Pygame, how can you even play more than one song?

Now it works for me, but with a time delay there is a better way, because I want two different scenarios to work. I want these games to be in that order, and my images look in order, and the images are long script and have time delays on them too.

#!/usr/bin/env python import pygame pygame.mixer.init() pygame.mixer.pre_init(44100, -16, 2, 2048) pygame.init() print "hey I finaly got this working!" sounda= pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG') soundb= pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG') soundc= pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG') soundd= pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG') sounda.play() pygame.time.delay(11000) soundb.play()<P> pygame.time.delay(180000) soundc.play() pygame.time.delay(90000) soundd.play() 
+6
source share
2 answers

Have you checked the pygame.Mixer module? By default, you can play 8 songs at a time.

If you use pygame.mixer.music , you can only play one song at a time.

If you use pygame.mixer.sound , you can play up to 8 tracks at a time.

The music module is designed for streaming music (it does not download the entire music file at once).

The sound module here plays various sounds during the game (sounds are fully loaded into memory).

So, in your example, if you want to play 4 songs at the same time:

 import pygame pygame.mixer.init() pygame.mixer.pre_init(44100, -16, 2, 2048) pygame.init() print "hey I finaly got this working!" sounds = [] sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG')) sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG')) sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG')) sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG')) for sound in sounds: sound.play() 
+7
source

The following script will load 4 sounds (sound_0.wav in sound_3.wav) and play them.

 sounds = [] for i in range(4): sound = pygame.mixer.Sound('sound_%d.wav'%i) sound.play() sounds.append(sound) 
0
source

Source: https://habr.com/ru/post/888129/


All Articles