import pygame
import math
import random
# Initialize pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Carrom Pool")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BROWN = (139, 69, 19)
BOAD_COLOR = (210, 180, 140)
# Game variables
class Striker:
def __init__(self):
self.radius = 20
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT - 100
self.color = WHITE
self.velocity = [0, 0]
self.power = 0
self.angle = 0
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
def update(self):
self.x += self.velocity[0]
self.y += self.velocity[1]
# Apply friction
self.velocity[0] *= 0.98
self.velocity[1] *= 0.98
# Stop when velocity is very small
if abs(self.velocity[0]) < 0.1 and abs(self.velocity[1]) < 0.1:
self.velocity[0] = 0
self.velocity[1] = 0
class Coin:
def __init__(self, x, y, color, is_queen=False):
self.radius = 15
self.x = x
self.y = y
self.color = color
self.is_queen = is_queen
self.velocity = [0, 0]
self.in_pocket = False
def draw(self):
if not self.in_pocket:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
if self.is_queen:
pygame.draw.circle(screen, RED, (int(self.x), int(self.y)), self.radius//2)
def update(self):
if not self.in_pocket:
self.x += self.velocity[0]
self.y += self.velocity[1]
# Apply friction
self.velocity[0] *= 0.98
self.velocity[1] *= 0.98
# Stop when velocity is very small
if abs(self.velocity[0]) < 0.1 and abs(self.velocity[1]) < 0.1:
self.velocity[0] = 0
self.velocity[1] = 0
class CarromBoard:
def __init__(self):
self.width = 600
self.height = 600
self.x = (SCREEN_WIDTH - self.width) // 2
self.y = (SCREEN_HEIGHT - self.height) // 2
self.pockets = [
(self.x, self.y), # Top-left
(self.x + self.width//2, self.y), # Top-middle
(self.x + self.width, self.y), # Top-right
(self.x, self.y + self.height), # Bottom-left
(self.x + self.width//2, self.y + self.height), # Bottom-middle
(self.x + self.width, self.y + self.height) # Bottom-right
]
self.pocket_radius = 25
def draw(self):
# Draw board
pygame.draw.rect(screen, BOARD_COLOR, (self.x, self.y, self.width, self.height))
# Draw border
pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height), 5)
# Draw pockets
for pocket in self.pockets:
pygame.draw.circle(screen, BLACK, pocket, self.pocket_radius)
# Main game function
def main():
clock = pygame.time.Clock()
running = True
board = CarromBoard()
striker = Striker()
# Create coins
coins = []
# Queen coin
coins.append(Coin(SCREEN_WIDTH//2, SCREEN_HEIGHT//2, RED, True))
# White coins
for i in range(9):
angle = random.uniform(0, 2*math.pi)
radius = random.uniform(50, 100)
x = SCREEN_WIDTH//2 + radius * math.cos(angle)
y = SCREEN_HEIGHT//2 + radius * math.sin(angle)
coins.append(Coin(x, y, WHITE))
# Black coins
for i in range(9):
angle = random.uniform(0, 2*math.pi)
radius = random.uniform(50, 100)
x = SCREEN_WIDTH//2 + radius * math.cos(angle)
y = SCREEN_HEIGHT//2 + radius * math.sin(angle)
coins.append(Coin(x, y, BLACK))
# Game state
player_turn = "white" # or "black"
game_over = False
while running:
screen.fill((50, 50, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle striker control
if event.type == pygame.MOUSEMOTION:
# Move striker within bounds
striker.x = max(board.x + striker.radius, min(event.pos[0], board.x + board.width - striker.radius))
if event.type == pygame.MOUSEBUTTONDOWN:
# Calculate angle and power for striker hit
dx = striker.x - event.pos[0]
dy = striker.y - event.pos[1]
distance = math.sqrt(dx*dx + dy*dy)
striker.power = min(distance, 100) / 2
striker.angle = math.atan2(dy, dx)
if event.type == pygame.MOUSEBUTTONUP:
# Apply force to striker
striker.velocity[0] = striker.power * math.cos(striker.angle)
striker.velocity[1] = striker.power * math.sin(striker.angle)
# Update game objects
striker.update()
for coin in coins:
coin.update()
# Check collision with striker
if not striker.velocity[0] == 0 and not striker.velocity[1] == 0:
dx = coin.x - striker.x
dy = coin.y - striker.y
distance = math.sqrt(dx*dx + dy*dy)
if distance < striker.radius + coin.radius:
# Simple collision response
angle = math.atan2(dy, dx)
coin.velocity[0] = striker.velocity[0] * math.cos(angle)
coin.velocity[1] = striker.velocity[1] * math.sin(angle)
striker.velocity[0] *= 0.7
striker.velocity[1] *= 0.7
# Check if coin is in pocket
for pocket in board.pockets:
dx = coin.x - pocket[0]
dy = coin.y - pocket[1]
distance = math.sqrt(dx*dx + dy*dy)
if distance < board.pocket_radius:
coin.in_pocket = True
# Add score based on coin type
# ...
# Draw everything
board.draw()
striker.draw()
for coin in coins:
coin.draw()
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()