Changeset 262:de60329cfc9f


Ignore:
Timestamp:
04/08/11 09:29:37 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Rebase:
34663534313731613064333639323461393737643931383739643236326631666537323831343265
Message:

Factor out sound stuff

Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/__main__.py

    r169 r262  
    99
    1010from . import options
    11 from .constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
     11from .constants import SCREEN, DEBUG
    1212from .engine import Engine
    1313from .levelscene import LevelScene
    1414from .menuscene import MenuScene
     15from .sound import SoundSystem
    1516
    1617
     
    4041    pygame.display.init()
    4142    pygame.font.init()
    42     if options['sound']:
    43         try:
    44             pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
    45         except pygame.error, exc:
    46             raise
    47             # TODO: bail out to no_sound(exc)
    48     else:
    49         # Ensure get_sound returns nothing, so everything else just works
    50         # TODO: bail out to disable_sound()
    51         pass
    52 
     43    soundsystem = SoundSystem(options['sound'])
    5344    pygame.display.set_mode(SCREEN, SWSURFACE)
    5445    #pygame.display.set_icon(pygame.image.load(
     
    5647    pygame.display.set_caption("Nine Tales")
    5748
    58     engine = Engine()
     49    engine = Engine(soundsystem)
    5950    if level is not None:
    60         engine.change_scene(LevelScene(engine.game_state, level))
     51        engine.change_scene(LevelScene(engine.game_state, soundsystem, level))
    6152    else:
    62         engine.change_scene(MenuScene(engine.game_state))
     53        engine.change_scene(MenuScene(engine.game_state, soundsystem))
    6354    try:
    6455        engine.run()
  • skaapsteker/cutscene.py

    r247 r262  
    1010
    1111class CutScene(Scene):
    12     def __init__(self, game_state, text, background, music=None):
    13         super(CutScene, self).__init__(game_state)
     12    def __init__(self, game_state, soundsystem, text, background, music=None):
     13        super(CutScene, self).__init__(game_state, soundsystem)
    1414        self.background = data.load_image('backgrounds/' + background)
    1515        self.start_time = pygame.time.get_ticks()
     
    1717
    1818        self._background_music = None
    19         if music and pygame.mixer.get_init():
    20             self._background_music = data.filepath(music)
     19        self._background_music = music
    2120
    2221        text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40),
     
    5756    def enter(self):
    5857        if self._background_music:
    59             pygame.mixer.music.load(self._background_music)
    60             pygame.mixer.music.play(-1)
    61 
    62     def leave(self):
    63         if self._background_music:
    64             pygame.mixer.music.stop()
     58            self._soundsystem.play_background_music(self._background_music)
    6559
    6660
    67 def opening_cutscene(game_state):
     61def opening_cutscene(game_state, soundsystem):
    6862    text = u"""
    6963    Many moons ago, an evil nine-tailed kitsune, a fearsome fox god, ruled the
     
    8377    """
    8478    text = unindent_text(text)
    85     return CutScene(game_state, text, 'background_01_back.png')
     79    return CutScene(game_state, soundsystem, text, 'background_01_back.png')
  • skaapsteker/engine.py

    r250 r262  
    99class Engine(object):
    1010
    11     def __init__(self):
     11    def __init__(self, soundsystem):
    1212        # avoid circular imports
    1313        from .gamestate import GameState
     
    1717        self._cur_frame = 0
    1818        self.game_state = GameState("game.json")
     19        self.soundsystem = soundsystem
    1920
    2021    def change_scene(self, next_scene):
     22        self.soundsystem.stop_music()
    2123        if self._current_scene is not None:
    2224            self._current_scene.leave()
     
    5254class Scene(object):
    5355
    54     def __init__(self, game_state):
     56    def __init__(self, game_state, soundsystem):
    5557        self.widgets = []
    5658        self.game_state = game_state
     59        self._soundsystem = soundsystem
    5760
    5861    def post(self, ev):
  • skaapsteker/level.py

    r249 r262  
    5454
    5555class Level(object):
    56     def __init__(self, leveldef, player):
     56    def __init__(self, leveldef, player, soundsystem):
    5757        self.name = leveldef
    5858        self.level_data = json.loads(data.load('levels/' + leveldef + '.json').read())
    5959        self.sprites = LayeredUpdates()
     60        self._soundsystem = soundsystem
    6061        self.build_backgrounds()
    6162        self.build_tiles()
     
    6465        self.setup_player(player)
    6566        self._background_music = None
    66         if 'music' in self.level_data and mixer.get_init():
    67             self._background_music = data.filepath('music/' + self.level_data['music'])
     67        if 'music' in self.level_data:
     68            # soundsystem will call data.filepath
     69            self._background_music = 'music/' + self.level_data['music']
    6870
    6971    def build_backgrounds(self):
     
    7274            self.backgrounds.append(data.load_image('backgrounds/' + background))
    7375
     76    def enter(self):
     77        if self._background_music:
     78            self._soundsystem.play_background_music(self._background_music)
    7479
    7580    def leave(self):
    76         if self._background_music:
    77             mixer.music.stop()
    78 
    79 
    80     def enter(self):
    81         if self._background_music:
    82             mixer.music.load(self._background_music)
    83             mixer.music.play(-1)
     81        pass
    8482
    8583    def build_tiles(self):
  • skaapsteker/levelscene.py

    r251 r262  
    1717class LevelScene(engine.Scene):
    1818
    19     def __init__(self, game_state, leveldef):
    20         super(LevelScene, self).__init__(game_state)
     19    def __init__(self, game_state, soundsystem, leveldef):
     20        super(LevelScene, self).__init__(game_state, soundsystem)
    2121
    2222        self._player = player.Player(game_state.world)
    23         self._level = level.Level(leveldef, self._player)
     23        self._level = level.Level(leveldef, self._player, soundsystem)
    2424        self._leveldef = leveldef
    2525        self._player_dead = False
     
    6868        import menuscene # avoid circular import
    6969        if pause:
    70             engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self))
     70            engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self._soundsystem, self))
    7171        else:
    7272            # FIXME: When starting the game, we shoudl ensure we have sane
     
    7474            if self._player_dead:
    7575                self._player.restore()
    76             engine.ChangeScene.post(menuscene.MenuScene(self.game_state, None))
     76            engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self._soundsystem, None))
    7777
    7878    def _restart(self):
    7979        if self._player_dead:
    8080            self._player.restore()
    81         engine.ChangeScene.post(LevelScene(self.game_state, self._leveldef))
     81        engine.ChangeScene.post(LevelScene(self.game_state, self._soundsystem, self._leveldef))
    8282
    8383    def _toggle_pause(self):
  • skaapsteker/menuscene.py

    r248 r262  
    99
    1010class MenuScene(Scene):
    11     def __init__(self, game_state, cur_game=None):
    12         super(MenuScene, self).__init__(game_state)
     11    def __init__(self, game_state, soundsystem, cur_game=None):
     12        super(MenuScene, self).__init__(game_state, soundsystem)
    1313        self.widgets.append(Text("MENU:", (50, 50), color='white', size=48))
    1414        self.cur_game = cur_game
     
    3333            ChangeScene.post(self.cur_game)
    3434        elif data == 'cutscene':
    35             ChangeScene.post(opening_cutscene(self.game_state))
     35            ChangeScene.post(opening_cutscene(self.game_state, self._soundsystem))
    3636        elif data == 'quit':
    3737            pygame.event.post(pygame.event.Event(QUIT))
    3838        else:
    39             ChangeScene.post(LevelScene(self.game_state, data))
     39            ChangeScene.post(LevelScene(self.game_state, self._soundsystem, data))
    4040
    4141    def draw(self, surface, engine):
Note: See TracChangeset for help on using the changeset viewer.