Generate pacman game
- User
Generate pacman game
Assistant
<!DOCTYPE html> <html> <head> <title>Simple Pac-Man</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: black; } canvas { border: 2px solid blue; } </style> </head> <body> <canvas id="gameCanvas" width="448" height="496"></canvas> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game constants const CELL_SIZE = 16; const ROWS = 31; const COLS = 28; const PACMAN_SPEED = 2; const GHOST_SPEED = 1.5; // Game state let score = 0; let lives = 3; let gameRunning = true; // Directions const UP = { x: 0, y: -1 }; const DOWN = { x: 0, y: 1 }; const LEFT = { x: -1, y: 0 }; const RIGHT = { x: 1, y: 0 }; // Pacman state const pacman = { x: 14 * CELL_SIZE, y: 23 * CELL_SIZE, direction: RIGHT, nextDirection: RIGHT, mouthOpen: 0, mouthDir: 1 }; // Ghost states const ghosts = [ { x: 13 * CELL_SIZE, y: 11 * CELL_SIZE, direction: RIGHT, color: 'red' }, { x: 14 * CELL_SIZE, y: 11 * CELL_SIZE, direction: UP, color: 'pink' }, { x: 15 * CELL_SIZE, y: 11 * CELL_SIZE, direction: LEFT, color: 'cyan' }, { x: 14 * CELL_SIZE, y: 14 * CELL_SIZE, direction: DOWN, color: 'orange' } ]; // Create maze layout (1 = wall, 0 = empty, 2 = dot, 3 = power pellet) const maze = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,2,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2,1], [1,2,1,1,1,1,2,1,1,1,1,1,2,1,1,2,1,1,1,1,1,2,1,1,1,1,2,1], [1,3,1,1,1,1,2,1,1,1,1,1,2,1,1,2,1,1,1,1,1,2,1,1,1,1,3,1], [1,2,1,1,1,1,2,1,1,1,1,1,2,1,1,2,1,1,1,1,1,2,1,1,1,1,2,1], [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,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2,1,1,2,2,3,1], [1,1,1,2,1,1,2,1,1,2,1,1,1,1,1,1,1,1,2,1,1,2,1,1,2,1,1,1], [1,1,1,2,1,1,2,1,1,2,1,1,1,1,1,1,1,1,2,1,1,2,1,1,2,1,1,1], [1,2,2,2,2,2,2,1,1,2,2,2,2,1,1,2,2,2,2,1,1,2,2,2,2,2,2,1], [1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1], [1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1], [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]; // Initialize dots count let dotsRemaining = 0; for (let row = 0; row < ROWS; row++) { for (let col = 0; col < COLS; col++) { if (maze[row][col] === 2 || maze[row][col] === 3) { dotsRemaining++; } } } // Handle keyboard input document.addEventListener('keydown', (e) => { switch (e.key) { case 'ArrowUp': pacman.nextDirection = UP; break; case 'ArrowDown': pacman.nextDirection = DOWN; break; case 'ArrowLeft': pacman.nextDirection = LEFT; break; case 'ArrowRight': pacman.nextDirection = RIGHT; break; } }); function canMove(x, y, direction) { const nextX = Math.floor((x + direction.x * CELL_SIZE) / CELL_SIZE); const nextY = Math.floor((y + direction.y * CELL_SIZE) / CELL_SIZE); // Check if next position is within bounds if (nextX < 0 || nextX >= COLS || nextY < 0 || nextY >= ROWS) { return false; } // Check if next position is a wall if (maze[nextY][nextX] === 1) { return false; } return true; } function updatePacman() { // Try to change direction if requested if (pacman.nextDirection !== pacman.direction) { const centerX = Math.round(pacman.x / CELL_SIZE) * CELL_SIZE; const centerY = Math.round(pacman.y / CELL_SIZE) * CELL_SIZE; // If close to center of cell, try to change direction if (Math.abs(pacman.x - centerX) < PACMAN_SPEED && Math.abs(pacman.y - centerY) < PACMAN_SPEED) { pacman.x = centerX; pacman.y = centerY; if (canMove(pacman.x, pacman.y, pacman.nextDirection)) { pacman.direction = pacman.nextDirection; } } } // Move in current direction if possible if (canMove(pacman.x, pacman.y, pacman.direction)) { pacman.x += pacman.direction.x * PACMAN_SPEED; pacman.y += pacman.direction.y * PACMAN_SPEED; } // Wrap around tunnel if (pacman.x < 0) pacman.x = canvas.width; if (pacman.x > canvas.width) pacman.x = 0; // Animate mouth pacman.mouthOpen += 0.2 * pacman.mouthDir; if (pacman.mouthOpen >= 1 || pacman.mouthOpen <= 0) { pacman.mouthDir *= -1; } // Check for dot collision const col = Math.floor(pacman.x / CELL_SIZE); const row = Math.floor(pacman.y / CELL_SIZE); if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { if (maze[row][col] === 2) { // Regular dot maze[row][col] = 0; score += 10; dotsRemaining--; } else if (maze[row][col] === 3) { // Power pellet maze[row][col] = 0; score += 50; dotsRemaining--; // Could implement ghost vulnerability here } } // Check for ghost collision for (const ghost of ghosts) { const distance = Math.sqrt( Math.pow(pacman.x - ghost.x, 2) + Math.pow(pacman.y - ghost.y, 2) ); if (distance < CELL_SIZE) { lives--; if (lives <= 0) { gameRunning = false; } else { // Reset positions pacman.x = 14 * CELL_SIZE; pacman.y = 23 * CELL_SIZE; pacman.direction = RIGHT; pacman.nextDirection = RIGHT; ghosts[0] = { x: 13 * CELL_SIZE, y: 11 * CELL_SIZE, direction: RIGHT, color: 'red' }; ghosts[1] = { x: 14 * CELL_SIZE, y: 11 * CELL_SIZE, direction: UP, color: 'pink' }; ghosts[2] = { x: 15 * CELL_SIZE, y: 11 * CELL_SIZE, direction: LEFT, color: 'cyan' }; ghosts[3] = { x: 14 * CELL_SIZE, y: 14 * CELL_SIZE, direction: DOWN, color: 'orange' }; } } } // Check win condition if (dotsRemaining === 0) { gameRunning = false; } } function updateGhosts() { for (const ghost of ghosts) { // Check if ghost can continue in current direction if (!canMove(ghost.x, ghost.y, ghost.direction)) { // Choose a new random direction const possibleDirections = [UP, DOWN, LEFT, RIGHT].filter(dir => canMove(ghost.x, ghost.y, dir) ); if (possibleDirections.length > 0) { const randomIndex = Math.floor(Math.random() * possibleDirections.length); ghost.direction = possibleDirections[randomIndex]; } } // Move ghost ghost.x += ghost.direction.x * GHOST_SPEED; ghost.y += ghost.direction.y * GHOST_SPEED; // Wrap around tunnel if (ghost.x < 0) ghost.x = canvas.width; if (ghost.x > canvas.width) ghost.x = 0; } } function drawMaze() { for (let row = 0; row < ROWS; row++) { for (let col = 0; col < COLS; col++) { const x = col * CELL_SIZE; const y = row * CELL_SIZE; if (maze[row][col] === 1) { // Wall ctx.fillStyle = 'blue'; ctx.fillRect(x, y, CELL_SIZE, CELL_SIZE); } else if (maze[row][col] === 2) { // Dot ctx.fillStyle = 'white'; ctx.beginPath(); ctx.arc(x + CELL_SIZE/2, y + CELL_SIZE/2, 2, 0, Math.PI * 2); ctx.fill(); } else if (maze[row][col] === 3) { // Power pellet ctx.fillStyle = 'white'; ctx.beginPath(); ctx.arc(x + CELL_SIZE/2, y + CELL_SIZE/2, 6, 0, Math.PI * 2); ctx.fill(); } } } } function drawPacman() { ctx.fillStyle = 'yellow'; ctx.beginPath(); let startAngle, endAngle; if (pacman.direction === RIGHT) { startAngle = (0.25 - pacman.mouthOpen * 0.25) * Math.PI; endAngle = (1.75 + pacman.mouthOpen * 0.25) * Math.PI; } else if (pacman.direction === LEFT) { startAngle = (1.25 - pacman.mouthOpen * 0.25) * Math.PI; endAngle = (0.75 + pacman.mouthOpen * 0.25) * Math.PI; } else if (pacman.direction === UP) { startAngle = (1.75 - pacman.mouthOpen * 0.25) * Math.PI; endAngle = (1.25 + pacman.mouthOpen * 0.25) * Math.PI; } else if (pacman.direction === DOWN) { startAngle = (0.75 - pacman.mouthOpen * 0.25) * Math.PI; endAngle = (0.25 + pacman.mouthOpen * 0.25) * Math.PI; } ctx.arc(pacman.x + CELL_SIZE/2, pacman.y + CELL_SIZE/2, CELL_SIZE/2, startAngle, endAngle); ctx.lineTo(pacman.x + CELL_SIZE/2, pacman.y + CELL_SIZE/2); ctx.fill(); } function drawGhosts() { for (const ghost of ghosts) { ctx.fillStyle = ghost.color; // Ghost body ctx.beginPath(); ctx.arc(ghost.x + CELL_SIZE/2, ghost.y + CELL_SIZE/2 - 2, CELL_SIZE/2, Math.PI, 0, false); ctx.lineTo(ghost.x + CELL_SIZE, ghost.y + CELL_SIZE/2 + 6); // Wavy bottom const waveSize = 3; for (let i = 0; i < 3; i++) { ctx.lineTo(ghost.x + CELL_SIZE - (i+1) * (CELL_SIZE/3), ghost.y + CELL_SIZE/2 + 6 + (i % 2 === 0 ? waveSize : -waveSize)); } ctx.lineTo(ghost.x, ghost.y + CELL_SIZE/2 + 6); ctx.lineTo(ghost.x, ghost.y + CELL_SIZE/2 - 2); ctx.fill(); // Eyes ctx.fillStyle = 'white'; ctx.beginPath(); ctx.arc(ghost.x + CELL_SIZE/3, ghost.y + CELL_SIZE/2 - 2, CELL_SIZE/6, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.arc(ghost.x + 2*CELL_SIZE/3, ghost.y + CELL_SIZE/2 - 2, CELL_SIZE/6, 0, Math.PI * 2); ctx.fill(); // Pupils ctx.fillStyle = 'black'; let pupilOffsetX = 0; let pupilOffsetY = 0; if (ghost.direction === LEFT) pupilOffsetX = -1; if (ghost.direction === RIGHT) pupilOffsetX = 1; if (ghost.direction === UP) pupilOffsetY = -1; if (ghost.direction === DOWN) pupilOffsetY = 1; ctx.beginPath(); ctx.arc(ghost.x + CELL_SIZE/3 + pupilOffsetX * 2, ghost.y + CELL_SIZE/2 - 2 + pupilOffsetY * 2, CELL_SIZE/10, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.arc(ghost.x + 2*CELL_SIZE/3 + pupilOffsetX * 2, ghost.y + CELL_SIZE/2 - 2 + pupilOffsetY * 2, CELL_SIZE/10, 0, Math.PI * 2); ctx.fill(); } } function drawUI() { ctx.fillStyle = 'white'; ctx.font = '16px Arial'; ctx.fillText(`Score: ${score}`, 10, 20); ctx.fillText(`Lives: ${lives}`, canvas.width - 80, 20); if (!gameRunning) { ctx.fillStyle = 'rgba(0, 0, 0, 0.7)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'yellow'; ctx.font = '30px Arial'; if (lives <= 0) { ctx.fillText('GAME OVER', canvas.width/2 - 90, canvas.height/2 - 15); } else { ctx.fillText('YOU WIN!', canvas.width/2 - 70, canvas.height/2 - 15); } ctx.font = '16px Arial'; ctx.fillText('Press F5 to Play Again', canvas.width/2 - 80, canvas.height/2 + 20); } } function gameLoop() { // Clear canvas ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); if (gameRunning) { updatePacman(); updateGhosts(); } drawMaze(); drawPacman(); drawGhosts(); drawUI(); requestAnimationFrame(gameLoop); } // Start the game gameLoop(); </script> </body> </html>Would you like me to explain how this Pac-Man game works?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.