gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Create a tic-tac-toe using pygame
- User
Create a tic-tac-toe using pygame
- Assistant
Create a tic-tac-toe using pygame
Creating a Tic-Tac-Toe game in Pygame involves setting up the window, drawing the board, and implementing the game logic. Below is a simple implementation of a two-player Tic-Tac-Toe game using Pygame.
First, make sure you have Pygame installed. If you haven't installed it yet, you can do so using pip:
pip install pygame
Here is a basic script to create a Tic-Tac-Toe game:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 300, 300
LINE_WIDTH = 15
BOARD_ROWS = 3
BOARD_COLUMNS = 3
SQUARE_SIZE = WIDTH // BOARD_COLUMNS
CIRCLE_RADIUS = SQUARE_SIZE // 3
CIRCLE_WIDTH = 15
CROSS_WIDTH = 25
SPACE = SQUARE_SIZE // 4
# Colors
BG_COLOR = (28, 170, 156)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (66, 66, 66)
# Screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tic Tac Toe')
screen.fill(BG_COLOR)
# Board
board = [[0 for _ in range(BOARD_COLUMNS)] for _ in range(BOARD_ROWS)]
def draw_lines():
# Horizontal lines
pygame.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE), (WIDTH, SQUARE_SIZE), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (0, 2 * SQUARE_SIZE), (WIDTH, 2 * SQUARE_SIZE), LINE_WIDTH)
# Vertical lines
pygame.draw.line(screen, LINE_COLOR, (SQUARE_SIZE, 0), (SQUARE_SIZE, HEIGHT), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (2 * SQUARE_SIZE, 0), (2 * SQUARE_SIZE, HEIGHT), LINE_WIDTH)
def draw_figures():
for row in range(BOARD_ROWS):
for col in range(BOARD_COLUMNS):
if board[row][col] == 1:
pygame.draw.circle(screen, CIRCLE_COLOR, (int(col * SQUARE_SIZE + SQUARE_SIZE // 2), int(row * SQUARE_SIZE + SQUARE_SIZE // 2)), CIRCLE_RADIUS, CIRCLE_WIDTH)
elif board[row][col] == 2:
pygame.draw.line(screen, CROSS_COLOR, (col * SQUARE_SIZE + SPACE, row * SQUARE_SIZE + SQUARE_SIZE - SPACE), (col * SQUARE_SIZE + SQUARE_SIZE - SPACE, row * SQUARE_SIZE + SPACE), CROSS_WIDTH)
pygame.draw.line(screen, CROSS_COLOR, (col * SQUARE_SIZE + SPACE, row * SQUARE_SIZE + SPACE), (col * SQUARE_SIZE + SQUARE_SIZE - SPACE, row * SQUARE_SIZE + SQUARE_SIZE - SPACE), CROSS_WIDTH)
def check_winner(player):
# Vertical win check
for col in range(BOARD_COLUMNS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player:
draw_vertical_winning_line(col, player)
return True
# Horizontal win check
for row in range(BOARD_ROWS):
if board[row][0] == player and board[row][1] == player and board[row][2] == player:
draw_horizontal_winning_line(row, player)
return True
# Ascending diagonal win check
if board[2][0] == player and board[1][1] == player and board[0][2] == player:
draw_asc_diagonal(player)
return True
# Descending diagonal win check
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
draw_desc_diagonal(player)
return True
return False
def draw_vertical_winning_line(col, player):
posX = col * SQUARE_SIZE + SQUARE_SIZE // 2
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (posX, 15), (posX, HEIGHT - 15), 15)
def draw_horizontal_winning_line(row, player):
posY = row * SQUARE_SIZE + SQUARE_SIZE // 2
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, posY), (WIDTH - 15, posY), 15)
def draw_asc_diagonal(player):
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, HEIGHT - 15), (WIDTH - 15, 15), 15)
def draw_desc_diagonal(player):
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, 15), (WIDTH - 15, HEIGHT - 15), 15)
def restart():
screen.fill(BG_COLOR)
draw_lines()
for row in range(BOARD_ROWS):
for col in range(BOARD_COLUMNS):
board[row][col] = 0
draw_lines()
player = 1
game_over = False
# Main event loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
mouseX = event.pos[0] # x
mouseY = event.pos[1] # y
clicked_row = mouseY // SQUARE_SIZE
clicked_col = mouseX // SQUARE_SIZE
if board[clicked_row][clicked_col] == 0:
board[clicked_row][clicked_col] = player
if check_winner(player):
game_over = True
player = 2 if player == 1 else 1
draw_figures()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
restart()
player = 1
game_over = False
pygame.display.update()
'R' key is pressed.This code creates a simple GUI for playing Tic-Tac-Toe. The game is for two players, switching turns after each valid move. The game can be restarted by pressing the R key. Adjust colors, sizes, and appearance to customize your game further.
Not yet included in a published release.