import socket
import sys
import pygame
# Initialization
window_width = 1050
window_height = 600
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode((window_width, window_height))
timer = pygame.time.Clock()
pygame.display.set_caption('Chess Game')
font = pygame.font.Font(None, 100)
icon_size = 200 # Further increased icon size
chess_icon = pygame.image.load('chess_icon.png')
chess_icon = pygame.transform.scale(chess_icon, (icon_size, icon_size))
# Load sound effects
player_win_sound = pygame.mixer.Sound('player_win.wav')
robot_win_sound = pygame.mixer.Sound('robot_win.wav')
# Networking
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.101', 1234))
running = True
initial_display = True
# Adjusted pastel colors for chessboard
light_square = (233, 246, 229) # Pastel green
dark_square = (174, 214, 241) # Pastel blue
def draw_chess_background():
tile_size = 75
for row in range(window_height // tile_size):
for col in range(window_width // tile_size):
if (row + col) % 2 == 0:
color = light_square
else:
color = dark_square
pygame.draw.rect(window, color, pygame.Rect(col * tile_size, row * tile_size, tile_size, tile_size))
def dynamic_colors(player):
if player == "player":
return (255, 255, 255), (0, 0, 0) # White background, Black text for player
else:
return (0, 0, 0), (255, 255, 255) # Black background, White text for robot
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
msg = s.recv(1024)
msg = msg.decode('utf-8')
if initial_display:
draw_chess_background()
window.blit(chess_icon, (window_width // 2 - icon_size // 2, 50))
text_msg = "Let's play chess!"
bg_color, text_color = dynamic_colors("player")
initial_display = False
else:
player = "player" if "player" in msg else "robot"
bg_color, text_color = dynamic_colors(player)
window.fill(bg_color)
if msg:
text_msg = msg
if "player wins" in msg:
player_win_sound.play()
elif "robot wins" in msg:
robot_win_sound.play()
text = font.render(text_msg, True, text_color)
text_rect = text.get_rect()
text_rect.center = (window_width // 2, 200)
window.blit(text, text_rect)
pygame.display.flip()
timer.tick(60)
pygame.quit()
sys.exit()