Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import pygame as pg
- import numpy as np
- pg.init()
- WIDTH = 800
- HEIGHT = WIDTH
- LINE_COLOR = (10, 52, 95)
- BACKGROUND_COLOR = (18, 212, 199)
- CIRCLE_COLOR = (255, 255, 255)
- X_COLOR = (1, 11, 10)
- BOARD_ROWs = 3
- BOARD_COLUMNs = 3
- SQUARE_SIZE = WIDTH // BOARD_ROWs
- CIRCLE_RADIUS = WIDTH / 10
- CIRCLE_WIDTH = 15
- X_WIDTH = 20
- SPACE = SQUARE_SIZE // 4
- screen = pg.display.set_mode((WIDTH, HEIGHT))
- pg.display.set_caption("Tic Tac Toe")
- screen.fill(BACKGROUND_COLOR)
- text_font = pg.font.SysFont("Arial", WIDTH // 10)
- def draw_text(text, font, text_col, x, y):
- img = font.render(text, True, text_col)
- screen.blit(img, (x, y))
- draw_text("Tic Tac Toe", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 4, HEIGHT // 2 - HEIGHT // 4)
- draw_text("Press X to play", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 3 + WIDTH // 200, HEIGHT // 2 - HEIGHT // 3 + HEIGHT // 3)
- board = np.zeros((BOARD_ROWs, BOARD_COLUMNs))
- def draw_lines_on_board(line_width):
- pg.draw.line(screen, LINE_COLOR, (SQUARE_SIZE, 0), (SQUARE_SIZE, WIDTH), line_width)
- pg.draw.line(screen, LINE_COLOR, (SQUARE_SIZE * 2, 0), (SQUARE_SIZE * 2, WIDTH), line_width)
- pg.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE), (WIDTH, SQUARE_SIZE), line_width)
- pg.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE * 2), (WIDTH, SQUARE_SIZE * 2), line_width)
- def draw_figures():
- for i in range(0, BOARD_ROWs):
- for j in range(0, BOARD_COLUMNs):
- if board[i][j] == 1:
- 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)
- 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)
- if board[i][j] == 2:
- pg.draw.circle(screen, CIRCLE_COLOR, (int(j * SQUARE_SIZE + SQUARE_SIZE // 2), int(i * SQUARE_SIZE + SQUARE_SIZE // 2)), CIRCLE_RADIUS, CIRCLE_WIDTH)
- player = 1
- game_over = True
- def mark_cell(i, j, player):
- board[i][j] = player
- def is_cell_available(i, j):
- return board[i][j] == 0
- def is_full():
- for i in range(0, BOARD_ROWs):
- for j in range(0, BOARD_COLUMNs):
- if board[i][j] == 0:
- return False
- return True
- def check_win(player):
- for j in range(0, BOARD_COLUMNs):
- if board[0][j] == player and board[1][j] == player and board[2][j] == player:
- draw_vertical_winning_line(j, player)
- return True
- for i in range(0, BOARD_ROWs):
- if board[i][0] == player and board[i][1] == player and board[i][2] == player:
- draw_horizontal_winning_line(i, player)
- return True
- if board[0][0] == player and board[1][1] == player and board[2][2] == player:
- draw_asc_diagonal(player)
- return True
- if board[0][2] == player and board[1][1] == player and board[2][0] == player:
- draw_desc_diagonal(player)
- return True
- return False
- def draw_vertical_winning_line(j, player):
- pos = j * SQUARE_SIZE + SQUARE_SIZE // 2
- if player == 1:
- color = X_COLOR
- else:
- color = CIRCLE_COLOR
- pg.draw.line(screen, color, (pos, 15), (pos, HEIGHT - 15), 15)
- def draw_horizontal_winning_line(i, player):
- pos = i * SQUARE_SIZE + SQUARE_SIZE // 2
- if player == 1:
- color = X_COLOR
- else:
- color = CIRCLE_COLOR
- pg.draw.line(screen, color, (15, pos), (WIDTH - 15, pos), 15)
- def draw_desc_diagonal(player):
- if player == 1:
- color = X_COLOR
- else:
- color = CIRCLE_COLOR
- pg.draw.line(screen, color, (15, HEIGHT - 15), (WIDTH - 15, 15), 15)
- def draw_asc_diagonal(player):
- if player == 1:
- color = X_COLOR
- else:
- color = CIRCLE_COLOR
- pg.draw.line(screen, color, (15, 15), (WIDTH - 15, HEIGHT - 15), 15)
- def restart():
- screen.fill(BACKGROUND_COLOR)
- draw_lines_on_board(15)
- for i in range(0, BOARD_ROWs):
- for j in range(0, BOARD_COLUMNs):
- board[i][j] = 0;
- not_game_started= True
- while True:
- for event in pg.event.get():
- if event.type == pg.QUIT:
- exit(0)
- if event.type == pg.KEYDOWN and not_game_started:
- if event.key == pg.K_x:
- restart()
- game_over = False
- player = 1
- not_game_started = False
- if event.type == pg.MOUSEBUTTONDOWN and not game_over:
- mouse_x = event.pos[0]
- mouse_y = event.pos[1]
- square_size = WIDTH / 3
- clicked_row = int(mouse_y // square_size)
- clicked_col = int(mouse_x // square_size)
- if is_cell_available(clicked_row, clicked_col):
- mark_cell(clicked_row, clicked_col, player)
- if check_win(player):
- game_over = True
- player = 3 - player
- print(board)
- draw_figures()
- if event.type == pg.KEYDOWN:
- if event.key == pg.K_r:
- restart()
- player = 1
- game_over = False
- available_cells = 0
- for i in range(0, BOARD_ROWs):
- for j in range(0, BOARD_COLUMNs):
- if board[i][j] == 0:
- available_cells += 1
- if available_cells == 0 and not check_win(player):
- screen.fill(BACKGROUND_COLOR)
- draw_text("DRAW", text_font, (0, 0, 0), WIDTH // 2 - WIDTH // 6, HEIGHT // 2 - HEIGHT // 3)
- draw_text("Press R to restart", text_font, (0, 0, 0), WIDTH // 9,
- HEIGHT // 2 - HEIGHT // 6)
- if event.type == pg.KEYDOWN:
- if event.key == pg.K_r:
- restart()
- player = 1
- game_over = False
- available_cells = BOARD_ROWs * BOARD_COLUMNs
- pg.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement