0% found this document useful (0 votes)
60 views2 pages

Pygame Tic-Tac-Toe Game Code

This document is a Python script that implements a Tic-Tac-Toe game using Pygame. It initializes a game window, loads images for the X and O players, and includes functions to check for winners and ties. The game runs in a loop, allowing players to click on the grid to place their marks until a winner is determined or a tie occurs.

Uploaded by

peterfrodo333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

Pygame Tic-Tac-Toe Game Code

This document is a Python script that implements a Tic-Tac-Toe game using Pygame. It initializes a game window, loads images for the X and O players, and includes functions to check for winners and ties. The game runs in a loop, allowing players to click on the grid to place their marks until a winner is determined or a tie occurs.

Uploaded by

peterfrodo333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import pygame

import random

# Initialize Pygame
[Link]()

# Define constants
WIDTH = 600
HEIGHT = 600
MARGIN = 50
SIZE = (WIDTH - 2 * MARGIN) // 3
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Tic-Tac-Toe")

# Load images for X and O


x_img = [Link]("x_img.png")
o_img = [Link]("o_img.png")
x_img = [Link](x_img, (SIZE, SIZE))
o_img = [Link](o_img, (SIZE, SIZE))

# Initialize grid
grid = [["" for _ in range(3)] for _ in range(3)]
current_player = [Link](["X", "O"]) # Randomly choose who starts

# Functions for checking winner, tie, and drawing grid


def get_grid_pos(x, y):
row = (y - MARGIN) // SIZE
col = (x - MARGIN) // SIZE
if 0 <= row < 3 and 0 <= col < 3:
return row, col
return None

def check_winner():
# For rows
for row in grid:
if row[0] == row[1] == row[2] and row[0] != "":
return row[0]
# For columns
for col in range(3):
if grid[0][col] == grid[1][col] == grid[2][col] and grid[0][col] != "":
return grid[0][col]
# For diagonals
if grid[0][0] == grid[1][1] == grid[2][2] and grid[0][0] != "":
return grid[0][0]
if grid[0][2] == grid[1][1] == grid[2][0] and grid[0][2] != "":
return grid[0][2]
return None

def check_tie():
if check_winner() is None:
for row in grid:
for cell in row:
if cell == "":
return False
return True
return False

def draw_grid():
for row in range(3):
for col in range(3):
x_pos = MARGIN + col * SIZE
y_pos = MARGIN + row * SIZE
if row == 1:
[Link](screen, "BLACK", (MARGIN, y_pos), (WIDTH - MARGIN,
y_pos), 5)
if col == 1:
[Link](screen, "BLACK", (x_pos, MARGIN), (x_pos, HEIGHT -
MARGIN), 5)
if grid[row][col] == "X":
[Link](x_img, (x_pos, y_pos))
elif grid[row][col] == "O":
[Link](o_img, (x_pos, y_pos))

# Game loop
running = True
while running:
[Link]((255, 255, 255))
draw_grid()
[Link]()

for event in [Link]():


if [Link] == [Link]:
running = False

if [Link] == [Link]:
x, y = [Link].get_pos()
pos = get_grid_pos(x, y)
if pos:
row, col = pos
if grid[row][col] == "":
grid[row][col] = current_player
current_player = "O" if current_player == "X" else "X"

if check_winner():
print(f"{check_winner()} wins!")
running = False
elif check_tie():
print("It's a tie!")
running = False

[Link]()

You might also like