예제 #1
0
    def update(self, hero: Hero):
        self._frame += 1

        if self._current_animation == 'attack':
            self._image = Assets().entities['Pirate']['attack'][self.frame //
                                                                5 % 7]

            if self.frame == 5 * 7:
                self._frame = 0
                self._current_animation = 'move'
        elif self._current_animation == 'move':
            self._image = Assets().entities['Pirate']['move'][0]

        if (hero.position.x <= self.position.x < hero.position.x + 2 and
                hero.position.y - 1 <= self.position.y <= hero.position.y + 2
            ) or (self.__side == RIGHT and hero.position.x - 46 / TILE_SIZE[0]
                  <= self.position.x <= hero.position.x - 32 / TILE_SIZE[0]
                  and hero.position.y - 1 / 2 <= self.position.y <=
                  hero.position.y + 2 + 1 / 2) or (
                      self.__side == LEFT
                      and hero.position.x + 64 / TILE_SIZE[1] <=
                      self.position.x <= hero.position.x + 78 / TILE_SIZE[1]
                      and hero.position.y - 1 / 2 <= self.position.y <=
                      hero.position.y + 2 + 1 / 2):
            self.attack(hero)
        elif self._current_animation == 'move':
            self.move(hero)
예제 #2
0
    def update(self):
        self._frame += 1

        if self._current_animation == 'attack':
            self._image = Assets().hero_animations[self._current_animation][
                self.frame // 4 % FC_HERO_ATTACK]
            if self.frame == 9 or self.frame == 29 or self.frame == 49:
                Sounds().sword_swing.play()
            if self.frame == 4 * self.__attack_count:
                self._frame = 0
                self.__attack_count = 0
                self._current_animation = 'move'
                self._image = Assets().hero_animations[
                    self._current_animation][FC_HERO_MOVE * int(
                        bool(self._inventory['equipment']['arm']))]
예제 #3
0
 def render(self, screen, health):
     image = pygame.transform.scale(self.image, self.size)
     rect = image.get_rect()
     for i in range(int(health)):
         rect.topleft = self.position.x + i * (self.size[0] +
                                               4), self.position.y
         screen.blit(image, rect)
     for i in range(int(health), Hero.MAX_HEALTH):
         rect.topleft = self.position.x + i * (self.size[0] +
                                               4), self.position.y
         screen.blit(Assets().emptyHeart, rect)
     if health % 1 != 0:
         rect.topleft = self.position.x + int(health) * (self.size[0] +
                                                         4), self.position.y
         screen.blit(Assets().halfHeart, rect)
예제 #4
0
 def move(self, hero: Hero):  # add hero argument
     if (self.position.x - hero.position.x)**2 + (
             self.position.y - hero.position.y
     )**2 <= 9**2 and self.position.x // HORIZONTAL_TILES_COUNT == hero.position.x // HORIZONTAL_TILES_COUNT and self.position.y // VERTICAL_TILES_COUNT == hero.position.y // VERTICAL_TILES_COUNT:
         self.__move_to_hero(hero)
         self._image = Assets().entities['Pirate']['move'][self.frame // 5 %
                                                           2]
예제 #5
0
 def render(self, screen, active_slot, inventory):
     image = pygame.transform.scale(self.image, self.size)
     for i in range(4):
         item = list(inventory.values())[i]
         if item is not None:
             image.blit(item.image,
                        pygame.Rect((6, SLOT_SIZE[1] * (i) + 6), item.size))
     if active_slot is not None and isinstance(active_slot, str):
         slot_number = list(inventory.keys()).index(active_slot)
         image.blit(Assets().active_slot,
                    pygame.Rect((0, SLOT_SIZE[1] * slot_number), SLOT_SIZE))
     rect = image.get_rect()
     rect.topleft = self.position.x, self.position.y
     screen.blit(image, rect)
예제 #6
0
 def render(self, screen, active_slot, inventory):
     image = pygame.transform.scale(self.image, self.size)
     for i in range(1, 7):
         if inventory[i] is not None:
             image.blit(
                 inventory[i].image,
                 pygame.Rect((SLOT_SIZE[1] * (i - 1) + 6, 6),
                             inventory[i].size))
     if active_slot is not None and isinstance(active_slot, int):
         image.blit(
             Assets().active_slot,
             pygame.Rect((SLOT_SIZE[1] * (active_slot - 1), 0), SLOT_SIZE))
     rect = image.get_rect()
     rect.topleft = self.position.x, self.position.y
     screen.blit(image, rect)
예제 #7
0
 def __init__(self,
              pos=Position(0, 0),
              size=(40, 75),
              is_active=True,
              frame=0,
              color='blue',
              destination={
                  'position': Position(0, 0),
                  'level': ''
              }):
     super(Portal, self).__init__(pos, size)
     super(RenderableObject, self).__init__()
     self.is_active = is_active
     self.__color = color
     self.__destination = destination
     self._image = Assets().portals[color][0]
예제 #8
0
 def __init__(self,
              name='Pirate',
              health=15,
              damage=0.5,
              armor=0,
              pos=Position(0, 0),
              size=(32, 50),
              side=RIGHT):
     super(Pirate, self).__init__(name,
                                  health,
                                  damage,
                                  armor,
                                  pos,
                                  size=(32, 50))
     self.__side = side
     self._image = pygame.transform.scale(
         Assets().entities[self.name]['move'][0], self.size)
예제 #9
0
 def __init__(self, name='', pos=Position(0, 0), items=None):
     super().__init__(name, pos=pos)
     if items is None:
         self._items = {
             1: None,
             2: None,
             3: None,
             4: None,
             5: None,
             6: None,
             7: None,
             8: None,
             9: None
         }
     else:
         self._items = items
     self._image = pygame.transform.scale2x(Assets().bargainer)
예제 #10
0
    def __init__(self,
                 name='',
                 health=MAX_HEALTH,
                 damage=3,
                 armor=0,
                 pos=Position(0, 0),
                 money=0,
                 inventory=None,
                 effects=None,
                 side=RIGHT):
        super().__init__(name, health, damage, armor, pos)
        self._money = money
        if inventory is None:
            self._inventory = {
                'items': {
                    1: None,
                    2: None,
                    3: None,
                    4: None,
                    5: None,
                    6: None
                },
                'equipment': {
                    'head': None,
                    'chest': None,
                    'legs': None,
                    'arm': None
                }
            }
        else:
            self._inventory = inventory

        if effects is None:
            self._effects = {'strength': 0, 'invisibility': 0, 'poisoning': 0}
        else:
            self._effects = effects
        self.__attack_count = 0
        self.__side = side
        self._image = pygame.transform.scale(
            Assets().hero_animations['move'][0], HERO_SIZE)
예제 #11
0
 def image(self):
     return pygame.transform.scale2x(Assets().items[self.name])
예제 #12
0
 def __init__(self):
     super().__init__(pos=Position(25, 15), size=HEART_SIZE)
     self._image = Assets().fullHeart
예제 #13
0
 def __init__(self):
     super().__init__(pos=Position(0,
                                   (HEIGHT - EQUIPMENT_BAR_SIZE[1]) // 2),
                      size=EQUIPMENT_BAR_SIZE)
     self._image = Assets().equipment_bar
예제 #14
0
 def __init__(self):
     super().__init__(pos=Position(230, 17), size=COIN_SIZE)
     self._image = Assets().coin
예제 #15
0
    def __init__(self, opt):
        self.session_id = opt.session_id
        # Initialize camera and window
        self.cap = cv.VideoCapture(opt.input_device,
                                   eval(f'cv.{opt.input_api}'))
        assert self.cap.isOpened(), 'Failed to initialize video capture!'

        self.width, self.height = opt.video_width, opt.video_height
        self.cap.set(cv.CAP_PROP_FRAME_WIDTH, self.width)
        self.cap.set(cv.CAP_PROP_FRAME_HEIGHT, self.height)
        self.window_name = 'tutorial'
        if opt.fullscreen:
            cv.namedWindow(self.window_name, cv.WINDOW_NORMAL)
            cv.setWindowProperty(self.window_name, cv.WND_PROP_FULLSCREEN,
                                 cv.WINDOW_FULLSCREEN)
        else:
            cv.namedWindow(self.window_name)
        self.width_qr = self.width // 4
        self.width_half = self.width // 2
        self.width_3qr = self.width // 4 * 3
        self.border_margin = round(max(self.width, self.height) * 0.025)

        # Initialize openpose library
        import os
        import sys
        sys.path.append(os.fspath(opt.op_path / 'python/openpose/Release'))
        os.environ['PATH'] += f';{os.fspath(opt.op_path/"x64/Release")}'
        os.environ['PATH'] += f';{os.fspath(opt.op_path/"bin")}'

        try:
            import pyopenpose as op
        except ImportError as e:
            print('Error: OpenPose library could not be found.')
            raise e

        self._op_wrapper = op.WrapperPython()
        self._op_wrapper.configure({
            'model_folder':
            os.fspath(opt.op_path / '../models/'),
            'model_pose':
            'BODY_25',
            'number_people_max':
            1
        })
        self._op_wrapper.start()
        self._datum = op.Datum()

        # Raw data recorder
        if not opt.no_save:
            self.recorder = DataRecorder(
                (self.width, self.height), 24,
                f'records/{opt.session_id}/{int(time())}')
        else:
            self.recorder = DummyDataRecorder()

        # Handler of each state
        self.handlers = {
            State.Ready: self.handle_ready,
            State.PoseReady: self.handle_pose_ready,
            State.PoseMeasuring: self.handle_pose_measuring,
            State.Finish: self.handle_finish,
        }

        # Load assets
        self.assets = Assets('assets/')
        self.poses: List[Posture] = [
            Pose_01, Pose_02, Pose_03, Pose_04, Pose_05, Pose_06, Pose_08,
            Pose_09, Pose_10
        ]
        self.poses_ui: List[InformationLayer] = generate_info_layers(
            self.poses)

        self.running: bool = True
        self.state: State = State.Ready
        self.pose_index_iter: Iterator[int] = iter(range(len(self.poses)))
        self.current_pose_i: int = 0
        self.t_start: float = perf_counter()
        self.fail_counter = deque([False] * opt.fail_tolerance,
                                  maxlen=opt.fail_tolerance)
        self.angle: float = np.pi
        self.confidence: float = 0
        self.score: int = 0

        self.keypoints: np.ndarray = np.array([], np.float32)
        self.frame: np.ndarray = np.array([], np.uint8)
예제 #16
0
 def __init__(self):
     super().__init__(pos=Position((WIDTH - INVENTORY_BAR_SIZE[0]) // 2,
                                   (HEIGHT - INVENTORY_BAR_SIZE[1])),
                      size=INVENTORY_BAR_SIZE)
     self._image = Assets().inventory_bar
예제 #17
0
    def move(self, key, room: Room):
        def can_stand(step_x=0, step_y=0):
            if step_x != 0:
                return room.tileOn(
                    Position(
                        int(self.position.x + step_x) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y) % VERTICAL_TILES_COUNT)
                ).type != '#' and room.tileOn(
                    Position(
                        int(self.position.x + step_x) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + 1) % VERTICAL_TILES_COUNT)
                ).type != '#' and (self.position.y % 1 == 0 or (room.tileOn(
                    Position(
                        int(self.position.x + step_x) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + 1) % VERTICAL_TILES_COUNT)
                ).type != '#' and room.tileOn(
                    Position(
                        int(self.position.x + step_x) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + 2) % VERTICAL_TILES_COUNT)).type
                                                                != '#'))
            elif step_y != 0:
                return room.tileOn(
                    Position(
                        int(self.position.x) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + step_y) % VERTICAL_TILES_COUNT)
                ).type != '#' and room.tileOn(
                    Position(
                        int(self.position.x + 1) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + step_y) % VERTICAL_TILES_COUNT)
                ).type != '#' and (self.position.x % 1 == 0 or (room.tileOn(
                    Position(
                        int(self.position.x + 1) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + step_y) % VERTICAL_TILES_COUNT)
                ).type != '#' and room.tileOn(
                    Position(
                        int(self.position.x + 2) % HORIZONTAL_TILES_COUNT,
                        int(self.position.y + step_y) %
                        VERTICAL_TILES_COUNT)).type != '#'))

        self._image = Assets().hero_animations['move'][
            self.frame // 7 % FC_HERO_MOVE +
            FC_HERO_MOVE * int(bool(self._inventory['equipment']['arm']))]
        if key == pygame.K_DOWN:
            if self.position.y % VERTICAL_TILES_COUNT == VERTICAL_TILES_COUNT - HERO_SIZE_IN_TILES[
                    1]:
                self.position.y += HERO_SIZE_IN_TILES[1]
            elif can_stand(step_y=HERO_SIZE_IN_TILES[1]):
                self.position.y += 1 / 4
        elif key == pygame.K_UP:
            if self.position.y % VERTICAL_TILES_COUNT == 0:
                self.position.y -= 1
            elif can_stand(step_y=-1 / 4):
                self.position.y -= 1 / 4
        elif key == pygame.K_LEFT:
            if self.position.x % HORIZONTAL_TILES_COUNT == 0:
                self.position.x -= HERO_SIZE_IN_TILES[0]
            elif can_stand(step_x=-1 / 4):
                self.position.x -= 1 / 4
            self.__side = LEFT
        elif key == pygame.K_RIGHT:
            if self.position.x % HORIZONTAL_TILES_COUNT == HORIZONTAL_TILES_COUNT - HERO_SIZE_IN_TILES[
                    0]:
                self.position.x += HERO_SIZE_IN_TILES[0]
            elif can_stand(step_x=HERO_SIZE_IN_TILES[0]):
                self.position.x += 1 / 4
            self.__side = RIGHT
예제 #18
0
 def update(self):
     if self.is_active:
         self._frame += 1
         self._image = Assets().portals[self.color][self.frame // 5 % 9]
예제 #19
0
 def __init__(self):
     super().__init__(pos=Position(25, 42), size=SHIELD_SIZE)
     self._image = Assets().armor
예제 #20
0
 def image(self):
     return pygame.transform.scale(Assets().tiles[self._type], TILE_SIZE)