Exemple #1
0
    def game_loop(self):
        while self.game_running:
            # Let the AI play if it's its turn
            if not self.human_turn():
                start_ai_time = pygame.time.get_ticks()
                token = self.turn_token()
                if token == ConnectFourBoard.RED:
                    move = self.red_player(self, token)
                elif token == ConnectFourBoard.BLUE:
                    move = self.blue_player(self, token)
                self.attempt_insert(move)
                stop_ai_time = pygame.time.get_ticks()
                ai_time_span = stop_ai_time - start_ai_time
                if self.game_running:
                    print("Time taken for move: {}".format(ai_time_span))
                if not self.red_turn:
                    self.red_times.append(ai_time_span)
                else:
                    self.blue_times.append(ai_time_span)
                if ai_time_span < self.ai_delay:
                    pygame.time.delay(self.ai_delay - ai_time_span)

            # Process all events, especially mouse events.
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event.type == MOUSEMOTION:
                    self.selected_index = \
                        ConnectFourGraphics.hovered_col(self.board)
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    if self.human_turn():
                        self.attempt_insert(self.selected_index)

            # Refresh the display and loop back
            self.draw()
            pygame.time.wait(40)

        # Once the game is finish, simply wait for the `QUIT` event
        while True:
            event = pygame.event.wait()
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            pygame.time.wait(60)
Exemple #2
0
    def game_loop(self):
        while self.game_running:

            # Let the AI play if it's its turn
            if not self.human_turn():
                start_ai_time = pygame.time.get_ticks()
                token = self.turn_token()
                if token == ConnectFourBoard.RED:
                    move = self.red_player(self.board, token, self.red_turn)
                elif token == ConnectFourBoard.BLUE:
                    move = self.blue_player(self.board, token, self.red_turn)
                self.attempt_insert(move)
                stop_ai_time = pygame.time.get_ticks()
                ai_time_span = stop_ai_time - start_ai_time
                self.time_cumulative[token] = self.time_cumulative[token] + ai_time_span
                if ai_time_span < self.ai_delay:
                    pygame.time.delay(self.ai_delay - ai_time_span)

            # Process all events, especially mouse events.
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event.type == MOUSEMOTION:
                    self.selected_index = ConnectFourGraphics.hovered_col(self.board)
                if event.type == MOUSEBUTTONDOWN and event.button == 1:
                    if self.human_turn():
                        self.attempt_insert(self.selected_index)

            # Refresh the display and loop back
            self.draw()
            pygame.time.wait(40)

        #return ("Red" if self.winner==1 else "Blue" if self.winner==2 else "None", self.time_cumulative[1], self.time_cumulative[2])
        # Once the game is finish, simply wait for the `QUIT` event
        while True:
            event = pygame.event.wait()
            if event.type == QUIT:
                pygame.quit()
                sys.exit(0)
            pygame.time.wait(60)