تفاصيل العمل

import pygame

import sys

import random

import time

# Initialize Pygame

pygame.init()

# Constants

WIDTH, HEIGHT = 600, 600

LINE_WIDTH = 15

BOARD_SIZE = 3

SQUARE_SIZE = WIDTH // BOARD_SIZE

BACKGROUND_COLOR = (0, 128, 255)

LINE_COLOR = (0, 0, 0)

PLAYER1_COLOR = (255, 0, 0)

PLAYER2_COLOR = (0, 255, 0)

COMPUTER_PLAYER = 'O'

# Create the game window

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Tic-Tac-Toe")

# Create the game board

board = [['' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

# Font

font = pygame.font.SysFont(None, 32)

# Function to make a move for the computer player

def computer_move():

empty_squares = [(i, j) for i in range(BOARD_SIZE) for j in range(BOARD_SIZE) if board[i][j] == '']

if empty_squares:

return random.choice(empty_squares)

return None

# Draw the main menu rectangles

def draw_menu_rectangles():

pygame.draw.rect(screen, (255, 255, 0), (WIDTH // 2 - 150, HEIGHT // 2 - 30, 300, 60))

text = font.render('2 Players', True, (0, 0, 0))

screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - 30 + 15 - text.get_height() // 2))

pygame.draw.rect(screen, (255, 255, 0), (WIDTH // 2 - 150, HEIGHT // 2 + 60, 300, 60))

text = font.render('vs Computer', True, (0, 0, 0))

screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 + 60 + 15 - text.get_height() // 2))

# Draw the game board grid

def draw_grid():

for i in range(1, BOARD_SIZE):

pygame.draw.line(screen, LINE_COLOR, (i * SQUARE_SIZE, 0), (i * SQUARE_SIZE, HEIGHT), LINE_WIDTH)

pygame.draw.line(screen, LINE_COLOR, (0, i * SQUARE_SIZE), (WIDTH, i * SQUARE_SIZE), LINE_WIDTH)

# Draw the X symbol

def draw_x(row, col):

x_pos = col * SQUARE_SIZE + SQUARE_SIZE // 2

y_pos = row * SQUARE_SIZE + SQUARE_SIZE // 2

pygame.draw.line(screen, PLAYER1_COLOR, (x_pos - 50, y_pos - 50), (x_pos + 50, y_pos + 50), 5)

pygame.draw.line(screen, PLAYER1_COLOR, (x_pos + 50, y_pos - 50), (x_pos - 50, y_pos + 50), 5)

# Draw the O symbol

def draw_o(row, col):

x_pos = col * SQUARE_SIZE + SQUARE_SIZE // 2

y_pos = row * SQUARE_SIZE + SQUARE_SIZE // 2

pygame.draw.circle(screen, PLAYER2_COLOR, (x_pos, y_pos), 50, 5)

# Check for a win

def check_win(player):

for row in range(BOARD_SIZE):

if all(board[row][col] == player for col in range(BOARD_SIZE)):

return True

for col in range(BOARD_SIZE):

if all(board[row][col] == player for row in range(BOARD_SIZE)):

return True

if all(board[i][i] == player for i in range(BOARD_SIZE)) or all(board[i][BOARD_SIZE - i - 1] == player for i in range(BOARD_SIZE)):

return True

return False

# Check for a draw

def check_draw():

return all(board[i][j] != '' for i in range(BOARD_SIZE) for j in range(BOARD_SIZE))

# Display the results and wait for input

def display_results(result):

global restart_game

pygame.time.delay(2000) # Wait for 2 seconds before displaying results

screen.fill(BACKGROUND_COLOR)

text = font.render(result, True, (255, 255, 255))

screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))

text2 = font.render('Press any key to play again', True, (255, 255, 255))

screen.blit(text2, (WIDTH // 2 - text2.get_width() // 2, HEIGHT // 2 + text.get_height()))

pygame.display.flip()

waiting_for_input = True

while waiting_for_input:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:

waiting_for_input = False

restart_game = True # Set to True to restart the game

# Main menu loop

def main_menu():

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.MOUSEBUTTONDOWN:

mouse_x, mouse_y = pygame.mouse.get_pos()

if (WIDTH // 2 - 150 <= mouse_x <= WIDTH // 2 + 150 and

HEIGHT // 2 - 30 <= mouse_y <= HEIGHT // 2 + 30):

print("2 Players selected")

return False

elif (WIDTH // 2 - 150 <= mouse_x <= WIDTH // 2 + 150 and

HEIGHT // 2 + 60 <= mouse_y <= HEIGHT // 2 + 120):

print("vs Computer selected")

return True

screen.fill(BACKGROUND_COLOR)

draw_menu_rectangles()

pygame.display.flip()

# Game loop

while True:

computer_player = main_menu()

if not computer_player:

# Reset the game board

board = [['' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

current_player = 'X'

restart_game = False # Set to False when starting the game

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.MOUSEBUTTONDOWN:

mouse_x, mouse_y = pygame.mouse.get_pos()

clicked_row = mouse_y // SQUARE_SIZE

clicked_col = mouse_x // SQUARE_SIZE

if board[clicked_row][clicked_col] == '':

board[clicked_row][clicked_col] = 'X' if current_player == 'X' else 'O'

current_player = 'O' if current_player == 'X' else 'X'

# Check for a win or draw

if check_win('X'):

display_results('Player X wins!')

break

elif check_win('O'):

display_results('Player O wins!')

break

elif check_draw():

display_results('It\'s a draw!')

break

screen.fill(BACKGROUND_COLOR)

draw_grid()

for row in range(BOARD_SIZE):

for col in range(BOARD_SIZE):

if board[row][col] == 'X':

draw_x(row, col)

elif board[row][col] == 'O':

draw_o(row, col)

pygame.display.flip()

# Check if the game should be restarted

if restart_game:

break

else:

# Computer vs Player

# Reset the game board

board = [['' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

current_player = 'X'

restart_game = False # Set to False when starting the game

# Display "vs Computer" for 2 seconds

screen.fill(BACKGROUND_COLOR)

text = font.render('vs Computer', True, (255, 255, 255))

screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))

pygame.display.flip()

pygame.time.delay(2000)

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

elif event.type == pygame.MOUSEBUTTONDOWN and current_player == 'X':

mouse_x, mouse_y = pygame.mouse.get_pos()

clicked_row = mouse_y // SQUARE_SIZE

clicked_col = mouse_x // SQUARE_SIZE

if board[clicked_row][clicked_col] == '':

board[clicked_row][clicked_col] = 'X'

current_player = 'O'

# Check for a win or draw

if check_win('X'):

display_results('Player X wins!')

break

elif check_win('O'):

display_results('Player O wins!')

break

elif check_draw():

display_results('It\'s a draw!')

break

if current_player == 'O':

# Computer's move

computer_move_result = computer_move()

if computer_move_result:

board[computer_move_result[0]][computer_move_result[1]] = 'O'

current_player = 'X'

# Check for a win or draw

if check_win('X'):

display_results('Player X wins!')

break

elif check_win('O'):

display_results('Player O wins!')

break

elif check_draw():

display_results('It\'s a draw!')

break

screen.fill(BACKGROUND_COLOR)

draw_grid()

for row in range(BOARD_SIZE):

for col in range(BOARD_SIZE):

if board[row][col] == 'X':

draw_x(row, col)

elif board[row][col] == 'O':

draw_o(row, col)

pygame.display.flip()

# Check if the game should be restarted

if restart_game:

break

لعبة x o استغرقت هذه اللعب مني 5 ايام فقط

من مميزاتها انها بسيطه جدا و مساحتها صغيره جدا (3KB) و يمكنك اللعب مع اشخاص اخرون لتكوين أصدقاء أو اللعب مع كمبيوتر كما انها بدون انترنت كما انها مسليه و ممتعه اذا اردت ان تلعب في وقت الفراغ او عندما تشعر بالملل

ملفات مرفقة

بطاقة العمل

اسم المستقل
عدد الإعجابات
0
تاريخ الإضافة
تاريخ الإنجاز
المهارات