예제 #1
0
파일: engine.py 프로젝트: lexatnet/school
def processing(obj, objects):

  collisions = get_collisions(obj, objects)

  forces = get_forces(obj, collisions)

  force = vector_sum(forces)

  speed = (
    obj['physics']['speed'][0]+force[0]/obj['physics']['weight'],
    obj['physics']['speed'][1]+force[1]/obj['physics']['weight']
  )

  obj['physics']['speed'] = speed

  current_position = obj['body'].position()

  next_position = (
    current_position[0]+speed[0],
    current_position[1]+speed[1]
  )

  # point_from = Point(current_position[0], current_position[1])
  # point_to = Point(next_position[0], next_position[1])
  # track = LineString(point_from, point_to)

  obj['body'].goto(next_position[0], next_position[1])
예제 #2
0
def standardize(inputs):
    """Standardize features according to (x - u) / s where u is mean and s
    is standard deviation.

    Parameters
    ----------
    inputs : list
        A list of lists of the dimension m * n where m is the number of
        samples and n is the number of features each sample is described.

    Returns
    -------
    list
        Standardized features.

    """
    mean_vector = utils.vector_mean(inputs)
    squares = []
    for p in inputs:
        squares.append(
            [(val - mean_vector[i]) ** 2 for i, val in enumerate(p)]
        )
    stds = [math.sqrt(x) for x in utils.scalar_multiply(1/len(
        squares), utils.vector_sum(squares))]
    standardized_inputs = []
    for p in inputs:
        standardized_inputs.append(
            [(val - mean_vector[i]) / stds[i] for i, val in enumerate(p)]
        )
    return standardized_inputs
    def move(self, direction, active_map):
        """
        Attempts to move player in specified direction
        :param direction: String containing direction character: U, L, R, D
        :param active_map: DungeonMap instance
        :return: Bool, if player moved to new location
        """

        logger.debug(
            f"Attempting to move player at {self.position} to {direction}...")

        direction = direction.upper()

        position_offset = None
        is_position_changed = False

        if direction == "U":
            position_offset = (-1, 0)
        elif direction == "L":
            position_offset = (0, -1)
        elif direction == "R":
            position_offset = (0, +1)
        elif direction == "D":
            position_offset = (+1, 0)

        if active_map.is_index_valid(
                utils.vector_sum(self.position, position_offset)):

            position = utils.vector_sum(self.position, position_offset)

            if active_map.game_map[position[0]][
                    position[1]] == DungeonMap.SYMBOL_TILE:
                active_map.game_map[position[0]][
                    position[1]] = DungeonMap.SYMBOL_PLAYER

            self.lock_position.acquire()
            self.position = position
            self.lock_position.release()

            is_position_changed = True
            logger.debug("Player moved successfully")

        else:
            logger.debug(
                f"Invalid position, couldn't move player to {direction}")

        return is_position_changed
    def check_nearby_tiles(self, position):
        """
        Checks if any traps or treasures exist in one tile radius from player (ignoring diagonal tile)
        :param position: Player position
        :return: Tuple containing two bool values, if there is a trap nearby, and if there is treasure nearby
        """

        logger.info("Checking nearby tiles...")

        offset_left = (0, -1)
        offset_right = (0, +1)
        offset_up = (-1, 0)
        offset_down = (+1, 0)

        offsets = [offset_left, offset_right, offset_up, offset_down]
        offsets = [
            utils.vector_sum(position, offset) for offset in offsets
            if self.is_index_valid(utils.vector_sum(position, offset))
        ]

        is_trap_nearby = False
        is_treasure_nearby = False

        for offset in offsets:

            if self.game_map[offset[0]][offset[1]] == DungeonMap.SYMBOL_TRAP:

                is_trap_nearby = True
                logger.debug(f"Nearby tile at {offset} contains trap")

            if self.game_map[offset[0]][
                    offset[1]] == DungeonMap.SYMBOL_TREASURE:

                is_treasure_nearby = True
                logger.debug(f"Nearby tile at {offset} contains treasure")

            if is_treasure_nearby and is_trap_nearby:
                break

        return is_trap_nearby, is_treasure_nearby
    def move(self, active_map):
        """
        Moves enemy to random tile
        :param active_map: DungeonMap instance
        :return: None
        """

        directions = ["U", "L", "R", "D"]

        while True:

            direction = random.choice(directions)
            position_offset = None

            if direction == "U":
                position_offset = (-1, 0)
            elif direction == "L":
                position_offset = (0, -1)
            elif direction == "R":
                position_offset = (0, +1)
            elif direction == "D":
                position_offset = (+1, 0)

            if active_map.is_index_valid(
                    utils.vector_sum(self.position, position_offset)):

                self.position = utils.vector_sum(self.position,
                                                 position_offset)
                logger.debug(f"Enemy moved to {self.position}")

                if self.attempt_attack_player():
                    self.reset()

                break

            else:
                directions.remove(direction)
예제 #6
0
파일: engine.py 프로젝트: lexatnet/school
 def f():
   player['physics']['speed'] = vector_sum([
     player['physics']['speed'],
     # сила прыжка
     force
   ])