예제 #1
0
    def step(self, dt):
        self._update_time_left()
        self._display_scores_and_time_left()

        try:
            self._handle_player_events()
            self._update_scores()
            self.board.draw_board(self.screen)
            self.board.draw_pieces(self.screen)
            self._display_scores_and_time_left()
            self._display_current_player()

        except utils.ValueOutOfRange:
            raise utils.ValueOutOfRange()

        except utils.InvalidAction:
            raise utils.InvalidAction()

        except utils.NoAvailableAction:
            self._update_scores()
            self.board.draw_board(self.screen)
            self.board.draw_pieces(self.screen)
            self._display_scores_and_time_left()
            self._display_current_player()
            raise utils.NoAvailableAction()
예제 #2
0
    def pos2label(self, pos):
        """
        Change the screen position to the label on the board.
        """
        pos = tuple([p - tl for p, tl in zip(pos, self.top_left)])

        if (pos[0] < 0 or pos[0] > self.side_length or
            pos[1] < 0 or pos[1] > self.side_length):
            raise utils.ValueOutOfRange()

        return self.board.pos2label(pos)
예제 #3
0
    def pos2label(self, pos):
        """
        Change the position on the board to label on the board.
        """
        if (pos[0] < 0.1 * self.side_length or pos[0] >= 0.9 * self.side_length
                or pos[1] < 0.1 * self.side_length
                or pos[1] >= 0.9 * self.side_length):
            raise utils.ValueOutOfRange()

        pos = [p - 0.1 * self.side_length for p in pos]
        x = int(pos[0] // (0.8 * self.side_length / len(self.cols)))
        y = int(pos[1] // (0.8 * self.side_length / len(self.rows)))
        return self.rows[y] + self.cols[x]
예제 #4
0
    def _handle_player_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.MOUSEMOTION:
                try:
                    if self.get_game_state()[self.board.enum[
                            self.last_label]] == 2:
                        self.board.update(self.last_label, 0)
                    label = self.pos2label(event.pos)
                    if self._is_available(label):
                        self.last_label = label
                        self.board.update(label, 2)

                except utils.ValueOutOfRange:
                    pass

            elif (event.type == pygame.MOUSEBUTTONDOWN
                  or event.type == pygame.USEREVENT):
                try:
                    label = self.pos2label(event.pos)
                    if self._is_available(label, flip=True):
                        cur_time = pygame.time.get_ticks()
                        self.time_left[
                            self.
                            cur_player] -= cur_time - self.prev_action_time
                        self.prev_action_time = cur_time

                        self.board.update(label, self.cur_player)
                        self.cur_player *= -1

                        if len(self._get_available_actions()) <= 0:
                            self.cur_player *= -1
                            raise utils.NoAvailableAction()
                    else:
                        raise utils.InvalidAction()

                except utils.ValueOutOfRange:
                    raise utils.ValueOutOfRange()