Example #1
0
    def load_level(self, fname):
        self.auto_play = None
        edit_level = EditableLevel()
        level = Level()
        try:
            edit_level.load_level(fname)
        except IOError as e:
            self._show_error(str(e), "Cannot load map")
            return

        try:
            level.init_from_level(edit_level)
        except (IOError, ValueError) as e:
            if self.mode != "edit":
                self._show_error(str(e), "Cannot play map!")
                return
            level = None

        self._game_state = "stopped"
        self.edit_level = edit_level
        self.edit_mctrl.level = edit_level

        self.play_ctrl.edit_level = edit_level

        if level:
            self.level = level
            self.level.add_event_listener(self.game_event)
            self.play_ctrl.level = level
            self.play_mctrl.level = level
            sc = functools.partial(self.score.update_score, self.level)

        lvl = edit_level
        grid = True
        if self.mode == "play":
            lvl = level
            grid = False

        self.game_window.use_level(lvl, grid=grid)
        if level:
            level.add_event_listener(sc)
            # must be done after game_window.use_level
            level.start()
            self._game_state = "running"
Example #2
0
    def new_map(self):
        self.new_lvl_d.close()
        res = self.new_lvl_d.value
        edit_level = self.edit_level
        can_rel = True
        trans = None

        if edit_level is None:
            can_rel = False
            edit_level = EditableLevel()

        def _compute_value(name, s):
            res = 0
            if s and (s[0] == "+" or s[0] == "-" or s == "0"):
                # +x or -x or 0 is assumed to be relative (the latter being
                # short for "+0")
                if not can_rel:
                    raise ValueError("Cannot do relative size based on non-existent map: %s=%s" \
                                         % (name, s))
                if name == "width":
                    return edit_level.width + int(s)
                return edit_level.height + int(s)
            else:
                return int(s.lstrip("="))

        try:
            width = _compute_value("width", res["width"].value)
            height = _compute_value("height", res["height"].value)
            clear = res["clear"].value
            if not clear:
                trans = Position(int(res["trans-width"].value),
                                 int(res["trans-height"].value))
        except (TypeError, ValueError) as e:
            self._show_error(str(e), "Cannot create map")
            return

        edit_level.new_map(width, height, translate=trans)

        self.edit_level = edit_level
        self.play_ctrl.edit_level = edit_level
        self.edit_mctrl.level = edit_level
        self.game_window.use_level(edit_level, grid=True)