create carrom game
- User
create carrom game
Assistant
I'll create a simple carrom game for you using HTML5 Canvas and JavaScript. This will be a basic version with physics simulation for striking and pocketing pieces.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Carrom Game</title> <style> body { margin: 0; padding: 20px; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #2c3e50; font-family: Arial, sans-serif; } #gameContainer { text-align: center; background-color: #34495e; padding: 20px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3); } canvas { border: 3px solid #8b6914; cursor: crosshair; background-color: #d4a574; } #scoreBoard { margin-top: 20px; color: white; font-size: 18px; } .score { display: inline-block; margin: 0 20px; } #controls { margin-top: 20px; color: white; } button { padding: 10px 20px; font-size: 16px; background-color: #3498db; color: white; border: none; border-radius: 5px; cursor: pointer; margin: 0 5px; } button:hover { background-color: #2980b9; } #instructions { color: white; margin-top: 20px; font-size: 14px; } </style> </head> <body> <div id="gameContainer"> <h1 style="color: white;">Carrom Game</h1> <canvas id="gameCanvas" width="600" height="600"></canvas> <div id="scoreBoard"> <div class="score">Player 1: <span id="player1Score">0</span></div> <div class="score">Player 2: <span id="player2Score">0</span></div> <div class="score">Turn: <span id="currentPlayer">Player 1</span></div> </div> <div id="controls"> <button onclick="resetGame()">Reset Game</button> </div> <div id="instructions"> <p>Click and drag to aim and shoot the striker!</p> <p>Pocket all your pieces (Player 1: White, Player 2: Black)</p> <p>Red piece is the Queen - worth 5 points!</p> </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game constants const BOARD_SIZE = 600; const POCKET_SIZE = 30; const PIECE_RADIUS = 15; const STRIKER_RADIUS = 20; const FRICTION = 0.98; const POCKET_POSITIONS = [ {x: 30, y: 30}, {x: BOARD_SIZE - 30, y: 30}, {x: 30, y: BOARD_SIZE - 30}, {x: BOARD_SIZE - 30, y: BOARD_SIZE - 30} ]; // Game state let pieces = []; let striker = null; let currentPlayer = 1; let scores = {player1: 0, player2: 0}; let isDragging = false; let dragStart = {x: 0, y: 0}; let dragEnd = {x: 0, y: 0}; class Piece { constructor(x, y, type, color) { this.x = x; this.y = y; this.vx = 0; this.vy = 0; this.type = type; // 'white', 'black', 'queen', 'striker' this.color = color; this.radius = type === 'striker' ? STRIKER_RADIUS : PIECE_RADIUS; this.pocketed = false; } update() { if (this.pocketed) return; this.x += this.vx; this.y += this.vy; // Apply friction this.vx *= FRICTION; this.vy *= FRICTION; // Stop if moving very slowly if (Math.abs(this.vx) < 0.1 && Math.abs(this.vy) < 0.1) { this.vx = 0; this.vy = 0; } // Bounce off walls if (this.x - this.radius < 0 || this.x + this.radius > BOARD_SIZE) { this.vx = -this.vx * 0.8; this.x = Math.max(this.radius, Math.min(BOARD_SIZE - this.radius, this.x)); } if (this.y - this.radius < 0 || this.y + this.radius > BOARD_SIZE) { this.vy = -this.vy * 0.8; this.y = Math.max(this.radius, Math.min(BOARD_SIZE - this.radius, this.y)); } // Check pockets for (let pocket of POCKET_POSITIONS) { let dist = Math.sqrt((this.x - pocket.x) ** 2 + (this.y - pocket.y) ** 2); if (dist < POCKET_SIZE) { this.pocketed = true; this.handlePocket(); } } } handlePocket() { if (this.type === 'striker') { // Penalty for pocketing striker scores[`player${currentPlayer}`] = Math.max(0, scores[`player${currentPlayer}`] - 5); } else if (this.type === 'queen') { scores[`player${currentPlayer}`] += 5; } else if (this.type === 'white' && currentPlayer === 1) { scores.player1 += 1; } else if (this.type === 'black' && currentPlayer === 2) { scores.player2 += 1; } updateScoreBoard(); } draw() { if (this.pocketed) return; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); ctx.strokeStyle = '#000'; ctx.lineWidth = 2; ctx.stroke(); // Add a dot for the queen if (this.type === 'queen') { ctx.beginPath(); ctx.arc(this.x, this.y, 5, 0, Math.PI * 2); ctx.fillStyle = '#fff'; ctx.fill(); } } } function initializeGame() { pieces = []; // Create pieces in a circular pattern const centerX = BOARD_SIZE / 2; const centerY = BOARD_SIZE / 2; // Queen in center pieces.push(new Piece(centerX, centerY, 'queen', '#ff0000')); // White and black pieces in alternating pattern const radius = 50; for (let i = 0; i < 6; i++) { const angle = (Math.PI * 2 * i) / 6; const x = centerX + Math.cos(angle) * radius; const y = centerY + Math.sin(angle) * radius; pieces.push(new Piece(x, y, i % 2 === 0 ? 'white' : 'black', i % 2 === 0 ? '#fff' : '#000')); } // Outer ring const outerRadius = 90; for (let i = 0; i < 12; i++) { const angle = (Math.PI * 2 * i) / 12; const x = centerX + Math.cos(angle) * outerRadius; const y = centerY + Math.sin(angle) * outerRadius; pieces.push(new Piece(x, y, i % 2 === 0 ? 'white' : 'black', i % 2 === 0 ? '#fff' : '#000')); } // Striker striker = new Piece(centerX, BOARD_SIZE - 100, 'striker', '#8b4513'); pieces.push(striker); } function checkCollision(p1, p2) { const dx = p1.x - p2.x; const dy = p1.y - p2.y; const distance = Math.sqrt(dx * dx + dy * dy); return distance < p1.radius + p2.radius; } function resolveCollision(p1, p2) { const dx = p2.x - p1.x; const dy = p2.y - p1.y; const distance = Math.sqrt(dx * dx + dy * dy); if (distance === 0) return; // Normalize const nx = dx / distance; const ny = dy / distance; // Relative velocity const dvx = p2.vx - p1.vx; const dvy = p2.vy - p1.vy; // Relative velocity in collision normal direction const dvn = dvx * nx + dvy * ny; // Do not resolve if velocities are separating if (dvn > 0) return; // Collision impulse const impulse = dvn * 0.8; // Some energy loss // Apply impulse p1.vx += impulse * nx; p1.vy += impulse * ny; p2.vx -= impulse * nx; p2.vy -= impulse * ny; // Separate pieces const overlap = p1.radius + p2.radius - distance; const separationX = overlap * nx / 2; const separationY = overlap * ny / 2; p1.x -= separationX; p1.y -= separationY; p2.x += separationX; p2.y += separationY; } function update() { let allStopped = true; // Update all pieces for (let piece of pieces) { piece.update(); if (piece.vx !== 0 || piece.vy !== 0) { allStopped = false; } } // Check collisions for (let i = 0; i < pieces.length; i++) { for (let j = i + 1; j < pieces.length; j++) { if (!pieces[i].pocketed && !pieces[j].pocketed && checkCollision(pieces[i], pieces[j])) { resolveCollision(pieces[i], pieces[j]); } } } // Switch turns when all pieces stop if (allStopped && (striker.vx !== 0 || striker.vy !== 0)) { striker.vx = 0; striker.vy = 0; } // Reset striker position if it was pocketed if (striker.pocketed) { striker.pocketed = false; striker.x = BOARD_SIZE / 2; striker.y = currentPlayer === 1 ? BOARD_SIZE - 100 : 100; switchTurn(); } } function draw() { // Clear canvas ctx.fillStyle = '#d4a574'; ctx.fillRect(0, 0, BOARD_SIZE, BOARD_SIZE); // Draw board lines ctx.strokeStyle = '#8b6914'; ctx.lineWidth = 3; ctx.strokeRect(40, 40, BOARD_SIZE - 80, BOARD_SIZE - 80); // Draw center circle ctx.beginPath(); ctx.arc(BOARD_SIZE / 2, BOARD_SIZE / 2, 100, 0, Math.PI * 2); ctx.stroke(); // Draw pockets ctx.fillStyle = '#000'; for (let pocket of POCKET_POSITIONS) { ctx.beginPath(); ctx.arc(pocket.x, pocket.y, POCKET_SIZE, 0, Math.PI * 2); ctx.fill(); } // Draw pieces for (let piece of pieces) { piece.draw(); } // Draw aiming line if (isDragging && striker.vx === 0 && striker.vy === 0) { ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(striker.x, striker.y); ctx.lineTo(striker.x + (dragStart.x - dragEnd.x) * 0.3, striker.y + (dragStart.y - dragEnd.y) * 0.3); ctx.stroke(); } } function gameLoop() { update(); draw(); requestAnimationFrame(gameLoop); } function switchTurn() { currentPlayer = currentPlayer === 1 ? 2 : 1; document.getElementById('currentPlayer').textContent = `Player ${currentPlayer}`; striker.x = BOARD_SIZE / 2; striker.y = currentPlayer === 1 ? BOARD_SIZE - 100 : 100; } function updateScoreBoard() { document.getElementById('player1Score').textContent = scores.player1; document.getElementById('player2Score').textContent = scores.player2; } function resetGame() { scores = {player1: 0, player2: 0}; currentPlayer = 1; updateScoreBoard(); document.getElementById('currentPlayer').textContent = 'Player 1'; initializeGame(); } // Mouse events canvas.addEventListener('mousedown', (e) => { const rect = canvas.getBoundingClientRect(); const x = e.clientX - rect.left; const y = e.clientY - rect.top; // Check if clicking on striker const dist = Math.sqrt((x - striker.x) ** 2 + (y - striker.y) ** 2); if (dist < striker.radius && striker.vx === 0 && striker.vy === 0) { isDragging = true; dragStart = {x, y}; dragEnd = {x, y}; } }); canvas.addEventListener('mousemove', (e) => { if (isDragging) { const rect = canvas.getBoundingClientRect(); dragEnd = { x: e.clientX - rect.left, y: e.clientY - rect.top }; } }); canvas.addEventListener('mouseup', () => { if (isDragging) { const power = Math.min(Math.sqrt((dragStart.x - dragEnd.x) ** 2 + (dragStart.y - dragEnd.y) ** 2) * 0.15, 20); const angle = Math.atan2(dragStart.y - dragEnd.y, dragStart.x - dragEnd.x); striker.vx = Math.cos(angle) * power; striker.vy = Math.sin(angle) * power; isDragging = false; // Switch turn after shot setTimeout(() => { let allStopped = true; for (let piece of pieces) { if (piece.vx !== 0 || piece.vy !== 0) { allStopped = false; break; } } if (allStopped) { switchTurn(); } }, 3000); } }); // Initialize and start game initializeGame(); gameLoop(); </script> </body> </html>This carrom game includes:
Features:
- Two-player turn-based gameplay
- Physics simulation for realistic piece movement
- Collision detection between pieces
- Pocketing system with 4 corner pockets
- Scoring system (1 point per piece, 5 points for the Queen)
- Striker control with click-and-drag aiming
- Visual feedback for shot power and direction
How to Play:
- Click and drag on the striker (brown piece) to aim
- Release to shoot - the farther you drag, the more power
- Player 1 (white pieces) starts from the bottom
- Player 2 (black pieces) plays from the top
- Pocket all your colored pieces to score points
- The red Queen is worth 5 points
- Pocketing the striker results in a -5 point penalty
The game uses HTML5 Canvas for rendering and JavaScript for game logic. You can save this as an HTML file and open it in any modern web browser to play!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.