Beispiel #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()
Beispiel #2
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()