示例#1
0
 def __init__(self, **kwargs):
     """
     Instantiate the LevelManager with event listener.
     :param kwargs:
     """
     super(LevelManager, self).__init__(**kwargs)
     self.level_service = LevelService()
     self.level_event_dispatcher = LevelEventDispatcher()
     self.level_event_dispatcher.bind(on_level_completed=self.do_level_up)
     self.levels_completed_pool = list()
示例#2
0
文件: Menu.py 项目: ISNABE/scape-me
    def __init__(self, event_dispatcher, **kwargs):
        """
        Initialize button and textures of MenuLevel.
        :param event_dispatcher: dispatcher
        :param kwargs: args of layout
        :rtype: void
        """
        super(MenuLevel, self).__init__(**kwargs)
        self.event_dispatcher = event_dispatcher
        self.level_service = LevelService()

        # Add fond.
        self.canvas.add(
            Rectangle(source=self.FOND_MENU_PATH, size=Window.size)
        )

        # Add button
        self.add_widget(
            Button(text="Menu", font_name=self.FONT_MENU_PATH, background_color=self.color_2,
                   pos_hint={'x': 0.82, 'y': 0}, size_hint=(0.18, 0.15),
                   on_press=self.switch_to_menu_screen)
        )

        menu_level_grid = GridLayout(size_hint=(0.7, 0.45), pos_hint={'x': 0.15, 'y': 0.3}, row=2)
        self.add_widget(menu_level_grid)

        set_list = os.listdir(self.MAPS_PATH)
        set_number = len(set_list)
        menu_level_grid.cols = set_number / 2

        for index in range(1, set_number + 1):
            if index % 2 == 0:
                button_title = "Level " + str(index)
                menu_level_grid.add_widget(
                    Button(
                        text=button_title,
                        font_name=self.FONT_MENU_PATH,
                        background_color=self.color_1,
                        on_press=self.launch_level,
                        cls=[index]
                    )
                )

            else:
                button_title = "Level " + str(index)
                menu_level_grid.add_widget(
                    Button(
                        text=button_title,
                        font_name=self.FONT_MENU_PATH,
                        background_color=self.color_2,
                        on_press=self.launch_level,
                        cls=[index]
                    )
                )
示例#3
0
    def __init__(self, event_dispatcher, music_provider, **kwargs):
        """
        Instantiate the LevelManager with event listener.

        :param event_dispatcher: dispatcher
        :param kwargs:
        """
        super(LevelManager, self).__init__(**kwargs)
        self.size = Window.size
        self.event_dispatcher = event_dispatcher
        self.music_provider = music_provider
        self.level_service = LevelService()
        self.level_event_dispatcher = LevelEventDispatcher()
        self.popup = None
        self.current_set_id = int
        self.current_level_id_in_set = int
        self.level = None
        self.action_bar = None
        self.level_event_dispatcher.bind(on_level_completed=self.do_level_up)
示例#4
0
class LevelManager(FloatLayout):
    """
    LevelManager children of FloatLayout.
    """

    def __init__(self, event_dispatcher, music_provider, **kwargs):
        """
        Instantiate the LevelManager with event listener.

        :param event_dispatcher: dispatcher
        :param kwargs:
        """
        super(LevelManager, self).__init__(**kwargs)
        self.size = Window.size
        self.event_dispatcher = event_dispatcher
        self.music_provider = music_provider
        self.level_service = LevelService()
        self.level_event_dispatcher = LevelEventDispatcher()
        self.popup = None
        self.current_set_id = int
        self.current_level_id_in_set = int
        self.level = None
        self.action_bar = None
        self.level_event_dispatcher.bind(on_level_completed=self.do_level_up)

    #####
    # Save and Level Up
    #####

    def do_level_up(self, _, completion_details, *unused):
        """
        Save level up in the completed pool and open popup.

        :param _:
        :param completion_details: Player stats.
        :param unused:
        :rtype: void
        """
        self.level_service.save_completion(completion_details)

        set_id_to_load = completion_details['set_id']
        level_id_in_set_to_load = completion_details['level_id_in_set'] + 1

        if level_id_in_set_to_load > 5:
            set_id_to_load = completion_details['set_id'] + 1
            level_id_in_set_to_load = 1

        open_pop_up(self, 'end_level', set_id_to_load, level_id_in_set_to_load, completion_details)

    #####
    # Set loading
    ####

    def load_level_in_set(self, set_id=None, level_id_in_set=1):
        """
        Load given level in given set with checking.

        :param set_id: id of the set to load.
        :param level_id_in_set: id of the level to load.
        :rtype: void
        """

        if not set_id or not self.level_service.does_set_exist(set_id):
            set_id = self.level_service.get_last_set_unlocked()

        self.clear_widgets()

        self.current_set_id = set_id
        self.current_level_id_in_set = level_id_in_set

        # display popup if level need popup
        open_pop_up(self, 'open_level', set_id, level_id_in_set)

        self.level = Level(
            self.level_event_dispatcher,
            set_id,
            level_id_in_set
        )
        self.add_widget(self.level)

        self.update_menu_level_bar()

        Window.bind(on_resize=self.update_menu_level_bar)

    #####
    # Pop up
    #####

    def pop_up_next(self, instance):
        """
        When player click on next button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()

        level_list = instance.cls
        self.load_level_in_set(level_list[0], level_list[1])

    def pop_up_replay(self, instance):
        """
        When player click on replay button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()
        level_list = instance.cls
        if level_list[1] == 1:
            level_list[0] -= 1
            level_list[1] = 5
        else:
            level_list[1] -= 1

        self.load_level_in_set(level_list[0], level_list[1])

    def pop_up_menu(self, _):
        """
        When player click on menu button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()
        self.switch_to_menu_screen()

    #####
    # Menu relatives
    #####

    def update_menu_level_bar(self, *_):
        """
        Update the menu label of the level.

        :rtype: void
        """

        if not self.current_set_id or not self.current_level_id_in_set:
            return

        self.action_bar = Builder.load_file('LevelActionBar.kv')
        self.action_bar.current_level = 'Set: {0} - Level: {1}'.format(
            self.current_set_id, self.current_level_id_in_set
        )
        self.add_widget(self.action_bar)

    def switch_to_menu_screen(self, *args):
        """
        Required method.
        """
        Logger.info("Propagate Menu")
        propagate_event('MenuLevel', self)
示例#5
0
class LevelManager(Widget):
    popup = None
    grid_layout = None

    def __init__(self, event_dispatcher, **kwargs):
        """
        Instantiate the LevelManager with event listener.

        :param event_dispatcher: dispatcher
        :param kwargs:
        """
        super(LevelManager, self).__init__(**kwargs)
        self.event_dispatcher = event_dispatcher
        self.level_service = LevelService()
        self.level_event_dispatcher = LevelEventDispatcher()
        self.level_event_dispatcher.bind(on_level_completed=self.do_level_up)

    def add_widget(self, widget, index=0):
        """
        Add widget only after removing any widget previously present.

        :param widget:
        :param index:
        :rtype: void
        """
        super(LevelManager, self).add_widget(widget, index)

    #####
    # Save and Level Up
    #####

    def do_level_up(self, instance, completion_details, *args):
        """
        Save level up in the completed pool and open popup.

        :param instance:
        :param completion_details:
        :param args:
        :rtype: void
        """
        self.level_service.save_completion(completion_details)

        set_id_to_load = completion_details['set_id']
        level_id_in_set_to_load = completion_details['level_id_in_set'] + 1

        if level_id_in_set_to_load >= 5:
            set_id_to_load = completion_details['set_id'] + 1
            level_id_in_set_to_load = 1

        open_pop_up(self, 'end_level', set_id_to_load, level_id_in_set_to_load, completion_details)

    #####
    # Set loading
    ####

    def can_load_set(self, set_id=None):
        """
        Test is player can play this set.

        :param set_id:
        :rtype: Boolean
        """
        if not self.level_service.does_set_exist(set_id):
            raise Exception("Set does not exist.")

        if not self.level_service.is_set_unlocked(set_id):
            Logger.info("Level is not unlocked yet.")
            return False

        return True

    def load_level_in_set(self, set_id=None, level_id_in_set=1):
        """
        Load given level in given set with checking.

        :param set_id:
        :param level_id_in_set:
        :rtype: void
        """

        if not set_id or not self.level_service.does_set_exist(set_id):
            set_id = self.level_service.get_last_set_unlocked()

        self.clear_widgets()

        # add map
        self.add_widget(Level(self.level_event_dispatcher, set_id, level_id_in_set))

        # add menu level
        self.update_menu_level_label(set_id, level_id_in_set)

        # display popup if level need popup
        open_pop_up(self, 'open_level', set_id, level_id_in_set)

    #####
    # Pop up
    #####

    def pop_up_next(self, instance):
        """
        When player click on next button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()

        level_list = instance.cls
        self.load_level_in_set(level_list[0], level_list[1])

    def pop_up_replay(self, instance):
        """
        When player click on replay button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()
        level_list = instance.cls
        if level_list[1] == 1:
            level_list[0] -= 1
            level_list[1] = 5
        else:
            level_list[1] -= 1

        self.load_level_in_set(level_list[0], level_list[1])

    def pop_up_menu(self, instance):
        """
        When player click on menu button in pop up.

        :param instance:
        :rtype: void
        """
        self.popup.dismiss()
        self.switch_to_menu_screen()

    #####
    # Menu relatives
    #####

    def update_menu_level_label(self, set_id, level_id):
        """
        Update the menu label of the level.

        :param set_id:
        :param level_id:
        :rtype: void
        """
        self.add_widget(
            Button(text="Menu", background_color=(0, 0, 0, 1), on_press=self.switch_to_menu_screen)
        )

    def switch_to_menu_screen(self, *args):
        """
        Required method.
        """
        Logger.info("Propagate Menu")
        propagate_event('Menu', self)
示例#6
0
class LevelManager(Widget):

    def __init__(self, **kwargs):
        """
        Instantiate the LevelManager with event listener.
        :param kwargs:
        """
        super(LevelManager, self).__init__(**kwargs)
        self.level_service = LevelService()
        self.level_event_dispatcher = LevelEventDispatcher()
        self.level_event_dispatcher.bind(on_level_completed=self.do_level_up)
        self.levels_completed_pool = list()

    def add_widget(self, widget, index=0):
        """
        Add widget only after removing any widget previously present.
        :param widget:
        :param index:
        :return:
        """
        self.clear_widgets()
        super(LevelManager, self).add_widget(widget, index)

    def do_level_up(self, instance, completion_details, *args):
        """
        Save advancement and level up the player loading next level.
        :param instance:
        :param completion_details:
        :param args:
        :return:
        """
        self.levels_completed_pool.append(completion_details)
        if completion_details['level_id_in_set'] >= 5:
            self.do_set_up()
            self.load_level_in_set()
            return
        self.load_level_in_set(completion_details['set_id'], completion_details['level_id_in_set'] + 1)

    def do_set_up(self):

        if len(self.levels_completed_pool) > 5:
            self.levels_completed_pool = list()
            return

        if len(self.levels_completed_pool) < 5:
            return

        for level_completed in self.levels_completed_pool:
            self.level_service.save_completion(level_completed)

    def load_set(self, set_id=None):
        """
        Load level in set
        :param set_id:
        :return:
        """
        self.load_level_in_set(set_id)

    def load_level_in_set(self, set_id=None, level_id_in_set=1):
        """
        Load given level in given set with checking.
        :param set_id:
        :param level_id_in_set:
        :return:
        """

        if not self.level_service.does_set_exist(set_id) or not set_id:
                set_id = self.level_service.get_resuming_set()

        self.add_widget(Level(self.level_event_dispatcher, set_id, level_id_in_set))
示例#7
0
文件: Menu.py 项目: ISNABE/scape-me
class MenuLevel(FloatLayout):
    """
    Level Menu class.
    """
    FONT_MENU_PATH = './resources/menu/vanadine_bold.ttf'
    FOND_MENU_PATH = './resources/menu/background_menu.png'
    MAPS_PATH = './resources/maps/'

    color_1 = COLORS['blue_color']
    color_2 = COLORS['dark_blue_color']

    def __init__(self, event_dispatcher, **kwargs):
        """
        Initialize button and textures of MenuLevel.
        :param event_dispatcher: dispatcher
        :param kwargs: args of layout
        :rtype: void
        """
        super(MenuLevel, self).__init__(**kwargs)
        self.event_dispatcher = event_dispatcher
        self.level_service = LevelService()

        # Add fond.
        self.canvas.add(
            Rectangle(source=self.FOND_MENU_PATH, size=Window.size)
        )

        # Add button
        self.add_widget(
            Button(text="Menu", font_name=self.FONT_MENU_PATH, background_color=self.color_2,
                   pos_hint={'x': 0.82, 'y': 0}, size_hint=(0.18, 0.15),
                   on_press=self.switch_to_menu_screen)
        )

        menu_level_grid = GridLayout(size_hint=(0.7, 0.45), pos_hint={'x': 0.15, 'y': 0.3}, row=2)
        self.add_widget(menu_level_grid)

        set_list = os.listdir(self.MAPS_PATH)
        set_number = len(set_list)
        menu_level_grid.cols = set_number / 2

        for index in range(1, set_number + 1):
            if index % 2 == 0:
                button_title = "Level " + str(index)
                menu_level_grid.add_widget(
                    Button(
                        text=button_title,
                        font_name=self.FONT_MENU_PATH,
                        background_color=self.color_1,
                        on_press=self.launch_level,
                        cls=[index]
                    )
                )

            else:
                button_title = "Level " + str(index)
                menu_level_grid.add_widget(
                    Button(
                        text=button_title,
                        font_name=self.FONT_MENU_PATH,
                        background_color=self.color_2,
                        on_press=self.launch_level,
                        cls=[index]
                    )
                )

    def switch_to_menu_screen(self, *_):
        """
        Required method.
        """
        Logger.info("Propagate Menu")
        propagate_event('Menu', self)

    def launch_level(self, value):
        """
        Load level.
        :param value:
        :rtype: void
        """
        set_id = value.cls[0]
        if self.level_service.can_load_set(set_id):
            propagate_event('LevelManager', self, set_id)
        else:
            open_pop_up(self, 'not_unlocked', set_id)