<!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>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
color: white;
font-size: 2em;
text-align: center;
display: none;
justify-content: center;
align-items: center;
flex-direction: column;
}
#nextBtn {
margin-top: 30px;
padding: 10px 30px;
font-size: 1em;
border: none;
border-radius: 8px;
background-color: #4CAF50;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="overlay">
<div id="scoreText">Player: 0 - AI: 0</div>
<button id="nextBtn">Start Next Round</button>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
let width = window.innerWidth;
let height = window.innerHeight;
canvas.width = width;
canvas.height = height;
// Game Variables
let playerScore = 0, aiScore = 0;
let lastLoser = 'ai'; // who starts next round
let roundActive = false;
let gameStarted = false;
let aiStartTimeout = null;
// Paddle settings
const PADDLE_RADIUS = 40;
const PUCK_RADIUS = 20;
// Player paddle
let player = {
x: width / 2,
y: height - 100,
radius: PADDLE_RADIUS
};
// AI paddle
let ai = {
x: width / 2,
y: 100,
radius: PADDLE_RADIUS,
targetY: 100,
speed: 6
};
// Puck
let puck = {
x: width / 2,
y: height - 100 - PADDLE_RADIUS - PUCK_RADIUS * 2,
vx: 0,
vy: 0,
radius: PUCK_RADIUS
};
function resetGame() {
if (aiStartTimeout) clearTimeout(aiStartTimeout);
// Set puck depending on who lost
if (lastLoser === 'player') {
puck.x = width / 2;
puck.y = 100 + ai.radius + puck.radius + 10;
puck.vx = 0;
puck.vy = 0;
aiStartTimeout = setTimeout(() => {
const angle = Math.random() * Math.PI / 3 - Math.PI / 6; // -30 to +30 deg
puck.vx = 6 * Math.sin(angle);
puck.vy = 6 * Math.cos(angle);
gameStarted = true;
}, 2000);
} else {
puck.x = width / 2;
puck.y = height - 100 - ai.radius - puck.radius - 10;
puck.vx = 0;
puck.vy = 0;
gameStarted = false;
}
roundActive = true;
}
function endRound(winner) {
roundActive = false;
if (winner === 'player') playerScore++;
else aiScore++;
document.getElementById('scoreText').textContent =
`Player: ${playerScore} - AI: ${aiScore}`;
document.getElementById('overlay').style.display = 'flex';
}
document.getElementById('nextBtn').addEventListener('click', () => {
lastLoser = (playerScore > aiScore) ? 'ai' : 'player';
document.getElementById('overlay').style.display = 'none';
resetGame();
});
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
resetGame();
});
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
if (!roundActive || gameStarted) return;
const touch = e.touches[0];
const dx = touch.clientX - puck.x;
const dy = touch.clientY - puck.y;
const dist = Math.sqrt(dx*dx + dy*dy);
if (dist < puck.radius + 20) {
const angle = (Math.random() * Math.PI / 3) - Math.PI / 6;
puck.vx = 6 * Math.sin(angle);
puck.vy = -6 * Math.cos(angle);
gameStarted = true;
}
}, { passive: false });
canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
if (!roundActive) return;
const touch = e.touches[0];
player.x = touch.clientX;
player.y = touch.clientY;
}, { passive: false });
function updateAI() {
// AI stays at back unless puck is slow or near
if (puck.y < height / 2 && Math.abs(puck.vx) + Math.abs(puck.vy) < 1.5) {
ai.targetY = puck.y;
} else {
ai.targetY = 100;
}
let dy = ai.targetY - ai.y;
if (Math.abs(dy) > ai.speed) {
ai.y += Math.sign(dy) * ai.speed;
} else {
ai.y = ai.targetY;
}
ai.x = width / 2;
// Simple collision with puck
const dx = puck.x - ai.x;
const dy2 = puck.y - ai.y;
const dist = Math.sqrt(dx*dx + dy2*dy2);
if (dist < ai.radius + puck.radius && puck.vy < 0) {
const angle = (Math.random() * Math.PI / 3) - Math.PI / 6;
puck.vx = 6 * Math.sin(angle);
puck.vy = 6 * Math.cos(angle);
}
}
function updatePuck() {
if (!gameStarted) return;
puck.x += puck.vx;
puck.y += puck.vy;
// Wall collisions
if (puck.x < puck.radius) {
puck.x = puck.radius;
puck.vx *= -1;
}
if (puck.x > width - puck.radius) {
puck.x = width - puck.radius;
puck.vx *= -1;
}
if (puck.y < puck.radius) {
puck.y = puck.radius;
puck.vy *= -1;
}
if (puck.y > height - puck.radius) {
puck.y = height - puck.radius;
puck.vy *= -1;
}
// Check goals
if (puck.y < 20) {
endRound('player');
}
if (puck.y > height - 20) {
endRound('ai');
}
}
function draw() {
ctx.clearRect(0, 0, width, height);
// Center line
ctx.strokeStyle = "#fff";
ctx.beginPath();
ctx.moveTo(0, height / 2);
ctx.lineTo(width, height / 2);
ctx.stroke();
// Draw paddles
ctx.fillStyle = "#ffcc00";
ctx.beginPath();
ctx.arc(player.x, player.y, player.radius, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#ff4444";
ctx.beginPath();
ctx.arc(ai.x, ai.y, ai.radius, 0, Math.PI * 2);
ctx.fill();
// Draw puck
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.arc(puck.x, puck.y, puck.radius, 0, Math.PI * 2);
ctx.fill();
// Score
ctx.fillStyle = "#fff";
ctx.font = "20px Arial";
ctx.fillText(`Player: ${playerScore}`, 20, 30);
ctx.fillText(`AI: ${aiScore}`, width - 100, 30);
}
function loop() {
if (roundActive) {
updateAI();
updatePuck();
}
draw();
requestAnimationFrame(loop);
}
resetGame();
loop();
</script>
</body>
</html>