Advertisement
Josif_tepe

Untitled

Nov 2nd, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. import pygame as pg
  2. import numpy as np
  3. pg.init()
  4. WIDTH = 600
  5. HEIGHT = 600
  6. LINE_COLOR = (10, 52, 95)
  7. BACKGROUND_COLOR = (18, 212, 199)
  8. CIRCLE_COLOR = (255, 255, 255)
  9. X_COLOR = (1, 11, 10)
  10. BOARD_ROWs = 3
  11. BOARD_COLUMNs = 3
  12. CIRCLE_RADIUS = 60
  13. CIRCLE_WIDTH = 15
  14. X_WIDTH = 20
  15. SPACE = 43
  16. screen = pg.display.set_mode((WIDTH, HEIGHT))
  17. pg.display.set_caption("Tic Tac Toe")
  18. screen.fill(BACKGROUND_COLOR)
  19.  
  20. board = np.zeros((BOARD_ROWs, BOARD_COLUMNs))
  21.  
  22. def draw_lines_on_board(line_width):
  23.     pg.draw.line(screen, LINE_COLOR, (200, 0), (200, 600), line_width)
  24.     pg.draw.line(screen, LINE_COLOR, (400, 0), (400, 600), line_width)
  25.     pg.draw.line(screen, LINE_COLOR, (0, 200), (600, 200), line_width)
  26.     pg.draw.line(screen, LINE_COLOR, (0, 400), (600, 400), line_width)
  27.  
  28. def draw_figures():
  29.     for i in range(0, BOARD_ROWs):
  30.         for j in range(0, BOARD_COLUMNs):
  31.             if board[i][j] == 1:
  32.                 pg.draw.line(screen, X_COLOR, (j * 200 + SPACE, i * 200 + 200 - SPACE), (j * 200 + 200 - SPACE, i * 200 + SPACE), X_WIDTH)
  33.                 pg.draw.line(screen, X_COLOR, (j * 200 + SPACE, i * 200 + SPACE), (j * 200 + 200 - SPACE, i * 200 + 200 - SPACE), X_WIDTH)
  34.             if board[i][j] == 2:
  35.                 pg.draw.circle(screen, CIRCLE_COLOR, (int(j * 200 + 100), int(i * 200 + 100)), CIRCLE_RADIUS, CIRCLE_WIDTH)
  36.  
  37. def mark_cell(i, j, player):
  38.     board[i][j] = player
  39.  
  40. def is_cell_available(i, j):
  41.     return board[i][j] == 0
  42.  
  43. def is_full():
  44.     for i in range(0, BOARD_ROWs):
  45.         for j in range(0, BOARD_COLUMNs):
  46.             if board[i][j] == 0:
  47.                 return False
  48.     return True
  49.  
  50. def check_win(player):
  51.     for j in range(0, BOARD_COLUMNs):
  52.         if board[0][j] == player and board[1][j] == player and board[2][j] == player:
  53.             draw_vertical_winning_line(j, player)
  54.             return True
  55.  
  56.     for i in range(0, BOARD_ROWs):
  57.         if board[i][0] == player and board[i][1] == player and board[i][2] == player:
  58.             draw_horizontal_winning_line(i, player)
  59.             return True
  60.  
  61.     if board[0][0] == player and board[1][1] == player and board[2][2] == player:
  62.         draw_asc_diagonal(player)
  63.         return True
  64.  
  65.     if board[0][2] == player and board[1][1] == player and board[2][0] == player:
  66.         draw_desc_diagonal(player)
  67.         return True
  68.  
  69.     return False
  70.  
  71. def draw_vertical_winning_line(j, player):
  72.     pos = j * 200 + 100
  73.  
  74.     if player == 1:
  75.         color = X_COLOR
  76.     else:
  77.         color = CIRCLE_COLOR
  78.  
  79.     pg.draw.line(screen, color, (pos, 15), (pos, HEIGHT - 15), 15)
  80.  
  81. def draw_horizontal_winning_line(i, player):
  82.     pos = i * 200 + 100
  83.  
  84.     if player == 1:
  85.         color = X_COLOR
  86.     else:
  87.         color = CIRCLE_COLOR
  88.  
  89.     pg.draw.line(screen, color, (15, pos), (WIDTH - 15, pos), 15)
  90.  
  91. def draw_desc_diagonal(player):
  92.     if player == 1:
  93.         color = X_COLOR
  94.     else:
  95.         color = CIRCLE_COLOR
  96.  
  97.     pg.draw.line(screen, color, (15, HEIGHT - 15), (WIDTH - 15, 15), 15)
  98.  
  99. def draw_asc_diagonal(player):
  100.     if player == 1:
  101.         color = X_COLOR
  102.     else:
  103.         color = CIRCLE_COLOR
  104.  
  105.     pg.draw.line(screen, color, (15, 15), (WIDTH - 15, HEIGHT - 15), 15)
  106. draw_lines_on_board(15)
  107. player = 1
  108. game_over = False
  109. while True:
  110.     for event in pg.event.get():
  111.         if event.type == pg.QUIT:
  112.             exit(0)
  113.  
  114.         if event.type == pg.MOUSEBUTTONDOWN and not game_over:
  115.             mouse_x = event.pos[0]
  116.             mouse_y = event.pos[1]
  117.  
  118.             clicked_row = int(mouse_y // 200)
  119.             clicked_col = int(mouse_x // 200)
  120.  
  121.             if is_cell_available(clicked_row, clicked_col):
  122.                 mark_cell(clicked_row, clicked_col, player)
  123.                 if check_win(player):
  124.                     game_over = True
  125.                 player = 3 - player
  126.             print(board)
  127.             draw_figures()
  128.  
  129.     pg.display.update()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement