|
| 1 | +const canvas = document.getElementById("pongCanvas"); |
| 2 | +const ctx = canvas.getContext("2d"); |
| 3 | + |
| 4 | +// Game constants |
| 5 | +const PADDLE_WIDTH = 12; |
| 6 | +const PADDLE_HEIGHT = 100; |
| 7 | +const BALL_SIZE = 14; |
| 8 | +const PLAYER_X = 20; |
| 9 | +const AI_X = canvas.width - PLAYER_X - PADDLE_WIDTH; |
| 10 | +const PADDLE_SPEED = 6; |
| 11 | +const AI_SPEED = 4; |
| 12 | + |
| 13 | +// Game state |
| 14 | +let playerY = canvas.height / 2 - PADDLE_HEIGHT / 2; |
| 15 | +let aiY = canvas.height / 2 - PADDLE_HEIGHT / 2; |
| 16 | + |
| 17 | +let ballX = canvas.width / 2 - BALL_SIZE / 2; |
| 18 | +let ballY = canvas.height / 2 - BALL_SIZE / 2; |
| 19 | +let ballSpeedX = Math.random() > 0.5 ? 5 : -5; |
| 20 | +let ballSpeedY = (Math.random() - 0.5) * 8; |
| 21 | + |
| 22 | +let playerScore = 0; |
| 23 | +let aiScore = 0; |
| 24 | + |
| 25 | +// Mouse control |
| 26 | +canvas.addEventListener("mousemove", (e) => { |
| 27 | + const rect = canvas.getBoundingClientRect(); |
| 28 | + const mouseY = e.clientY - rect.top; |
| 29 | + playerY = mouseY - PADDLE_HEIGHT / 2; |
| 30 | + // Clamp paddle within canvas |
| 31 | + playerY = Math.max(0, Math.min(canvas.height - PADDLE_HEIGHT, playerY)); |
| 32 | +}); |
| 33 | + |
| 34 | +// Draw functions |
| 35 | +function drawRect(x, y, w, h, color = "#fff") { |
| 36 | + ctx.fillStyle = color; |
| 37 | + ctx.fillRect(x, y, w, h); |
| 38 | +} |
| 39 | + |
| 40 | +function drawBall(x, y, size, color = "#fff") { |
| 41 | + ctx.fillStyle = color; |
| 42 | + ctx.beginPath(); |
| 43 | + ctx.arc(x + size / 2, y + size / 2, size / 2, 0, Math.PI * 2); |
| 44 | + ctx.fill(); |
| 45 | +} |
| 46 | + |
| 47 | +function drawNet() { |
| 48 | + ctx.strokeStyle = "#888"; |
| 49 | + ctx.lineWidth = 3; |
| 50 | + for (let i = 0; i < canvas.height; i += 32) { |
| 51 | + ctx.beginPath(); |
| 52 | + ctx.moveTo(canvas.width / 2, i); |
| 53 | + ctx.lineTo(canvas.width / 2, i + 20); |
| 54 | + ctx.stroke(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function drawScores() { |
| 59 | + ctx.font = "32px Arial"; |
| 60 | + ctx.fillStyle = "#fff"; |
| 61 | + ctx.fillText(playerScore, canvas.width / 4, 40); |
| 62 | + ctx.fillText(aiScore, canvas.width * 3 / 4, 40); |
| 63 | +} |
| 64 | + |
| 65 | +// Game logic |
| 66 | +function resetBall() { |
| 67 | + ballX = canvas.width / 2 - BALL_SIZE / 2; |
| 68 | + ballY = canvas.height / 2 - BALL_SIZE / 2; |
| 69 | + ballSpeedX = Math.random() > 0.5 ? 5 : -5; |
| 70 | + ballSpeedY = (Math.random() - 0.5) * 8; |
| 71 | +} |
| 72 | + |
| 73 | +function updateBall() { |
| 74 | + ballX += ballSpeedX; |
| 75 | + ballY += ballSpeedY; |
| 76 | + |
| 77 | + // Top and bottom wall collision |
| 78 | + if (ballY <= 0 || ballY + BALL_SIZE >= canvas.height) { |
| 79 | + ballSpeedY *= -1; |
| 80 | + // Clamp inside canvas |
| 81 | + ballY = Math.max(0, Math.min(canvas.height - BALL_SIZE, ballY)); |
| 82 | + } |
| 83 | + |
| 84 | + // Left paddle collision |
| 85 | + if ( |
| 86 | + ballX <= PLAYER_X + PADDLE_WIDTH && |
| 87 | + ballY + BALL_SIZE > playerY && |
| 88 | + ballY < playerY + PADDLE_HEIGHT && |
| 89 | + ballX > PLAYER_X - BALL_SIZE |
| 90 | + ) { |
| 91 | + ballX = PLAYER_X + PADDLE_WIDTH; |
| 92 | + ballSpeedX *= -1; |
| 93 | + // Add some spin based on where the ball hits the paddle |
| 94 | + let hitPos = (ballY + BALL_SIZE / 2) - (playerY + PADDLE_HEIGHT / 2); |
| 95 | + ballSpeedY = hitPos * 0.25; |
| 96 | + } |
| 97 | + |
| 98 | + // Right paddle (AI) collision |
| 99 | + if ( |
| 100 | + ballX + BALL_SIZE >= AI_X && |
| 101 | + ballY + BALL_SIZE > aiY && |
| 102 | + ballY < aiY + PADDLE_HEIGHT && |
| 103 | + ballX < AI_X + PADDLE_WIDTH + BALL_SIZE |
| 104 | + ) { |
| 105 | + ballX = AI_X - BALL_SIZE; |
| 106 | + ballSpeedX *= -1; |
| 107 | + let hitPos = (ballY + BALL_SIZE / 2) - (aiY + PADDLE_HEIGHT / 2); |
| 108 | + ballSpeedY = hitPos * 0.25; |
| 109 | + } |
| 110 | + |
| 111 | + // Score |
| 112 | + if (ballX < 0) { |
| 113 | + aiScore++; |
| 114 | + resetBall(); |
| 115 | + } else if (ballX > canvas.width) { |
| 116 | + playerScore++; |
| 117 | + resetBall(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function updateAI() { |
| 122 | + // Simple AI: move paddle center toward ball center |
| 123 | + let aiCenter = aiY + PADDLE_HEIGHT / 2; |
| 124 | + let ballCenter = ballY + BALL_SIZE / 2; |
| 125 | + if (aiCenter < ballCenter - 10) { |
| 126 | + aiY += AI_SPEED; |
| 127 | + } else if (aiCenter > ballCenter + 10) { |
| 128 | + aiY -= AI_SPEED; |
| 129 | + } |
| 130 | + // Clamp within canvas |
| 131 | + aiY = Math.max(0, Math.min(canvas.height - PADDLE_HEIGHT, aiY)); |
| 132 | +} |
| 133 | + |
| 134 | +function draw() { |
| 135 | + // Clear |
| 136 | + drawRect(0, 0, canvas.width, canvas.height, "#111"); |
| 137 | + drawNet(); |
| 138 | + drawScores(); |
| 139 | + // Paddles |
| 140 | + drawRect(PLAYER_X, playerY, PADDLE_WIDTH, PADDLE_HEIGHT, "#0ff"); |
| 141 | + drawRect(AI_X, aiY, PADDLE_WIDTH, PADDLE_HEIGHT, "#f0f"); |
| 142 | + // Ball |
| 143 | + drawBall(ballX, ballY, BALL_SIZE, "#fff"); |
| 144 | +} |
| 145 | + |
| 146 | +function gameLoop() { |
| 147 | + updateBall(); |
| 148 | + updateAI(); |
| 149 | + draw(); |
| 150 | + requestAnimationFrame(gameLoop); |
| 151 | +} |
| 152 | + |
| 153 | +// Start game |
| 154 | +gameLoop(); |
0 commit comments