if posX < len(gameBoard.game_map[0]) * cycleRadius * 2: arrowNum = int(posX / (cycleRadius * 2)) for i in range(len(arrowsColor)): if arrowNum != i: arrowsColor[i] = 0 else: arrowsColor[i] = currentPlayer.number else: arrowsColor = [0] * len(gameBoard.game_map[0]) if event.type == pygame.MOUSEBUTTONDOWN: clickX = event.pos[0] if clickX < len(gameBoard.game_map[0]) * cycleRadius * 2: try: colInput = int(clickX / (cycleRadius * 2)) gameBoard.drop_piece(currentPlayer.number, colInput) arrowsColor = [0] * len(gameBoard.game_map[0]) dropped = True except Exception as exception: print(exception) else: # currentPlayer == AIPlayer col = currentPlayer.get_drop_input(gameBoard.game_map) gameBoard.drop_piece(currentPlayer.number, col) dropped = True refresh_screen() if dropped: if gameBoard.check_win(currentPlayer.number): break
def run_game(): global currentPlayer global gameBoard global game_running game_running = True currentPlayer = player1 fake_wait = 0 dropped_count = 0 gameBoard = GameBoard() while dropped_count != allSlots: dropped = False if type(currentPlayer) == Person: for ev in pygame.event.get(): if ev.type == pygame.QUIT: sys.exit() if ev.type == pygame.MOUSEBUTTONDOWN: if ev.pos[0] < COLUMN_COUNT * cycleRadius * 2: try: input_column = int(ev.pos[0] / (cycleRadius * 2)) gameBoard.drop_piece(currentPlayer.number, input_column) dropped = True except Exception as exception: print(exception) else: # currentPlayer == AIPlayer for ev in pygame.event.get(): if ev.type == pygame.QUIT: sys.exit() fake_wait += 1 if fake_wait == AI_FAKE_TIME: input_column = currentPlayer.get_drop_input(gameBoard.game_map) gameBoard.drop_piece(currentPlayer.number, input_column) dropped = True refresh_screen() if dropped: fake_wait = 0 if gameBoard.check_win(currentPlayer.number): break if currentPlayer == player1: currentPlayer = player2 else: currentPlayer = player1 dropped_count += 1 clock.tick(30) if dropped_count == allSlots: label = finalFont.render("Game draw", True, WHITE) elif currentPlayer == player1: label = finalFont.render("Player 1 won !!", True, LIGHT_BLUE) else: label = finalFont.render("Player 2 won !!", True, LIGHT_RED) screen.blit(label, (40, ROW_COUNT * cycleRadius)) pygame.display.update() pygame.time.wait(1000) game_running = False