Exemplo n.º 1
0
    def __init__(self, main_menu):
        self.parent     = main_menu.parent
        self.main_menu  = main_menu
        self.title_banner = main_menu.title_banner
        self.alpha = 255

        self.music_manager = MusicManager()
        self.volume = self.music_manager.get_music_volume()

        fade = animations.ParallelAnimation()
        fade_display = animations.FadeOut(self.set_alpha, time=3000)
        fade_sound = animations.MoveValue(self.music_manager.set_music_volume, self.volume, 0,  time=3000)
        fade.add_animation(fade_display)
        fade.add_animation(fade_sound)
        self.animations = fade

        self.parent.event_handler.register_key_listener(self.handle_keypress)
Exemplo n.º 2
0
    def __init__(self):
        pygame.mixer.pre_init(44100, -16, 1, 2048)
        pygame.init()

        # Load settings from JSON file
        json_data = open(directories.SETTINGS_PATH).read()
        self.settings = json.loads(json_data)
        self.resolution_key = self.settings['screen']['resolution']
        resolution = self.settings['valid_resolutions'][self.resolution_key]
        self.resolution = (resolution['width'], resolution['height'])
        self.title = self.settings['title']

        self.winners = []

        self.event_handler = EventHandler()
        self.music_manager = MusicManager(self.settings)
        self.sound_manager = SoundManager(self.settings)
        self.image_manager = ImageManager()

        self.music_manager.set_music_stopped_event(MUSIC_STOPPED)

        self.event_handler.register_quit_listener(self.quit)
        self.event_handler.register_set_game_state_listener(self.update_state)
        self.event_handler.register_music_stopped_listener(
            self.music_manager.handle_song_stopped)
        self.event_handler.register_state_change_listener(
            self.music_manager.handle_state_change)
        self.event_handler.register_sound_effect_listener(
            self.sound_manager.handle_sound_effect)

        # self.event_handler.register_settings_update_listener(self.update_settings)
        self.event_handler.register_settings_update_listener(
            self.music_manager.handle_settings_update)
        self.event_handler.register_settings_update_listener(
            self.sound_manager.handle_settings_update)

        self.quit = False
        self.states = {
            'main_menu': main_menu.MainMenu,
            'in_game': in_game.Game,
            'announce_winners': winners.AnnounceWinners
        }
        self.state_code = None
        SpellBook.load_spell_book(directories.MAGIC_PATH)
        self.teams = team.load_teams(directories.TEAM_PATH)
Exemplo n.º 3
0
class StateAnimatedOutro(State):
    def __init__(self, main_menu):
        self.parent     = main_menu.parent
        self.main_menu  = main_menu
        self.title_banner = main_menu.title_banner
        self.alpha = 255

        self.music_manager = MusicManager()
        self.volume = self.music_manager.get_music_volume()

        fade = animations.ParallelAnimation()
        fade_display = animations.FadeOut(self.set_alpha, time=3000)
        fade_sound = animations.MoveValue(self.music_manager.set_music_volume, self.volume, 0,  time=3000)
        fade.add_animation(fade_display)
        fade.add_animation(fade_sound)
        self.animations = fade

        self.parent.event_handler.register_key_listener(self.handle_keypress)

    def set_alpha(self, alpha):
        self.alpha = alpha

    def render(self):
        surface = pygame.Surface(self.parent.resolution)
        surface.blit(self.title_banner.render(), self.main_menu.banner_position)
        surface.blit(self.main_menu.menu.get_menu_surface(), self.main_menu.menu_position)

        mask = pygame.Surface(self.parent.resolution, pygame.SRCALPHA)
        mask.fill((0,0,0, 255-self.alpha))
        surface.blit(mask, (0,0))

        return surface

    def update(self, delta_t):
        if self.animations.finished():
            self.exit_state()
        else:
            self.animations.animate(delta_t)

    def skip(self):
        self.animations.skip()

    def handle_keypress(self, event):
        if event.type == pygame.KEYDOWN:
            self.skip()

    def exit_state(self):
        self.parent.event_handler.unregister_key_listener(self.handle_keypress)
        event = pygame.event.Event(SET_GAME_STATE, state="in_game", seed='league_view')
        pygame.event.post(event)
Exemplo n.º 4
0
    def __init__(self, parent, state_seed='default'):
        self.parent = parent
        self.event_handler = parent.event_handler
        self.resolution = self.parent.resolution

        self.states = {'default': StateWinnersView}

        self.cur_state = state_seed
        self.state = self.states[self.cur_state](self)

        music_manager = MusicManager()
        music_manager.restore_music_volume()
        music_manager.play_song("champions", loops=-1)
Exemplo n.º 5
0
class Walton:
    def __init__(self):
        pygame.mixer.pre_init(44100, -16, 1, 2048)
        pygame.init()

        # Load settings from JSON file
        json_data = open(directories.SETTINGS_PATH).read()
        self.settings = json.loads(json_data)
        self.resolution_key = self.settings['screen']['resolution']
        resolution = self.settings['valid_resolutions'][self.resolution_key]
        self.resolution = (resolution['width'], resolution['height'])
        self.title = self.settings['title']

        self.winners = []

        self.event_handler = EventHandler()
        self.music_manager = MusicManager(self.settings)
        self.sound_manager = SoundManager(self.settings)
        self.image_manager = ImageManager()

        self.music_manager.set_music_stopped_event(MUSIC_STOPPED)

        self.event_handler.register_quit_listener(self.quit)
        self.event_handler.register_set_game_state_listener(self.update_state)
        self.event_handler.register_music_stopped_listener(
            self.music_manager.handle_song_stopped)
        self.event_handler.register_state_change_listener(
            self.music_manager.handle_state_change)
        self.event_handler.register_sound_effect_listener(
            self.sound_manager.handle_sound_effect)

        # self.event_handler.register_settings_update_listener(self.update_settings)
        self.event_handler.register_settings_update_listener(
            self.music_manager.handle_settings_update)
        self.event_handler.register_settings_update_listener(
            self.sound_manager.handle_settings_update)

        self.quit = False
        self.states = {
            'main_menu': main_menu.MainMenu,
            'in_game': in_game.Game,
            'announce_winners': winners.AnnounceWinners
        }
        self.state_code = None
        SpellBook.load_spell_book(directories.MAGIC_PATH)
        self.teams = team.load_teams(directories.TEAM_PATH)

    def __game_loop(self):
        clock = pygame.time.Clock()
        time = pygame.time.get_ticks()
        pygame.mouse.set_visible(False)

        # self.set_state('announce_winners', 'default')
        self.set_state('main_menu', 'intro')
        # self.set_state('in_game', 'battle_view')

        while not self.quit:
            # Regulate framerate
            clock.tick(60)

            for event in pygame.event.get():
                self.event_handler.handle_event(event)

            delta_t = pygame.time.get_ticks() - time
            time = pygame.time.get_ticks()
            self.state.update(delta_t)
            render = self.state.render()
            self.screen.blit(render, (0, 0))
            pygame.display.flip()

    def __initialize_display(self):
        self.resolution_key = self.settings['screen']['resolution']
        resolution = self.settings['valid_resolutions'][self.resolution_key]
        self.resolution = (resolution['width'], resolution['height'])

        if self.settings['screen']['fullscreen']:
            self.screen = pygame.display.set_mode(
                self.resolution, pygame.FULLSCREEN | pygame.DOUBLEBUF)
        else:
            self.screen = pygame.display.set_mode(self.resolution,
                                                  pygame.DOUBLEBUF)
        pygame.display.set_caption(self.settings['title'])

    def set_state(self, state_code, state_seed=None):
        self.state_code = state_code
        if state_seed != None:
            self.state = self.states[self.state_code](self, state_seed)
        else:
            self.state = self.states[self.state_code](self)
        event = pygame.event.Event(STATE_CHANGED, state=state_code)
        pygame.event.post(event)

    def run(self):
        self.__initialize_display()
        self.__game_loop()

    def update_settings(self, event):
        if self.settings['screen']['resolution'] != self.resolution_key:
            self.__initialize_display()
            self.state.update_display()

    def start_game(self, event):
        self.set_state('in_game')

    def update_state(self, event):
        self.set_state(event.state, event.seed)

    def quit(self, event):
        with open(directories.SETTINGS_PATH, "w") as f:
            json.dump(self.settings, f, indent=4, ensure_ascii=False)

        # Wait for any sound effects to stop playing
        if self.state_code == "main_menu":
            while self.sound_manager.still_playing():
                continue

        self.quit = True
Exemplo n.º 6
0
    def __init__(self, settings):
        self.settings = settings
        self.selected = 0
        self.valid_resolutions = [
            "{} x {}".format(item['width'], item['height'])
            for item in settings['valid_resolutions']
        ]
        self.spacing_margin = 25
        self.padding = 10

        self.sound_enabled = BooleanMenuItem(
            "Sound", self.settings['sound']['sound_enabled'])
        self.sound_volume = SliderMenuItem(
            "Sound Volume", 0, 100,
            self.settings['sound']['sound_volume'] * 100,
            self.preview_sound_change)
        self.sound_volume.bottom_margin = self.spacing_margin

        self.music_enabled = BooleanMenuItem(
            "Music", self.settings['sound']['music_enabled'])
        self.music_volume = SliderMenuItem(
            "Music Volume", 0, 100,
            self.settings['sound']['music_volume'] * 100,
            self.preview_music_change)
        self.music_volume.bottom_margin = self.spacing_margin

        self.fullscreen = BooleanMenuItem(
            "Fullscreen", self.settings['screen']['fullscreen'])
        self.resolution = MultiOptionMenuItem(
            "Resolution", self.valid_resolutions,
            self.settings['screen']['resolution'])
        self.resolution.bottom_margin = self.spacing_margin

        self.finished_choice = ChoiceMenuItem("Apply", "Cancel", self.finished)

        self.sound_manager = SoundManager()
        self.music_manager = MusicManager()
        self.items = [
            self.sound_enabled, self.sound_volume, self.music_enabled,
            self.music_volume, self.fullscreen, self.resolution,
            self.finished_choice
        ]

        self.callback = None
        self.done = False

        max_height = 0
        self.width = 0
        for item in self.items:
            if item.get_height() > max_height:
                max_height = item.get_height()
            if item.get_width() > self.width:
                self.width = item.get_width()

        self.height = (max_height + self.padding) * (len(
            self.items)) - self.padding
        for item in self.items:
            self.height += item.bottom_margin
            item.set_height(max_height)
            item.set_width(self.width)

        self.items[self.selected].set_focused(True)
Exemplo n.º 7
0
class SettingsMenu:
    def __init__(self, settings):
        self.settings = settings
        self.selected = 0
        self.valid_resolutions = [
            "{} x {}".format(item['width'], item['height'])
            for item in settings['valid_resolutions']
        ]
        self.spacing_margin = 25
        self.padding = 10

        self.sound_enabled = BooleanMenuItem(
            "Sound", self.settings['sound']['sound_enabled'])
        self.sound_volume = SliderMenuItem(
            "Sound Volume", 0, 100,
            self.settings['sound']['sound_volume'] * 100,
            self.preview_sound_change)
        self.sound_volume.bottom_margin = self.spacing_margin

        self.music_enabled = BooleanMenuItem(
            "Music", self.settings['sound']['music_enabled'])
        self.music_volume = SliderMenuItem(
            "Music Volume", 0, 100,
            self.settings['sound']['music_volume'] * 100,
            self.preview_music_change)
        self.music_volume.bottom_margin = self.spacing_margin

        self.fullscreen = BooleanMenuItem(
            "Fullscreen", self.settings['screen']['fullscreen'])
        self.resolution = MultiOptionMenuItem(
            "Resolution", self.valid_resolutions,
            self.settings['screen']['resolution'])
        self.resolution.bottom_margin = self.spacing_margin

        self.finished_choice = ChoiceMenuItem("Apply", "Cancel", self.finished)

        self.sound_manager = SoundManager()
        self.music_manager = MusicManager()
        self.items = [
            self.sound_enabled, self.sound_volume, self.music_enabled,
            self.music_volume, self.fullscreen, self.resolution,
            self.finished_choice
        ]

        self.callback = None
        self.done = False

        max_height = 0
        self.width = 0
        for item in self.items:
            if item.get_height() > max_height:
                max_height = item.get_height()
            if item.get_width() > self.width:
                self.width = item.get_width()

        self.height = (max_height + self.padding) * (len(
            self.items)) - self.padding
        for item in self.items:
            self.height += item.bottom_margin
            item.set_height(max_height)
            item.set_width(self.width)

        self.items[self.selected].set_focused(True)

    def render(self):
        surface = pygame.Surface((self.width, self.height))
        offset = 0
        for i in range(len(self.items)):
            item_surface = self.items[i].render()
            surface.blit(item_surface, (0, offset))
            offset += self.items[i].get_height(
            ) + self.padding + self.items[i].bottom_margin

        return surface

    def preview_sound_change(self, volume):
        self.sound_manager.set_volume(volume / 100.0)

    def preview_music_change(self, volume):
        self.music_manager.set_music_volume(volume / 100.0)

    def move_selection(self, distance):
        event = pygame.event.Event(SOUND_EFFECT, message="menu_move")
        pygame.event.post(event)
        self.items[self.selected].set_focused(False)
        self.selected += distance // abs(distance)
        self.selected %= len(self.items)
        self.items[self.selected].set_focused(True)

    def click_selected(self, direction):
        if direction == 3:
            self.quit()
        else:
            self.items[self.selected].click(direction)

    def register_finished_callback(self, callback):
        self.callback = callback

    def quit(self):
        self.finished_choice.set_value(1)
        self.finished_choice.click(2)

    def finished(self):
        if self.finished_choice.get_value() == 0:
            self.settings['sound'][
                'sound_enabled'] = self.sound_enabled.get_value()
            self.settings['sound'][
                'sound_volume'] = self.sound_volume.get_value() / 100.0
            self.settings['sound'][
                'music_enabled'] = self.music_enabled.get_value()
            self.settings['sound'][
                'music_volume'] = self.music_volume.get_value() / 100.0
            self.settings['screen']['fullscreen'] = self.fullscreen.get_value()
            self.settings['screen']['resolution'] = self.resolution.get_value()

        event = pygame.event.Event(SETTINGS_UPDATED)
        pygame.event.post(event)

        if self.callback != None:
            self.callback()