def flashButtonAnimation(color, animationSpeed=50): if color == YELLOW: sound = BEEP1 flashColor = BRIGHTYELLOW rectangle = YELLOWRECT elif color == BLUE: sound = BEEP2 flashColor = BRIGHTBLUE rectangle = BLUERECT elif color == RED: sound = BEEP3 flashColor = BRIGHTRED rectangle = REDRECT elif color == GREEN: sound = BEEP4 flashColor = BRIGHTGREEN rectangle = GREENRECT r, g, b, a = flashColor channel = sdlmixer.Mix_PlayChannel(-1, sound, 0) for start, end, step in ((0, 255, 1), (255, 0, -1)): # animation loop for alpha in range(start, end, animationSpeed * step): handle_events() REN.fill(rectangle, color) REN.fill(rectangle, (r,g,b,alpha)) REN.present() sdl2.SDL_Delay(1000//FPS) REN.fill(rectangle, color) REN.present()
def play(self, loops): channel = sdlmixer.Mix_PlayChannel(channel=-1, chunk=self.sample, loops=loops) if channel == -1: raise RuntimeError("Cannot play sample: {}".format( sdlmixer.Mix_GetError())) return channel
def play(self): while (True): yield if self.model.state[0] == 'lineclear': sdlmixer.Mix_PlayChannel(-1, self.erasefx, 0) while (self.model.state[0] != 'are'): yield sdlmixer.Mix_PlayChannel(-1, self.dropfx, 0) while (self.model.state[0] == 'are'): yield if self.model.state[0] == 'are': sdlmixer.Mix_PlayChannel(-1, self.lockfx, 0) while (self.model.state[0] == 'are'): yield if self.model.state[0] == 'spawn': sdlmixer.Mix_PlayChannel(-1, self.blockfx[self.model.next[1]], 0) continue
def gameOverAnimation(color=WHITE, animationSpeed=50): # play all beeps at once, then flash the background # play all four beeps at the same time, roughly. channel = sdlmixer.Mix_PlayChannel(-1, BEEP1, 0) channel = sdlmixer.Mix_PlayChannel(-1, BEEP2, 0) channel = sdlmixer.Mix_PlayChannel(-1, BEEP3, 0) channel = sdlmixer.Mix_PlayChannel(-1, BEEP4, 0) r, g, b, a = color for i in range(3): # do the flash 3 times for start, end, step in ((0, 255, 1), (255, 0, -1)): # The first iteration in this loop sets the following for loop # to go from 0 to 255, the second from 255 to 0. for alpha in range(start, end, animationSpeed * step): # animation loop # alpha means transpaRENcy. 255 is opaque, 0 is invisible handle_events() REN.fill((0, 0, WINDOWWIDTH, WINDOWHEIGHT), (r,g,b,alpha)) drawButtons() REN.present() sdl2.SDL_Delay(1000//FPS)
def object_play_sound(self, game_object, sound_filename, loops=0, allow_multiple=False): # TODO: volume param? sdlmixer.MIX_MAX_VOLUME if not specified # bail if same object isn't allowed to play same sound multiple times if not allow_multiple and sound_filename in self.playing_sounds: for playing_sound in self.playing_sounds[sound_filename]: if playing_sound.go is game_object: return sound = self.register_sound(sound_filename) channel = sdlmixer.Mix_PlayChannel(-1, sound, loops) # add sound to dicts of playing sounds and channels new_playing_sound = PlayingSound(sound_filename, channel, game_object, loops == -1) if sound_filename in self.playing_sounds: self.playing_sounds[sound_filename].append(new_playing_sound) else: self.playing_sounds[sound_filename] = [new_playing_sound] self.playing_channels[channel] = new_playing_sound
def play(self, volume=1.0, repeat=1): ''' Play the sound *Parameters:* - `volume`: Sound volume 0.0 to 1.0 - `repeat`: Number of time to repeat the sound, 0=infinity *Returns:* Channel id of the sound ''' channel = mixer.Mix_PlayChannel(-1, self.sample, repeat-1) if channel == -1: msg = "Cannot play the sound: %s" % mixer.Mix_GetError() logger.error(msg) raise SoundError(msg) mixer.Mix_Volume(channel, int(mixer.MIX_MAX_VOLUME * volume)) return channel
def play(self, sound): self.played.append(sound.path) chunk = sound.raw if sdlmixer.Mix_PlayChannel(-1, chunk, 0) == -1: raise Exception("Could not play chunk") sdlmixer.Mix_HaltChannel(-1)
def play(chunk): if sdlmixer.Mix_PlayChannel(-1, chunk, 0) == -1: raise Exception("Could not play chunk")
raise RuntimeError("Cannot initialize audio system: {}".format( sdl2.SDL_GetError())) # int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize) if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError("Cannot open mixed audio: {}".format( sdlmixer.Mix_GetError())) sdlmixer.Mix_AllocateChannels(64) sound_file = RESOURCES.get_path("Cat.wav") sample = sdlmixer.Mix_LoadWAV(sdl2.ext.compat.byteify(sound_file, "utf-8")) if sample is None: raise RuntimeError("Cannot open audio file: {}".format( sdlmixer.Mix_GetError())) count = 5 while count > 0: channel = sdlmixer.Mix_PlayChannel(channel=-1, chunk=sample, loops=0) sdlmixer.Mix_SetPosition(channel, 270, 100) print(channel) if channel == -1: raise RuntimeError("Cannot play sample: {}".format( sdlmixer.Mix_GetError())) # while sdlmixer.Mix_Playing(channel): sdl2.SDL_Delay(800) count -= 1 sdlmixer.Mix_CloseAudio() sdl2.SDL_Quit(sdl2.SDL_INIT_AUDIO)
def play(self, chunk): self.played.append(chunk) if sdlmixer.Mix_PlayChannel(-1, chunk, 0) == -1: raise Exception("Could not play chunk") sdlmixer.Mix_HaltChannel(-1)