Changeset 254

Show
Ignore:
Timestamp:
01/07/07 21:33:21 (5 years ago)
Author:
mg
Message:

Redo the way sounds effects are done.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/pyspacewar/ui.py

    r253 r254  
    212212    value = 1 / (1 + math.exp(-t)) 
    213213    return y1 + (y2 - y1) * value 
    214  
    215  
    216 class NoSound(object): 
    217     """A pygame.mixer.Sound stub for when you cannot load a sound file.""" 
    218  
    219     def play(*args): 
    220         pass 
    221  
    222     def stop(*args): 
    223         pass 
    224  
    225     def set_volume(*args): 
    226         pass 
    227214 
    228215 
     
    14001387        action = self.menu_items[self.menu.selected_item][1:] 
    14011388        if action: 
    1402             self.ui.menu_sound.play() 
     1389            self.ui.play_sound('menu') 
    14031390            handler = action[0] 
    14041391            args = action[1:] 
     
    20091996        config.add_section('sounds') 
    20101997        config.read([find('sounds', 'sounds.ini')]) 
    2011  
    2012         def load_sound(name): 
     1998        self.sounds = {} 
     1999        self.sound_looping = {} 
     2000        for name in ['thruster', 'fire', 'bounce', 'hit', 'explode', 'respawn', 
     2001                     'menu']: 
    20132002            if config.has_option('sounds', name): 
    20142003                filename = config.get('sounds', name) 
    20152004                if filename: 
    20162005                    try: 
    2017                         return pygame.mixer.Sound(find('sounds', filename)) 
     2006                        sound = pygame.mixer.Sound(find('sounds', filename)) 
     2007                        self.sounds[name] = sound 
    20182008                    except pygame.error: 
    20192009                        print "pyspacewar: could not load %s" % filename 
    2020             return NoSound() 
    2021  
    2022         self.thruster_sound_playing = False 
    2023         self.thruster_sound = load_sound('thruster') 
    2024         self.thruster_sound.set_volume(0.5) 
    2025         self.fire_sound = load_sound('fire') 
    2026         self.bounce_sound = load_sound('bounce') 
    2027         self.hit_sound = load_sound('hit') 
    2028         self.explode_sound = load_sound('explode') 
    2029         self.respawn_sound = load_sound('respawn') 
    2030         self.menu_sound = load_sound('menu') 
     2010        if 'thruster' in self.sounds: 
     2011            self.sounds['thruster'].set_volume(0.5) 
    20312012 
    20322013    def _load_music(self): 
     
    20572038                pygame.mixer.music.stop() 
    20582039        self.now_playing = which 
     2040 
     2041    def play_sound(self, which): 
     2042        """Play a certain sound effect.""" 
     2043        if which in self.sounds: 
     2044            self.sounds[which].play() 
     2045 
     2046    def start_sound(self, which): 
     2047        """Start looping a certain sound effect.""" 
     2048        if not self.sound_looping.get(which) and which in self.sounds: 
     2049            self.sounds[which].play(-1) 
     2050            self.sound_looping[which] = True 
     2051 
     2052    def stop_sound(self, which): 
     2053        """Stop playing a certain sound effect.""" 
     2054        if self.sound_looping.get(which) and which in self.sounds: 
     2055            self.sounds[which].stop() 
     2056            self.sound_looping[which] = False 
    20592057 
    20602058    def _init_fonts(self): 
     
    22092207    def main_menu(self): 
    22102208        """Enter the main menu.""" 
    2211         self.menu_sound.play() 
     2209        self.play_sound('menu') 
    22122210        self.ui_mode = MainMenuMode(self) 
    22132211 
     
    22532251    def game_menu(self): 
    22542252        """Enter the game menu.""" 
    2255         self.menu_sound.play() 
     2253        self.play_sound('menu') 
    22562254        self.ui_mode = GameMenuMode(self) 
    22572255 
     
    23552353        player_id = self.ships.index(ship) 
    23562354        if not self.ai_controlled[player_id] or self.sound_in_vacuum: 
    2357             self.fire_sound.play() 
     2355            self.play_sound('fire') 
    23582356 
    23592357    def bounce_effect_Ship(self, ship, obstacle): 
     
    23632361            # It sounds weird to hear that sound when dead ships bounce 
    23642362            if not self.ai_controlled[player_id] or self.sound_in_vacuum: 
    2365                 self.bounce_sound.play() 
     2363                self.play_sound('bounce') 
    23662364 
    23672365    def hit_effect_Ship(self, ship, missile): 
     
    23692367        player_id = self.ships.index(ship) 
    23702368        if not self.ai_controlled[player_id] or self.sound_in_vacuum: 
    2371             self.hit_sound.play() 
     2369            self.play_sound('hit') 
    23722370 
    23732371    def explode_effect_Ship(self, ship, killer): 
     
    23752373        player_id = self.ships.index(ship) 
    23762374        if not self.ai_controlled[player_id] or self.sound_in_vacuum: 
    2377             self.explode_sound.play() 
     2375            self.play_sound('explode') 
    23782376 
    23792377    def respawn_effect_Ship(self, ship): 
    23802378        """Play a sound effect when the player's ship respawns.""" 
    23812379        player_id = self.ships.index(ship) 
    2382         self.respawn_sound.play() 
     2380        self.play_sound('respawn') 
    23832381 
    23842382    def update_continuous_sounds(self): 
     
    23902388                               ship.left_thrust or ship.right_thrust or 
    23912389                               ship.engage_brakes) or makes_noise 
    2392         if self.thruster_sound_playing and not makes_noise: 
    2393             self.thruster_sound.stop() 
    2394         elif not self.thruster_sound_playing and makes_noise: 
    2395             self.thruster_sound.play(-1) 
    2396         self.thruster_sound_playing = makes_noise 
     2390        if makes_noise: 
     2391            self.start_sound('thruster') 
     2392        else: 
     2393            self.stop_sound('thruster') 
    23972394 
    23982395    def draw(self):