def set_playing(self, sound: pygame.mixer.Sound, fade_ms=0): if not PLAY_SOUNDS: return if sound not in self.playing_sounds: self.playing_sounds[sound] = self.free_channels[0] self.free_channels = self.free_channels[1:] if self.get_enabled() and not self.playing_sounds[sound].get_busy(): sound.play(loops=-1, fade_ms=fade_ms)
def check_collision(pipes, bird_rect, death_sound: pygame.mixer.Sound) -> bool: for pipe in pipes: if bird_rect.colliderect(pipe): death_sound.play() return False if bird_rect.top <= -100 or bird_rect.bottom >= 900: death_sound.play() return False return True
def play_sound(self, sound: pygame.mixer.Sound): """ Plays the sound with volume proportional to distance to player If that sound is already played, it is stopped and played again with better volume This is done to remove sounds ugly overlaying :param sound: sound to play :return: None """ # The volume is calculated as a upside-down parabola # pygame converts negative volume to 1 volume = max( 0, -0.000001 * (self.game.player.pos - self.pos).length_squared() + 1) if sound.get_num_channels() == 0 or sound.get_volume() < volume: sound.stop() sound.set_volume(volume) sound.play()
def update( self, game_cursor_pos: tuple, game_sound: pg.mixer.Sound, game_client_data_player_img: pg.Surface, game_client_dt: float, game_client_data_bullet_img: pg.Surface, game_client_data_weaponvfx: list, walls: pg.sprite.Group, ) -> (pg.sprite.Sprite, pg.sprite.Sprite): pos, direction = self.get_keys(game_cursor_pos) bullet, vfx = None, None if pos: bullet = Bullet( self.settings, game_client_data_bullet_img, pos, direction, self.rot, ) vfx = Weapon_VFX( self.settings, game_client_data_weaponvfx, self.pos + vec(self.settings["player"]["hand_offset"]).rotate(-self.rot), ) if self.sound_on: # and random() < 0.75: game_sound.play() self.rot = (game_cursor_pos - self.pos).angle_to(vec(1, 0)) % 360 self.image = pg.transform.rotate( game_client_data_player_img, self.rot, ) self.rect = self.image.get_rect() self.rect.center = self.pos self.pos += self.vel * game_client_dt self.hit_rect.centerx = self.pos.x collide_with_walls(self, walls, "x") self.hit_rect.centery = self.pos.y collide_with_walls(self, walls, "y") self.rect.center = self.hit_rect.center return bullet, vfx
def play_audio(audio: pygame.mixer.Sound, loop=False) -> None: audio.play(-1 if loop else 0)
def play(sound: pygame.mixer.Sound): volume = get_sound_volume() sound.set_volume(volume) sound.play()
def play_sound(sound: pygame.mixer.Sound) -> None: if Window.__enable_sound and isinstance(sound, pygame.mixer.Sound): sound.play()
def play(self, sound: pygame.mixer.Sound): if not PLAY_SOUNDS: return if self.get_enabled(): sound.play()
def play(sound: pygame.mixer.Sound, volume=False) -> None: volume = get_sound_volume() if volume is False else volume/10 # 0->10 must be divided by ten to be in 0->1 range sound.set_volume(volume) sound.play()