Changeset 254
- Timestamp:
- 01/07/07 21:33:21 (5 years ago)
- Files:
-
- 1 modified
-
trunk/src/pyspacewar/ui.py (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/pyspacewar/ui.py
r253 r254 212 212 value = 1 / (1 + math.exp(-t)) 213 213 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 pass221 222 def stop(*args):223 pass224 225 def set_volume(*args):226 pass227 214 228 215 … … 1400 1387 action = self.menu_items[self.menu.selected_item][1:] 1401 1388 if action: 1402 self.ui. menu_sound.play()1389 self.ui.play_sound('menu') 1403 1390 handler = action[0] 1404 1391 args = action[1:] … … 2009 1996 config.add_section('sounds') 2010 1997 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']: 2013 2002 if config.has_option('sounds', name): 2014 2003 filename = config.get('sounds', name) 2015 2004 if filename: 2016 2005 try: 2017 return pygame.mixer.Sound(find('sounds', filename)) 2006 sound = pygame.mixer.Sound(find('sounds', filename)) 2007 self.sounds[name] = sound 2018 2008 except pygame.error: 2019 2009 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) 2031 2012 2032 2013 def _load_music(self): … … 2057 2038 pygame.mixer.music.stop() 2058 2039 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 2059 2057 2060 2058 def _init_fonts(self): … … 2209 2207 def main_menu(self): 2210 2208 """Enter the main menu.""" 2211 self. menu_sound.play()2209 self.play_sound('menu') 2212 2210 self.ui_mode = MainMenuMode(self) 2213 2211 … … 2253 2251 def game_menu(self): 2254 2252 """Enter the game menu.""" 2255 self. menu_sound.play()2253 self.play_sound('menu') 2256 2254 self.ui_mode = GameMenuMode(self) 2257 2255 … … 2355 2353 player_id = self.ships.index(ship) 2356 2354 if not self.ai_controlled[player_id] or self.sound_in_vacuum: 2357 self. fire_sound.play()2355 self.play_sound('fire') 2358 2356 2359 2357 def bounce_effect_Ship(self, ship, obstacle): … … 2363 2361 # It sounds weird to hear that sound when dead ships bounce 2364 2362 if not self.ai_controlled[player_id] or self.sound_in_vacuum: 2365 self. bounce_sound.play()2363 self.play_sound('bounce') 2366 2364 2367 2365 def hit_effect_Ship(self, ship, missile): … … 2369 2367 player_id = self.ships.index(ship) 2370 2368 if not self.ai_controlled[player_id] or self.sound_in_vacuum: 2371 self. hit_sound.play()2369 self.play_sound('hit') 2372 2370 2373 2371 def explode_effect_Ship(self, ship, killer): … … 2375 2373 player_id = self.ships.index(ship) 2376 2374 if not self.ai_controlled[player_id] or self.sound_in_vacuum: 2377 self. explode_sound.play()2375 self.play_sound('explode') 2378 2376 2379 2377 def respawn_effect_Ship(self, ship): 2380 2378 """Play a sound effect when the player's ship respawns.""" 2381 2379 player_id = self.ships.index(ship) 2382 self. respawn_sound.play()2380 self.play_sound('respawn') 2383 2381 2384 2382 def update_continuous_sounds(self): … … 2390 2388 ship.left_thrust or ship.right_thrust or 2391 2389 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') 2397 2394 2398 2395 def draw(self):
