Changeset 249

Show
Ignore:
Timestamp:
01/05/07 04:14:50 (5 years ago)
Author:
mg
Message:

Make the sounds configurable in an ini file.

Location:
trunk/src/pyspacewar
Files:
1 added
1 modified

Legend:

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

    r248 r249  
    212212    value = 1 / (1 + math.exp(-t)) 
    213213    return y1 + (y2 - y1) * value 
     214 
     215 
     216class 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 
    214227 
    215228 
     
    19801993    def _load_sounds(self): 
    19811994        """Load sound effects.""" 
     1995        config = ConfigParser.RawConfigParser() 
     1996        config.add_section('sounds') 
     1997        config.read([find('sounds', 'sounds.ini')]) 
     1998 
     1999        def load_sound(name): 
     2000            if config.has_option('sounds', name): 
     2001                filename = config.get('sounds', name) 
     2002                if filename: 
     2003                    try: 
     2004                        return pygame.mixer.Sound(find('sounds', filename)) 
     2005                    except pygame.error: 
     2006                        print "pyspacewar: could not load %s" % filename 
     2007            return NoSound() 
     2008 
    19822009        self.thruster_sound_playing = False 
    1983         self.thruster_sound = pygame.mixer.Sound(find('sounds', 
    1984                                                       'Pink_Noise1.wav')) 
     2010        self.thruster_sound = load_sound('thruster') 
    19852011        self.thruster_sound.set_volume(0.5) 
    1986         self.fire_sound = pygame.mixer.Sound(find('sounds', 
    1987                                                   'Gun_Silencer.wav')) 
    1988         self.bounce_sound = pygame.mixer.Sound(find('sounds', 
    1989                                                     'electricshock.wav')) 
    1990         self.hit_sound = pygame.mixer.Sound(find('sounds', 'Grenade2.wav')) 
    1991         self.explode_sound = pygame.mixer.Sound(find('sounds', 'bomb.wav')) 
    1992         self.respawn_sound = pygame.mixer.Sound(find('sounds', 'coin2.wav')) 
    1993         self.menu_sound = pygame.mixer.Sound(find('sounds', 'briefcs1.wav')) 
     2012        self.fire_sound = load_sound('fire') 
     2013        self.bounce_sound = load_sound('bounce') 
     2014        self.hit_sound = load_sound('hit') 
     2015        self.explode_sound = load_sound('explode') 
     2016        self.respawn_sound = load_sound('respawn') 
     2017        self.menu_sound = load_sound('menu') 
    19942018 
    19952019    def _load_music(self):