def test_set_reserved(self): # __doc__ (as of 2008-08-02) for pygame.mixer.set_reserved: # pygame.mixer.set_reserved(count): return count mixer.init() default_num_channels = mixer.get_num_channels() # try reserving all the channels result = mixer.set_reserved(default_num_channels) self.assertEqual(result, default_num_channels) # try reserving all the channels + 1 result = mixer.set_reserved(default_num_channels + 1) # should still be default self.assertEqual(result, default_num_channels) # try unreserving all result = mixer.set_reserved(0) # should still be default self.assertEqual(result, 0) # try reserving half result = mixer.set_reserved(int(default_num_channels / 2)) # should still be default self.assertEqual(result, int(default_num_channels / 2))
def main(): pygame.init() mixer.init() # audio channel allocation: # # 0 - background music # 1 - ship engines # 2 - ship guns # 3 - alien # 4 to 7 - explosions # # we reserve the first four channels for us to allocate and let mixer pick # channels for explosions automatically mixer.set_reserved(4) surface = pygame.display.set_mode([0, 0], pygame.FULLSCREEN) #surface = pygame.display.set_mode([800, 600]) pygame.mouse.set_visible(False) pygame.display.set_caption("Argh, it's the Asteroids!!") game = Game(surface) game.play_game() pygame.quit()
def __load(self, plane_id, color_id): self.group_image = [] self.ammo_image = [] self.ammo_number = [] self.ammo_sounds = [] self.ammo_index = 0 #on ouvre le fichier de specs json et on récupère le dossier des animations de l'avion data = json.load(open(SPEC_DIR, "r"))["body"] path_image = path.join(data[plane_id]["image"], str(color_id)) #on charge des specs... self.speed = data[plane_id]["vitesse"] self.ammo = data[plane_id]["munitions"] self.shoot_delay = self.ammo[0]["frequence_de_tir"] self.max_life = data[plane_id]["vie"] self.actual_life = self.max_life #on paramètre le rectangle d'affichage de la vie avec un rect extérieur pour la bordure noir #et un intérieur de couleur pour l'affichage de la vie self.rect_life = Rect(0 + WIDTH // 4, HEIGHT - 10, WIDTH - WIDTH // 2, 10) self.rect_life_inner = self.rect_life.copy() self.rect_life_inner.left += 1 self.rect_life_inner.top += 1 self.rect_life_inner.width -= 2 self.rect_life_inner.height -= 2 #on génère 100 nuances de couleures entre rouge et vert self.color_scale = list(Color("red").range_to(Color("green"), 100)) #on récupère la première que l'on converti du format (1, 1, 1) à (250, 250, 250) rgb_color = self.color_scale[99].rgb self.actual_color = (int(rgb_color[0] * 250), int(rgb_color[1] * 250), int(rgb_color[2] * 250)) #on réserve de channels de son pour le son de l'avion et celui de ses munitions mixer.set_reserved(PLAYER_CHANNEL) mixer.set_reserved(AMMO_CHANNEL) #on les récupères, on change leur volume et on lance le son de l'avion à l'infini self.ammo_channel = mixer.Channel(AMMO_CHANNEL) self.ammo_channel.set_volume(BULLET_SOUND_PLAYER) self.sound_channel = mixer.Channel(PLAYER_CHANNEL) self.sound_channel.set_volume(SOUND_PLAYER) self.sound_channel.play(mixer.Sound(data[plane_id]["sound"]), -1) #pour chaque images d'animations de l'avion on les charge, on les transforme et on les ajoute à l'array finale for file in os.listdir(path_image): img = image.load(path.join(path_image, file)).convert_alpha() img = transform.scale(img, (100, 75)) self.group_image.append(img) #idem pour chaque munition avec les trois array representant les images, le nombre de munitions et les sons par munitions for file in self.ammo: img = image.load(path.join(file["image"])).convert_alpha() size = (img.get_width() // 3, img.get_height() // 3) img = transform.scale(img, size) img.set_colorkey(BLACK) self.ammo_image.append(img) self.ammo_number.append(file["base_munitions"]) self.ammo_sounds.append(mixer.Sound(file["sound"]))
def __init__(self, screen_size_, channels_: int = 8): """ :param screen_size_: pygame.Rect; Size of the active display :param channels_ : integer; number of channels to reserved for the sound controller :return : None """ if not isinstance(screen_size_, pygame.Rect): raise ValueError( "\n screen_size_ argument must be a pygame.Rect type, got %s " % type(screen_size_)) if not isinstance(channels_, int): raise ValueError( "\n channels_ argument must be a integer type, got %s " % type(channels_)) assert channels_ >= 1, "\nArgument channel_num_ must be >=1" if pygame.mixer.get_init() is None: raise ValueError( "\nMixer has not been initialized." "\nUse pygame.mixer.init() before starting the Sound controller" ) self.channel_num = channels_ # channel to init self.start = mixer.get_num_channels( ) # get the total number of playback channels self.end = self.channel_num + self.start # last channel mixer.set_num_channels( self.end) # sets the number of available channels for the mixer. mixer.set_reserved( self.end) # reserve channels from being automatically used self.channels = [ mixer.Channel(j + self.start) for j in range(self.channel_num) ] # create a channel object for controlling playback self.snd_obj = [None ] * self.channel_num # list of un-initialised objects self.channel = self.start # pointer to the bottom of the stack self.all = list(range( self.start, self.end)) # create a list with all channel number self.screen_size = screen_size_ # size of the display (used for stereo mode)
def __init__(self,game,priority=1): super(SoundController, self).__init__(game,priority) self.logger = logging.getLogger('game.sound') try: self.sounds = {} self.music = {} self.queue = {} self.queue[SNDCHL_MUSIC]=deque() #for queing self.music_track = mixer.Channel(SNDCHL_MUSIC) mixer.set_reserved(SNDCHL_MUSIC) self.queue[SNDCHL_SFX]=deque() #for queing self.sfx_track = mixer.Channel(SNDCHL_SFX) mixer.set_reserved(SNDCHL_SFX) self.queue[SNDCHL_VOICE]=deque() #for queing self.voice_track = mixer.Channel(SNDCHL_VOICE) mixer.set_reserved(SNDCHL_VOICE) # self.end_time[SNDCHL_VOICE] = 0 # self.end_time[SNDCHL_MUSIC] = 0 # self.end_time[SNDCHL_SFX] = 0 except Exception, e: # The import mixer above may work, but init can still fail if mixer is not fully supported. self.enabled = False self.logger.error("pygame mixer init failed; sound will be disabled: "+str(e))
def __init__(self,game,priority=10): super(SoundController, self).__init__(game,priority) self.logger = logging.getLogger('game.sound') try: mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=256) #256 prev mixer.init() mixer.set_num_channels(8) self.queue=deque() #for queing up quotes mixer.set_reserved(CH_MUSIC_1) mixer.set_reserved(CH_MUSIC_2) mixer.set_reserved(CH_VOICE) #mixer.Channel(CH_VOICE).set_endevent(pygame.locals.USEREVENT) -- pygame event queue really needs display and cause a ton of issues, creating own except Exception, e: self.logger.error("pygame mixer init failed; sound will be disabled: "+str(e)) self.enabled = False
def init(self, game): mixer.set_num_channels(8) mixer.set_reserved(2) self.ui_channel = mixer.Channel(0) self.bg_channel = mixer.Channel(1) self.set_bg(self.load_sound("data/sound/background/Puzzle-Game.wav"))
gameOverSound.set_volume(1.0) welcomeSound = mixer.Sound("sounds/welcome.wav") welcomeSound.set_volume(1.0) robotWalkSound = mixer.Sound("sounds/robot_walk.ogg") robotWalkSound.set_volume(.25) playerGunSound = mixer.Sound("sounds/player_gun.ogg") playerGunSound.set_volume(.10) robotGunSound = mixer.Sound("sounds/player_gun.ogg") robotGunSound.set_volume(.01) robotShotSound = mixer.Sound("sounds/robot_shot.wav") robotShotSound.set_volume(.75) bulletClashSound = mixer.Sound("sounds/bullet_clash.ogg") bulletClashSound.set_volume(.10) nextLevelSound = mixer.Sound("sounds/nextlevel.wav") nextLevelSound.set_volume(1.0) ottoAliveSound = mixer.Sound("sounds/otto.wav") ottoAliveSound.set_volume(1.0) soundTrack0Sound = mixer.Sound("sounds/BMUSIC.ogg") soundTrack0Sound.set_volume(0.25) mixer.set_reserved(SOUNDTRACK_CHAN) def playSound(sound): chan = mixer.find_channel(False) # may want to queue sound on a existing channel # but not reserved if a open channel is not found # tbd if (chan != None): chan.play(sound)