コード例 #1
0
ファイル: sprites.py プロジェクト: bryceharrington/apx
    def move(self, poly):
        if self.frozen:
            return

        dot = (self.x, self.y)

        # check if we are still on the new poly, because if we are not, then
        # we will keep walking the old one until we get back on track
        if game_utils.on_line(dot, poly):
            self._polys_stack = [poly]
        else:
            if poly not in self._polys_stack:
                self._polys_stack.append(poly)

                if len(self._polys_stack) == 2:
                    self.show_confusion()

            # we go from the freshest to oldest poly to see if we can find ourselves
            for i, p in enumerate(reversed(self._polys_stack)):
                if game_utils.on_line(dot, p):
                    poly = p
                    break

        if not poly:
            return

        dot2 = None
        if dot in poly:
            dot2 = self.next(dot, poly)
        else:
            line = game_utils.on_line(dot, poly)
            if not line:
                return
            dot2 = line[1] if self.clockwise else line[0]


        # distance is sum because one of them will be the same

        speed = self.speed
        while speed > 0:
            distance = game_utils.distance(dot, dot2)
            direction = 1 if any ((a<b for a, b in zip(dot, dot2))) else -1

            step_speed = min(speed, distance)

            if dot[0] == dot2[0]:
                # vertical movement
                self.y += step_speed * direction
            else:
                # horizontal movement
                self.x += step_speed * direction

            distance = distance - step_speed
            if distance == 0:
                dot, dot2 = dot2, self.next(dot2, poly)

            speed = speed - step_speed

        self.current_line = game_utils.on_line((self.x, self.y), poly)
コード例 #2
0
ファイル: board.py プロジェクト: bryceharrington/apx
    def _handle_keys(self, keys_down, game):
        x, y = self.cube.x, self.cube.y


        # if we have direction, we know that user wants to do something
        # when drawing hasn't been initiated, user can move around the game poly
        # moving around game poly means the dot is on the line at any given time


        # when drawing is started, user can move all over the place but can't bump
        # into the current drawing polygon
        # path is closed if the move ends up on the game poly
        #

        # both cases we will want to adjust the new_x or new direction so that
        # it is somewhere on the line
        game_poly_dot, non_game_poly_dot = None, None


        current_pos = (self.cube.x, self.cube.y)
        if current_pos in self.game_area:
            current_line = [game_utils.prev_dot(current_pos, self.game_area),
                            current_pos,
                            game_utils.next_dot(current_pos, self.game_area)]
        else:
            current_line = game_utils.on_line(current_pos, self.game_area)

        key_directions = {
            gdk.KEY_Left: "left",
            gdk.KEY_j: "left",
            gdk.KEY_J: "left",

            gdk.KEY_Right: "right",
            gdk.KEY_l: "right",
            gdk.KEY_L: "right",

            gdk.KEY_Up: "up",
            gdk.KEY_i: "up",
            gdk.KEY_I: "up",

            gdk.KEY_Down: "down",
            gdk.KEY_k: "down",
            gdk.KEY_K: "down",
        }

        direction = None
        for keyval in reversed(keys_down):
            if keyval in key_directions:
                direction = key_directions[keyval]
                break

        if not direction:
            return
        speed_direction = 1 if direction in ("right", "down") else -1

        # find the furthest step we can take for the next game polygon dot
        # when not drawing that's as far as we can go on current line
        # when drawing, that's the closest border within step
        for speed in reversed(range(self.cube.speed)):
            game_x = x if direction in ("up", "down") else x + (speed + 1) * speed_direction
            game_y = y if direction in ("left", "right") else y + (speed + 1) * speed_direction

            if current_line:
                if not game_poly_dot and game_utils.on_line((game_x, game_y), current_line):
                    # if we are on a line then we are looking for the max position at
                    # which we still have a hit
                    game_poly_dot = (game_x, game_y)
            else:
                # roaming around - checking if the next move is valid
                if game_utils.on_line((game_x, game_y), self.game_area):
                    game_poly_dot = (game_x, game_y)



        # look for the furthest valid non-game poly
        # we stop when we find non-poly point and then bump into an on-line point
        for speed in range(self.cube.speed):
            game_x = x if direction in ("up", "down") else x + (speed + 1) * speed_direction
            game_y = y if direction in ("left", "right") else y + (speed + 1) * speed_direction

            on_line = game_utils.on_line((game_x, game_y), self.game_area)

            if not self.in_game_bounds((game_x, game_y)):
                break

            if not on_line:
                non_game_poly_dot = (game_x, game_y)
            elif on_line and non_game_poly_dot:
                break


        space_down = any([key in keys_down for key in (gdk.KEY_space, gdk.KEY_Shift_L, gdk.KEY_Shift_R)])
        if space_down and non_game_poly_dot and not game.claiming:
            if game_utils.in_area(non_game_poly_dot, self.game_rects, on_line=True):
                game.claiming = True
                self.cube.set_drawing(True)
                self._current_direction = None


        if game.claiming and self._current_direction != direction:
            self._current_direction = direction
            if (self.cube.x, self.cube.y) not in self.current_polygon:
                self.current_polygon.append((self.cube.x, self.cube.y))


        # when we are drawing, we can move outside the claimed poly's
        # when we are moving around, we can move only within the poly
        # check if the move is valid
        # we will go through all lines and check that our path is not
        # intersecting
        if game.claiming and game_poly_dot:
            self.close_claim(self.current_polygon + [game_poly_dot],
                             self.cube.current_speed,
                             game)
            game.claiming = False
            self.cube.set_drawing(False)
            self.current_polygon = []
            self.current_polygon_path.points = []
            self.cube.x, self.cube.y = game_poly_dot
            self.cube.current_line = game_utils.on_line(game_poly_dot, self.game_area)
            return


        on_current_poly = non_game_poly_dot and game_utils.on_line(non_game_poly_dot, self.current_polygon) is not None
        if game.claiming and not on_current_poly and non_game_poly_dot:
            if game_utils.in_area(non_game_poly_dot, self.game_rects, on_line=True):
                self.cube.x, self.cube.y = non_game_poly_dot
                self.current_polygon_path.points = self.current_polygon + [non_game_poly_dot]

        if not game.claiming and game_poly_dot:
            # legal move is one where the next dot is on the same line as the prev one
            line = game_utils.on_line([self.cube.x, self.cube.y], self.game_area)
            new_line, good_lines = None, []
            if line:
                prev1, prev2 = line
                lines = [(dot1, dot2) for dot1, dot2 in zip(self.game_area, self.game_area[1:])]
                lines.append((self.game_area[-1], self.game_area[0]))
                good_lines = [line for line in lines if prev1 in line or prev2 in line]
                new_line = game_utils.on_line(game_poly_dot, self.game_area)


            if new_line in good_lines:
                self.cube.x, self.cube.y = game_poly_dot
                self.cube.current_line = new_line