... and update from 8 in 8 with SVG file type.
import pygame
from pygame.math import Vector2
import random
import os
username = os.getlogin()
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Eight-in-a-Row")
# Font setup
font = pygame.font.Font(None, 74)
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 120)
# Game board dimensions
BOARD_WIDTH = 8
BOARD_HEIGHT = 8
class Player:
def __init__(self, color, is_computer=False):
self.color = color
self.pieces = set()
self.is_computer = is_computer
self.svg_image = self.load_svg_image()
def add_piece(self, x, y):
self.pieces.add((x, y))
# def draw_pieces(self):
# for x, y in self.pieces:
# pygame.draw.circle(screen, self.color,
# (x * width // BOARD_WIDTH + width // (2 * BOARD_WIDTH),
# y * height // BOARD_HEIGHT + height // (2 * BOARD_HEIGHT)),
# min(width, height) // (2 * BOARD_WIDTH) - 5)
def load_svg_image(self):
if self.color == BLUE:
svg_path = "penguin-svgrepo-com.svg"
elif self.color == WHITE:
svg_path = "cube-svgrepo-com.svg"
else:
svg_path = "cube-svgrepo-com.svg"
return pygame.image.load(svg_path)
def draw_pieces(self):
piece_size = min(width, height) // (BOARD_WIDTH) - 10
for x, y in self.pieces:
pos = Vector2(x * width // BOARD_WIDTH + width // (2 * BOARD_WIDTH),
y * height // BOARD_HEIGHT + height // (2 * BOARD_HEIGHT))
scaled_image = pygame.transform.scale(self.svg_image, (piece_size, piece_size))
image_rect = scaled_image.get_rect(center=pos)
screen.blit(scaled_image, image_rect)
def make_move(self, board):
if self.is_computer:
empty_squares = [(x, y) for x in range(BOARD_WIDTH) for y in range(BOARD_HEIGHT)
if (x, y) not in board[0] and (x, y) not in board[1]]
if empty_squares:
return random.choice(empty_squares)
return None
def check_winner(player):
directions = [(0, 1), (1, 0), (1, 1), (1, -1)]
for x, y in player.pieces:
for dx, dy in directions:
if all((x + i*dx, y + i*dy) in player.pieces for i in range(8)):
return True
return False
def display_winner(winner):
text = font.render(f"Player {winner} wins!", True, [145,190,190])
text_rect = text.get_rect(center=(width // 2, height // 2))
screen.blit(text, text_rect)
pygame.display.flip()
pygame.time.wait(3000) # Display the message for 3 seconds
# Create players
player1 = Player(BLUE)
player2 = Player(WHITE, is_computer=True)
# Game loop
running = True
turn = 0
game_over = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
if turn % 2 == 0: # Human player's turn
mouse_x, mouse_y = event.pos
column = mouse_x // (width // BOARD_WIDTH)
row = mouse_y // (height // BOARD_HEIGHT)
if (column, row) not in player1.pieces and (column, row) not in player2.pieces:
player1.add_piece(column, row)
if check_winner(player1):
# print("Player 1 wins!")
display_winner(username)
game_over = True
turn += 1
if not game_over and turn % 2 == 1: # Computer player's turn
move = player2.make_move((player1.pieces, player2.pieces))
if move:
player2.add_piece(*move)
if check_winner(player2):
# print("Player 2 (Computer) wins!")
display_winner("Computer")
game_over = True
turn += 1
screen.fill(BLACK)
# Draw game board
for i in range(BOARD_WIDTH + 1):
pygame.draw.line(screen, WHITE, (i * width // BOARD_WIDTH, 0), (i * width // BOARD_WIDTH, height), 2)
for i in range(BOARD_HEIGHT + 1):
pygame.draw.line(screen, WHITE, (0, i * height // BOARD_HEIGHT), (width, i * height // BOARD_HEIGHT), 2)
# Draw pieces
player1.draw_pieces()
player2.draw_pieces()
pygame.display.flip()
pygame.quit()