|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Catch the Ball Game</title> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + margin: 0; |
| 10 | + display: flex; |
| 11 | + justify-content: center; |
| 12 | + align-items: center; |
| 13 | + height: 100vh; |
| 14 | + background-color: #f0f0f0; |
| 15 | + font-family: 'Roboto', sans-serif; |
| 16 | + } |
| 17 | + |
| 18 | + .game-container { |
| 19 | + text-align: center; |
| 20 | + } |
| 21 | + |
| 22 | + .game-title { |
| 23 | + font-family: 'Roboto', sans-serif; |
| 24 | + font-size: 36px; |
| 25 | + font-weight: 700; |
| 26 | + margin-bottom: 20px; |
| 27 | + color: #0095DD; |
| 28 | + } |
| 29 | + |
| 30 | + canvas { |
| 31 | + background-color: #333; |
| 32 | + border: 1px solid #000; |
| 33 | + } |
| 34 | + |
| 35 | + .controls { |
| 36 | + margin-top: 10px; |
| 37 | + } |
| 38 | + |
| 39 | + button { |
| 40 | + padding: 10px 20px; |
| 41 | + margin: 0 10px; |
| 42 | + font-size: 16px; |
| 43 | + } |
| 44 | + |
| 45 | + #playAgainBtn { |
| 46 | + display: none; |
| 47 | + margin-top: 10px; |
| 48 | + padding: 10px 20px; |
| 49 | + font-size: 16px; |
| 50 | + background-color: #0095DD; |
| 51 | + color: white; |
| 52 | + border: none; |
| 53 | + cursor: pointer; |
| 54 | + text-align: center; |
| 55 | + } |
| 56 | + </style> |
| 57 | +</head> |
| 58 | +<body> |
| 59 | + <div class="game-container"> |
| 60 | + <div class="game-title">Catch the Ball</div> |
| 61 | + <canvas id="gameCanvas"></canvas> |
| 62 | + <button id="playAgainBtn">Play Again</button> |
| 63 | + </div> |
| 64 | + <script> |
| 65 | + const canvas = document.getElementById('gameCanvas'); |
| 66 | + const ctx = canvas.getContext('2d'); |
| 67 | + let paddleWidth; |
| 68 | + let paddleHeight; |
| 69 | + let ballRadius; |
| 70 | + let paddleX; |
| 71 | + let ballX; |
| 72 | + let ballY; |
| 73 | + let ballDX = 2; |
| 74 | + let ballDY = 2; |
| 75 | + let score = 0; |
| 76 | + let rightPressed = false; |
| 77 | + let leftPressed = false; |
| 78 | + |
| 79 | + function resizeCanvas() { |
| 80 | + canvas.width = window.innerWidth * 0.8; |
| 81 | + canvas.height = window.innerHeight * 0.6; |
| 82 | + paddleWidth = canvas.width * 0.1; |
| 83 | + paddleHeight = canvas.height * 0.03; |
| 84 | + ballRadius = canvas.width * 0.02; |
| 85 | + paddleX = (canvas.width - paddleWidth) / 2; |
| 86 | + ballX = canvas.width / 2; |
| 87 | + ballY = ballRadius; |
| 88 | + } |
| 89 | + |
| 90 | + resizeCanvas(); |
| 91 | + |
| 92 | + document.addEventListener('keydown', keyDownHandler); |
| 93 | + document.addEventListener('keyup', keyUpHandler); |
| 94 | + canvas.addEventListener('mousemove', mouseMoveHandler); |
| 95 | + window.addEventListener('resize', resizeCanvas); |
| 96 | + document.getElementById('playAgainBtn').addEventListener('click', resetGame); |
| 97 | + |
| 98 | + canvas.addEventListener('touchstart', (e) => { |
| 99 | + const touchX = e.touches[0].clientX - canvas.offsetLeft; |
| 100 | + if (touchX > paddleX + paddleWidth / 2) { |
| 101 | + rightPressed = true; |
| 102 | + } else { |
| 103 | + leftPressed = true; |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + canvas.addEventListener('touchend', () => { |
| 108 | + rightPressed = false; |
| 109 | + leftPressed = false; |
| 110 | + }); |
| 111 | + |
| 112 | + function keyDownHandler(e) { |
| 113 | + if (e.key === 'Right' || e.key === 'ArrowRight') { |
| 114 | + rightPressed = true; |
| 115 | + } else if (e.key === 'Left' || e.key === 'ArrowLeft') { |
| 116 | + leftPressed = true; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + function keyUpHandler(e) { |
| 121 | + if (e.key === 'Right' || e.key === 'ArrowRight') { |
| 122 | + rightPressed = false; |
| 123 | + } else if (e.key === 'Left' || e.key === 'ArrowLeft') { |
| 124 | + leftPressed = false; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + function mouseMoveHandler(e) { |
| 129 | + const relativeX = e.clientX - canvas.offsetLeft; |
| 130 | + if (relativeX > 0 && relativeX < canvas.width) { |
| 131 | + paddleX = relativeX - paddleWidth / 2; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + function drawPaddle() { |
| 136 | + ctx.beginPath(); |
| 137 | + ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight); |
| 138 | + ctx.fillStyle = '#0095DD'; |
| 139 | + ctx.fill(); |
| 140 | + ctx.closePath(); |
| 141 | + } |
| 142 | + |
| 143 | + function drawBall() { |
| 144 | + ctx.beginPath(); |
| 145 | + ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2); |
| 146 | + ctx.fillStyle = '#0095DD'; |
| 147 | + ctx.fill(); |
| 148 | + ctx.closePath(); |
| 149 | + } |
| 150 | + |
| 151 | + function drawScore() { |
| 152 | + ctx.font = '16px Arial'; |
| 153 | + ctx.fillStyle = '#0095DD'; |
| 154 | + ctx.fillText('Score: ' + score, 8, 20); |
| 155 | + } |
| 156 | + |
| 157 | + function draw() { |
| 158 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 159 | + drawPaddle(); |
| 160 | + drawBall(); |
| 161 | + drawScore(); |
| 162 | + |
| 163 | + if (rightPressed && paddleX < canvas.width - paddleWidth) { |
| 164 | + paddleX += 7; |
| 165 | + } else if (leftPressed && paddleX > 0) { |
| 166 | + paddleX -= 7; |
| 167 | + } |
| 168 | + |
| 169 | + ballX += ballDX; |
| 170 | + ballY += ballDY; |
| 171 | + |
| 172 | + if (ballX + ballDX > canvas.width - ballRadius || ballX + ballDX < ballRadius) { |
| 173 | + ballDX = -ballDX; |
| 174 | + } |
| 175 | + |
| 176 | + if (ballY + ballDY < ballRadius) { |
| 177 | + ballDY = -ballDY; |
| 178 | + } else if (ballY + ballDY > canvas.height - ballRadius) { |
| 179 | + if (ballX > paddleX && ballX < paddleX + paddleWidth) { |
| 180 | + ballDY = -ballDY; |
| 181 | + score++; |
| 182 | + } else { |
| 183 | + endGame(); |
| 184 | + return; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + requestAnimationFrame(draw); |
| 189 | + } |
| 190 | + |
| 191 | + function endGame() { |
| 192 | + alert('Game Over! Your score is: ' + score); |
| 193 | + document.getElementById('playAgainBtn').style.display = 'inline-block'; |
| 194 | + } |
| 195 | + |
| 196 | + function resetGame() { |
| 197 | + score = 0; |
| 198 | + ballDX = 2; |
| 199 | + ballDY = 2; |
| 200 | + resizeCanvas(); |
| 201 | + document.getElementById('playAgainBtn').style.display = 'none'; |
| 202 | + draw(); |
| 203 | + } |
| 204 | + |
| 205 | + draw(); |
| 206 | + </script> |
| 207 | +</body> |
| 208 | +</html> |
0 commit comments