i want to create an air hockey game for mobile where player would use his thumb/finger…
- User
i want to create an air hockey game for mobile where player would use his thumb/finger to move the circle to hit the "ball/ puck". The players paddle should follow the players thumb/finger and should be very fast to keep up. I dont want to use game engines like unity, i want simple ways: javascript with libraries with cdn links in single html file. The game should have an AI-player that will play against the player. The game field should be on full screen of the phone, the scores and buttons should appear only after the round/game ends. After the game starts the puck should be in still position (not moving) in front of the player - by touching the puck the game starts. When the round ends the score should appear and the "start next round" button should appear as well. The round starts again with standing still puck but now in front of the player who lost the previous round. The AI should also start the round with the initial hit if he lost the previous round (add 2 second delay before he makes a hit so the user can be prepared at the start of the round). The Ai should not just bluntly move towards the puck, he should move on backside and move up and down only if the puck slows down or stops on his field. I think this is simple and intuitive way to make a game like that. I have linux mint cinnamon and samsung galaxy core 03
Assistant
Sure! Here's a complete HTML file for your mobile air hockey game using plain JavaScript and the HTML5 Canvas. This code includes touch controls for the player paddle, an AI opponent, full-screen display, scoring, and round restarts. The AI behaves as specified, moving to the backside and only moving up and down if the puck slows down or stops on its field.
Save the following code as an HTML file and open it in your mobile browser:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>Mobile Air Hockey</title> <style> body, html { margin: 0; padding: 0; overflow: hidden; background-color: #222; } canvas { display: block; } #overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.8); color: white; font-size: 2em; text-align: center; display: flex; flex-direction: column; justify-content: center; align-items: center; display: none; } #score { font-size: 3em; } #startButton { font-size: 1.5em; padding: 10px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <canvas id="gameCanvas"></canvas> <div id="overlay"> <div id="score"></div> <button id="startButton">Start Next Round</button> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const overlay = document.getElementById('overlay'); const scoreDisplay = document.getElementById('score'); const startButton = document.getElementById('startButton'); let canvasWidth, canvasHeight; let playerScore = 0; let aiScore = 0; let playerPaddle = { x: 0, y: 0, radius: 30 }; let aiPaddle = { x: 0, y: 0, radius: 30 }; let puck = { x: 0, y: 0, radius: 15, dx: 0, dy: 0 }; let gameStarted = false; let roundActive = false; let aiStartsRound = false; let lastRoundLoser = null; function resizeCanvas() { canvasWidth = window.innerWidth; canvasHeight = window.innerHeight; canvas.width = canvasWidth; canvas.height = canvasHeight; resetGame(); } function resetGame() { playerPaddle.x = canvasWidth / 2; playerPaddle.y = canvasHeight - playerPaddle.radius - 10; aiPaddle.x = canvasWidth / 2; aiPaddle.y = aiPaddle.radius + 10; puck.x = canvasWidth / 2; puck.y = canvasHeight / 2; puck.dx = 0; puck.dy = 0; } function startRound() { roundActive = true; gameStarted = false; if (aiStartsRound) { puck.x = aiPaddle.x; puck.y = aiPaddle.y + aiPaddle.radius + puck.radius + 10; setTimeout(() => { puck.dx = 5; puck.dy = 0; gameStarted = true; }, 2000); } else { puck.x = playerPaddle.x; puck.y = playerPaddle.y - playerPaddle.radius - puck.radius - 10; } } function endRound(loser) { roundActive = false; if (loser === 'player') { aiScore++; aiStartsRound = true; } else if (loser === 'ai') { playerScore++; aiStartsRound = false; } lastRoundLoser = loser; scoreDisplay.innerHTML = `Player: ${playerScore}<br>AI: ${aiScore}`; overlay.style.display = 'flex'; } function update() { if (!roundActive) return; if (gameStarted) { puck.x += puck.dx; puck.y += puck.dy; // AI logic if (puck.x > canvasWidth / 2) { if (Math.abs(puck.dx) + Math.abs(puck.dy) < 2) { aiPaddle.y = puck.y; } else { aiPaddle.y = canvasHeight / 2; } aiPaddle.y = Math.max(aiPaddle.radius, Math.min(aiPaddle.y, canvasHeight - aiPaddle.radius)); } // Puck collision with walls if (puck.y - puck.radius < 0 || puck.y + puck.radius > canvasHeight) { puck.dy = -puck.dy; } // Puck collision with paddles if (distance(puck, playerPaddle) < puck.radius + playerPaddle.radius) { let angle = Math.atan2(puck.y - playerPaddle.y, puck.x - playerPaddle.x); puck.dx = 5 * Math.cos(angle); puck.dy = 5 * Math.sin(angle); } if (distance(puck, aiPaddle) < puck.radius + aiPaddle.radius) { let angle = Math.atan2(puck.y - aiPaddle.y, puck.x - aiPaddle.x); puck.dx = -5 * Math.cos(angle); puck.dy = -5 * Math.sin(angle); } // Check for goals if (puck.x - puck.radius < 0) { endRound('player'); } else if (puck.x + puck.radius > canvasWidth) { endRound('ai'); } } } function draw() { ctx.clearRect(0, 0, canvasWidth, canvasHeight); // Draw field ctx.fillStyle = '#333'; ctx.fillRect(0, 0, canvasWidth, canvasHeight); // Draw center line ctx.beginPath(); ctx.setLineDash([10, 15]); ctx.moveTo(canvasWidth / 2, 0); ctx.lineTo(canvasWidth / 2, canvasHeight); ctx.strokeStyle = 'white'; ctx.stroke(); // Draw player paddle ctx.beginPath(); ctx.arc(playerPaddle.x, playerPaddle.y, playerPaddle.radius, 0, Math.PI * 2); ctx.fillStyle = 'yellow'; ctx.fill(); // Draw AI paddle ctx.beginPath(); ctx.arc(aiPaddle.x, aiPaddle.y, aiPaddle.radius, 0, Math.PI * 2); ctx.fillStyle = 'red'; ctx.fill(); // Draw puck ctx.beginPath(); ctx.arc(puck.x, puck.y, puck.radius, 0, Math.PI * 2); ctx.fillStyle = 'white'; ctx.fill(); } function distance(a, b) { return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)); } canvas.addEventListener('touchstart', (e) => { if (!roundActive) return; const touch = e.touches[0]; if (!gameStarted && distance({ x: touch.clientX, y: touch.clientY }, puck) < puck.radius) { puck.dx = 5; puck.dy = 0; gameStarted = true; } playerPaddle.x = touch.clientX; playerPaddle.y = touch.clientY; }); canvas.addEventListener('touchmove', (e) => { if (!roundActive) return; const touch = e.touches[0]; playerPaddle.x = touch.clientX; playerPaddle.y = touch.clientY; }); startButton.addEventListener('click', () => { overlay.style.display = 'none'; startRound(); }); function gameLoop() { update(); draw(); requestAnimationFrame(gameLoop); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); gameLoop(); </script> </body> </html>Features:
- Full-Screen Canvas: The game occupies the entire screen on mobile devices.
- Touch Controls: The player's paddle follows the touch position.
- AI Opponent: The AI moves to the back of its side and adjusts when the puck approaches.
- Game Flow:
- The puck starts stationary and is hit by the player or AI.
- The game ends when the puck crosses a boundary, and the score is displayed.
- A "Start Next Round" button appears after each round.
- The next round starts with the puck in front of the previous loser.
- The AI gets a 2-second delay if it starts the round.
How to Use:
- Save the code as an
.htmlfile. - Open it on your Samsung Galaxy Core 03 (or any mobile browser).
- Touch the puck to start the game.
- Play against the AI, and restart rounds using the "Start Next Round" button.
This implementation is lightweight and uses only vanilla JavaScript, making it easy to modify or expand.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.