Advertisement
Josif_tepe

Untitled

Nov 16th, 2024
74
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.00 KB | None | 1 0
  1. import time
  2.  
  3. import pygame as pg
  4. import numpy as np
  5. pg.init()
  6. WIDTH = 800
  7. HEIGHT = WIDTH
  8. LINE_COLOR = (10, 52, 95)
  9. BACKGROUND_COLOR = (18, 212, 199)
  10. CIRCLE_COLOR = (255, 255, 255)
  11. X_COLOR = (1, 11, 10)
  12. BOARD_ROWs = 3
  13. BOARD_COLUMNs = 3
  14. SQUARE_SIZE = WIDTH // BOARD_ROWs
  15. CIRCLE_RADIUS = WIDTH / 10
  16. CIRCLE_WIDTH = 15
  17. X_WIDTH = 20
  18. SPACE = SQUARE_SIZE // 4
  19. screen = pg.display.set_mode((WIDTH, HEIGHT))
  20. pg.display.set_caption("Tic Tac Toe")
  21. screen.fill(BACKGROUND_COLOR)
  22.  
  23. text_font = pg.font.SysFont("Arial", WIDTH // 10)
  24. def draw_text(text, font, text_col, x, y):
  25.     img = font.render(text, True, text_col)
  26.     screen.blit(img, (x, y))
  27.  
  28.  
  29. draw_text("Tic Tac Toe", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 4, HEIGHT // 2 - HEIGHT // 4)
  30. draw_text("Press X to play", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 3 + WIDTH // 200, HEIGHT // 2 - HEIGHT // 3 + HEIGHT // 3)
  31. board = np.zeros((BOARD_ROWs, BOARD_COLUMNs))
  32.  
  33. def draw_lines_on_board(line_width):
  34.     pg.draw.line(screen, LINE_COLOR, (SQUARE_SIZE, 0), (SQUARE_SIZE, WIDTH), line_width)
  35.     pg.draw.line(screen, LINE_COLOR, (SQUARE_SIZE * 2, 0), (SQUARE_SIZE * 2, WIDTH), line_width)
  36.     pg.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE), (WIDTH, SQUARE_SIZE), line_width)
  37.     pg.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE * 2), (WIDTH, SQUARE_SIZE * 2), line_width)
  38.  
  39. def draw_figures():
  40.     for i in range(0, BOARD_ROWs):
  41.         for j in range(0, BOARD_COLUMNs):
  42.             if board[i][j] == 1:
  43.                 pg.draw.line(screen, X_COLOR, (j * SQUARE_SIZE + SPACE, i * SQUARE_SIZE + SQUARE_SIZE - SPACE), (j * SQUARE_SIZE + SQUARE_SIZE - SPACE, i * SQUARE_SIZE + SPACE), X_WIDTH)
  44.                 pg.draw.line(screen, X_COLOR, (j * SQUARE_SIZE + SPACE, i * SQUARE_SIZE + SPACE), (j * SQUARE_SIZE + SQUARE_SIZE - SPACE, i * SQUARE_SIZE + SQUARE_SIZE - SPACE), X_WIDTH)
  45.             if board[i][j] == 2:
  46.                 pg.draw.circle(screen, CIRCLE_COLOR, (int(j * SQUARE_SIZE + SQUARE_SIZE // 2), int(i * SQUARE_SIZE + SQUARE_SIZE // 2)), CIRCLE_RADIUS, CIRCLE_WIDTH)
  47. player = 1
  48. game_over = True
  49. def mark_cell(i, j, player):
  50.     board[i][j] = player
  51.  
  52. def is_cell_available(i, j):
  53.     return board[i][j] == 0
  54.  
  55. def is_full():
  56.     for i in range(0, BOARD_ROWs):
  57.         for j in range(0, BOARD_COLUMNs):
  58.             if board[i][j] == 0:
  59.                 return False
  60.     return True
  61.  
  62. def check_win(player):
  63.     for j in range(0, BOARD_COLUMNs):
  64.         if board[0][j] == player and board[1][j] == player and board[2][j] == player:
  65.             draw_vertical_winning_line(j, player)
  66.             return True
  67.  
  68.     for i in range(0, BOARD_ROWs):
  69.         if board[i][0] == player and board[i][1] == player and board[i][2] == player:
  70.             draw_horizontal_winning_line(i, player)
  71.             return True
  72.  
  73.     if board[0][0] == player and board[1][1] == player and board[2][2] == player:
  74.         draw_asc_diagonal(player)
  75.         return True
  76.  
  77.     if board[0][2] == player and board[1][1] == player and board[2][0] == player:
  78.         draw_desc_diagonal(player)
  79.         return True
  80.  
  81.     return False
  82.  
  83. def draw_vertical_winning_line(j, player):
  84.     pos = j * SQUARE_SIZE + SQUARE_SIZE // 2
  85.  
  86.     if player == 1:
  87.         color = X_COLOR
  88.     else:
  89.         color = CIRCLE_COLOR
  90.  
  91.     pg.draw.line(screen, color, (pos, 15), (pos, HEIGHT - 15), 15)
  92.  
  93. def draw_horizontal_winning_line(i, player):
  94.     pos = i * SQUARE_SIZE + SQUARE_SIZE // 2
  95.  
  96.     if player == 1:
  97.         color = X_COLOR
  98.     else:
  99.         color = CIRCLE_COLOR
  100.  
  101.     pg.draw.line(screen, color, (15, pos), (WIDTH - 15, pos), 15)
  102.  
  103. def draw_desc_diagonal(player):
  104.     if player == 1:
  105.         color = X_COLOR
  106.     else:
  107.         color = CIRCLE_COLOR
  108.  
  109.     pg.draw.line(screen, color, (15, HEIGHT - 15), (WIDTH - 15, 15), 15)
  110.  
  111. def draw_asc_diagonal(player):
  112.     if player == 1:
  113.         color = X_COLOR
  114.     else:
  115.         color = CIRCLE_COLOR
  116.  
  117.     pg.draw.line(screen, color, (15, 15), (WIDTH - 15, HEIGHT - 15), 15)
  118. def restart():
  119.     screen.fill(BACKGROUND_COLOR)
  120.     draw_lines_on_board(15)
  121.     for i in range(0, BOARD_ROWs):
  122.         for j in range(0, BOARD_COLUMNs):
  123.             board[i][j] = 0;
  124.  
  125. not_game_started= True
  126.  
  127. while True:
  128.     for event in pg.event.get():
  129.         if event.type == pg.QUIT:
  130.             exit(0)
  131.         if event.type == pg.KEYDOWN and not_game_started:
  132.             if event.key == pg.K_x:
  133.                 restart()
  134.                 game_over = False
  135.                 player = 1
  136.                 not_game_started = False
  137.  
  138.         if event.type == pg.MOUSEBUTTONDOWN and not game_over:
  139.             mouse_x = event.pos[0]
  140.             mouse_y = event.pos[1]
  141.  
  142.             square_size = WIDTH / 3
  143.             clicked_row = int(mouse_y // square_size)
  144.             clicked_col = int(mouse_x // square_size)
  145.  
  146.             if is_cell_available(clicked_row, clicked_col):
  147.                 mark_cell(clicked_row, clicked_col, player)
  148.                 if check_win(player):
  149.                     game_over = True
  150.                 player = 3 - player
  151.             print(board)
  152.             draw_figures()
  153.  
  154.         if event.type == pg.KEYDOWN:
  155.             if event.key == pg.K_r:
  156.                 restart()
  157.                 player = 1
  158.                 game_over = False
  159.  
  160.         available_cells = 0
  161.         for i in range(0, BOARD_ROWs):
  162.             for j in range(0, BOARD_COLUMNs):
  163.                 if board[i][j] == 0:
  164.                     available_cells += 1
  165.  
  166.         if available_cells == 0 and not check_win(player):
  167.             screen.fill(BACKGROUND_COLOR)
  168.             draw_text("DRAW", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 6, HEIGHT // 2 - HEIGHT // 3)
  169.             draw_text("Press R to restart", text_font, (0, 0, 0), WIDTH // 9,
  170.                       HEIGHT // 2 - HEIGHT // 6)
  171.             if event.type == pg.KEYDOWN:
  172.                 if event.key == pg.K_r:
  173.                     restart()
  174.                     player = 1
  175.                     game_over = False
  176.                     available_cells = BOARD_ROWs * BOARD_COLUMNs
  177.  
  178.  
  179.     pg.display.update()
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement