anycoder-c789af13 / index.html
brenoppl's picture
Upload folder using huggingface_hub
b722d07 verified
Raw History Blame Contribute Delete
24.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Neon Cyber Pong</title>
<!-- Importing FontAwesome for Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root {
--bg-color: #050505;
--primary-color: #00f3ff; /* Cyan */
--secondary-color: #ff0055; /* Magenta */
--text-color: #ffffff;
--glass-bg: rgba(255, 255, 255, 0.05);
--glass-border: rgba(255, 255, 255, 0.1);
--font-main: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: var(--font-main);
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
/* --- Header & Nav --- */
header {
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
background: rgba(0, 0, 0, 0.8);
border-bottom: 1px solid var(--glass-border);
z-index: 10;
}
.logo {
font-size: 1.5rem;
font-weight: 800;
letter-spacing: 2px;
text-transform: uppercase;
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 20px rgba(0, 243, 255, 0.3);
}
.built-with {
font-size: 0.8rem;
color: #888;
text-decoration: none;
transition: color 0.3s;
}
.built-with:hover {
color: var(--primary-color);
}
/* --- Main Game Area --- */
main {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
position: relative;
padding: 20px;
}
.game-container {
position: relative;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.8);
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--glass-border);
}
canvas {
display: block;
background: radial-gradient(circle at center, #1a1a2e 0%, #000000 100%);
}
/* --- UI Overlays (Start, Pause, Game Over) --- */
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(5, 5, 5, 0.85);
backdrop-filter: blur(8px);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 20;
transition: opacity 0.3s ease;
}
.hidden {
opacity: 0;
pointer-events: none;
}
.overlay h1 {
font-size: 3rem;
margin-bottom: 1rem;
text-transform: uppercase;
letter-spacing: 5px;
}
.overlay p {
font-size: 1.2rem;
margin-bottom: 2rem;
color: #ccc;
}
.btn {
padding: 12px 30px;
font-size: 1rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
display: inline-flex;
align-items: center;
gap: 10px;
}
.btn-primary {
background: var(--primary-color);
color: #000;
box-shadow: 0 0 15px rgba(0, 243, 255, 0.4);
}
.btn-primary:hover {
background: #fff;
box-shadow: 0 0 25px rgba(0, 243, 255, 0.8);
transform: translateY(-2px);
}
.btn-secondary {
background: transparent;
border: 1px solid var(--secondary-color);
color: var(--secondary-color);
margin-top: 10px;
}
.btn-secondary:hover {
background: var(--secondary-color);
color: #fff;
box-shadow: 0 0 15px rgba(255, 0, 85, 0.4);
}
/* --- Score Board --- */
.score-board {
position: absolute;
top: 20px;
width: 100%;
display: flex;
justify-content: center;
gap: 100px;
pointer-events: none;
font-size: 4rem;
font-weight: 900;
font-family: monospace;
z-index: 5;
}
.score-player { color: var(--primary-color); text-shadow: 0 0 20px var(--primary-color); }
.score-ai { color: var(--secondary-color); text-shadow: 0 0 20px var(--secondary-color); }
/* --- Controls Footer --- */
footer {
padding: 1rem;
text-align: center;
background: rgba(0, 0, 0, 0.8);
border-top: 1px solid var(--glass-border);
color: #666;
font-size: 0.9rem;
}
.controls-hint {
display: flex;
justify-content: center;
gap: 20px;
margin-bottom: 5px;
}
.key {
background: #222;
padding: 2px 8px;
border-radius: 4px;
border: 1px solid #444;
color: #fff;
font-family: monospace;
font-weight: bold;
}
/* --- Mobile Controls --- */
.mobile-controls {
display: none; /* Hidden on desktop by default */
position: absolute;
bottom: 20px;
width: 100%;
justify-content: space-between;
padding: 0 40px;
pointer-events: none;
z-index: 15;
}
.touch-btn {
width: 60px;
height: 60px;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
color: white;
pointer-events: auto;
user-select: none;
-webkit-user-select: none;
}
.touch-btn:active {
background: rgba(0, 243, 255, 0.3);
}
@media (max-width: 768px) {
.mobile-controls {
display: flex;
}
.score-board {
font-size: 2.5rem;
gap: 50px;
}
.overlay h1 { font-size: 2rem; }
.game-container {
width: 100%;
height: 100%;
border: none;
}
canvas {
width: 100%;
height: 100%;
}
}
</style>
</head>
<body>
<header>
<div class="logo"><i class="fas fa-table-tennis-paddle-ball"></i> Neon Pong</div>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="built-with">Built with anycoder</a>
</header>
<main>
<div class="score-board">
<div id="scorePlayer" class="score-player">0</div>
<div id="scoreAI" class="score-ai">0</div>
</div>
<div class="game-container">
<canvas id="gameCanvas"></canvas>
<!-- Start Screen -->
<div id="startScreen" class="overlay">
<h1>Neon Pong</h1>
<p>First to 5 wins</p>
<button id="startBtn" class="btn btn-primary">
<i class="fas fa-play"></i> Start Game
</button>
</div>
<!-- Pause Screen -->
<div id="pauseScreen" class="overlay hidden">
<h1>Paused</h1>
<button id="resumeBtn" class="btn btn-primary">
<i class="fas fa-play"></i> Resume
</button>
<button id="quitBtn" class="btn btn-secondary">
<i class="fas fa-times"></i> Quit
</button>
</div>
<!-- Game Over Screen -->
<div id="gameOverScreen" class="overlay hidden">
<h1 id="winnerText">Player Wins!</h1>
<p>Final Score: <span id="finalScore">5 - 3</span></p>
<button id="restartBtn" class="btn btn-primary">
<i class="fas fa-redo"></i> Play Again
</button>
</div>
</div>
<!-- Mobile Controls -->
<div class="mobile-controls">
<div class="touch-btn" id="btnLeft"><i class="fas fa-chevron-down"></i></div>
<div class="touch-btn" id="btnRight"><i class="fas fa-chevron-up"></i></div>
</div>
</main>
<footer>
<div class="controls-hint">
<span><span class="key">W</span> Up</span>
<span><span class="key">S</span> Down</span>
<span><span class="key">Space</span> Pause</span>
</div>
<div>Use Mouse or Touch to control the paddle</div>
</footer>
<script>
/**
* NEON PONG GAME ENGINE
* Handles rendering, physics, particles, and game state management.
*/
// --- Configuration ---
const config = {
winScore: 5,
paddleSpeed: 8,
aiSpeed: 5,
ballSpeedBase: 6,
ballSpeedMax: 12,
colors: {
primary: '#00f3ff',
secondary: '#ff0055',
ball: '#ffffff'
}
};
// --- DOM Elements ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scorePlayerEl = document.getElementById('scorePlayer');
const scoreAiEl = document.getElementById('scoreAI');
const startScreen = document.getElementById('startScreen');
const pauseScreen = document.getElementById('pauseScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const winnerText = document.getElementById('winnerText');
const finalScore = document.getElementById('finalScore');
// --- Game State ---
let gameState = 'MENU'; // MENU, PLAYING, PAUSED, GAMEOVER
let animationId;
let lastTime = 0;
// --- Resize Logic ---
function resizeCanvas() {
// Keep aspect ratio roughly 3:4 or fit to container
const container = document.querySelector('.game-container');
// We set internal resolution higher for crisp rendering on high DPI
const dpr = window.devicePixelRatio || 1;
// Logical size
let width = Math.min(800, window.innerWidth - 40);
let height = Math.min(600, window.innerHeight - 150);
// Adjust for mobile
if (window.innerWidth < 768) {
width = window.innerWidth - 40;
height = width * 0.75;
}
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
canvas.width = width * dpr;
canvas.height = height * dpr;
ctx.scale(dpr, dpr);
// Store logical size for physics
return { width, height };
}
let canvasSize = resizeCanvas();
window.addEventListener('resize', () => { canvasSize = resizeCanvas(); resetBall(); });
// --- Game Objects ---
const player = {
x: 20,
y: 0,
width: 10,
height: 80,
score: 0,
color: config.colors.primary,
dy: 0
};
const ai = {
x: 0, // Set in init
y: 0,
width: 10,
height: 80,
score: 0,
color: config.colors.secondary,
speed: config.aiSpeed
};
const ball = {
x: 0,
y: 0,
radius: 6,
speed: config.ballSpeedBase,
dx: 0,
dy: 0,
trail: [] // For visual effect
};
let particles = []; // Array for explosion effects
// --- Initialization ---
function init() {
const { width, height } = canvasSize;
player.y = height / 2 - player.height / 2;
ai.x = width - 30;
ai.y = height / 2 - ai.height / 2;
resetBall();
updateScoreDisplay();
}
function resetBall() {
const { width, height } = canvasSize;
ball.x = width / 2;
ball.y = height / 2;
ball.speed = config.ballSpeedBase;
ball.trail = [];
// Random direction
const dirX = Math.random() > 0.5 ? 1 : -1;
const dirY = (Math.random() * 2 - 1); // -1 to 1
// Normalize and apply speed
const len = Math.sqrt(dirX*dirX + dirY*dirY);
ball.dx = (dirX / len) * ball.speed;
ball.dy = (dirY / len) * ball.speed;
}
// --- Particle System ---
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.size = Math.random() * 3 + 1;
this.speedX = (Math.random() - 0.5) * 8;
this.speedY = (Math.random() - 0.5) * 8;
this.life = 1.0; // Opacity
this.decay = Math.random() * 0.03 + 0.01;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.life -= this.decay;
this.size *= 0.95;
}
draw(ctx) {
ctx.save();
ctx.globalAlpha = this.life;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
function createExplosion(x, y, color) {
for (let i = 0; i < 15; i++) {
particles.push(new Particle(x, y, color));
}
}
// --- Physics & Logic ---
function update() {
if (gameState !== 'PLAYING') return;
const { width, height } = canvasSize;
// Move Player
player.y += player.dy;
// Clamp Player
if (player.y < 0) player.y = 0;
if (player.y + player.height > height) player.y = height - player.height;
// Move AI (Simple tracking with delay)
const centerAi = ai.y + ai.height / 2;
if (centerAi < ball.y - 10) {
ai.y += ai.speed;
} else if (centerAi > ball.y + 10) {
ai.y -= ai.speed;
}
// Clamp AI
if (ai.y < 0) ai.y = 0;
if (ai.y + ai.height > height) ai.y = height - ai.height;
// Move Ball
ball.x += ball.dx;
ball.y += ball.dy;
// Ball Trail Logic
ball.trail.push({x: ball.x, y: ball.y});
if (ball.trail.length > 10) ball.trail.shift();
// Wall Collisions (Top/Bottom)
if (ball.y - ball.radius < 0 || ball.y + ball.radius > height) {
ball.dy *= -1;
// Keep inside
ball.y = ball.y < 0 ? ball.radius : height - ball.radius;
}
// Paddle Collisions
// Player
if (ball.x - ball.radius < player.x + player.width &&
ball.x + ball.radius > player.x &&
ball.y > player.y &&
ball.y < player.y + player.height) {
// Calculate impact point (-1 to 1)
let collidePoint = (ball.y - (player.y + player.height/2));
collidePoint = collidePoint / (player.height/2);
let angleRad = (Math.PI/4) * collidePoint;
let direction = 1; // Right
ball.dx = direction * ball.speed * Math.cos(angleRad);
ball.dy = ball.speed * Math.sin(angleRad);
// Increase speed slightly
ball.speed = Math.min(ball.speed + 0.5, config.ballSpeedMax);
// Visuals
createExplosion(ball.x, ball.y, player.color);
}
// AI
if (ball.x + ball.radius > ai.x &&
ball.x - ball.radius < ai.x + ai.width &&
ball.y > ai.y &&
ball.y < ai.y + ai.height) {
let collidePoint = (ball.y - (ai.y + ai.height/2));
collidePoint = collidePoint / (ai.height/2);
let angleRad = (Math.PI/4) * collidePoint;
let direction = -1; // Left
ball.dx = direction * ball.speed * Math.cos(angleRad);
ball.dy = ball.speed * Math.sin(angleRad);
ball.speed = Math.min(ball.speed + 0.5, config.ballSpeedMax);
createExplosion(ball.x, ball.y, ai.color);
}
// Scoring
if (ball.x < 0) {
ai.score++;
handleScore();
} else if (ball.x > width) {
player.score++;
handleScore();
}
// Update Particles
particles.forEach((p, index) => {
p.update();
if (p.life <= 0) particles.splice(index, 1);
});
}
function handleScore() {
createExplosion(ball.x, ball.y, '#ffffff');
updateScoreDisplay();
if (player.score >= config.winScore || ai.score >= config.winScore) {
endGame();
} else {
resetBall();
}
}
function updateScoreDisplay() {
scorePlayerEl.textContent = player.score;
scoreAiEl.textContent = ai.score;
}
function endGame() {
gameState = 'GAMEOVER';
const winner = player.score >= config.winScore ? 'Player' : 'AI';
winnerText.textContent = `${winner} Wins!`;
winnerText.style.color = player.score >= config.winScore ? config.colors.primary : config.colors.secondary;
finalScore.textContent = `${player.score} - ${ai.score}`;
gameOverScreen.classList.remove('hidden');
}
// --- Rendering ---
function draw() {
const { width, height } = canvasSize;
// Clear Canvas
ctx.clearRect(0, 0, width, height);
// Draw Center Line
ctx.beginPath();
ctx.setLineDash([10, 10]);
ctx.moveTo(width / 2, 0);
ctx.lineTo(width / 2, height);
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.setLineDash([]);
// Draw Player Paddle
ctx.shadowBlur = 15;
ctx.shadowColor = player.color;
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Draw AI Paddle
ctx.shadowColor = ai.color;
ctx.fillStyle = ai.color;
ctx.fillRect(ai.x, ai.y, ai.width, ai.height);
// Draw Ball Trail
if (gameState === 'PLAYING') {
ball.trail.forEach((pos, index) => {
const alpha = index / ball.trail.length;
ctx.beginPath();
ctx.arc(pos.x, pos.y, ball.radius * alpha, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${alpha * 0.5})`;
ctx.fill();
});
// Draw Ball
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = config.colors.ball;
ctx.shadowColor = config.colors.ball;
ctx.shadowBlur = 10;
ctx.fill();
}
// Draw Particles
particles.forEach(p => p.draw(ctx));
// Reset Shadow for next frame performance
ctx.shadowBlur = 0;
}
function gameLoop(timestamp) {
if (gameState === 'PLAYING') {
update();
}
draw();
animationId = requestAnimationFrame(gameLoop);
}
// --- Input Handling ---
// Keyboard
window.addEventListener('keydown', (e) => {
if (gameState === 'PLAYING') {
if (e.key === 'w' || e.key === 'W' || e.key === 'ArrowUp') player.dy = -config.paddleSpeed;
if (e.key === 's' || e.key === 'S' || e.key === 'ArrowDown') player.dy = config.paddleSpeed;
}
if (e.code === 'Space') {
togglePause();
}
});
window.addEventListener('keyup', (e) => {
if (e.key === 'w' || e.key === 'W' || e.key === 'ArrowUp' ||
e.key === 's' || e.key === 'S' || e.key === 'ArrowDown') {
player.dy = 0;
}
});
// Mouse
canvas.addEventListener('mousemove', (e) => {
if (gameState !== 'PLAYING') return;
const rect = canvas.getBoundingClientRect();
const scaleY = canvas.height / rect.height; // Scale logic if needed
// Calculate mouse position relative to canvas
const mouseY = e.clientY - rect.top;
// Center paddle on mouse
player.y = mouseY - player.height / 2;
});
// Touch
const btnLeft = document.getElementById('btnLeft');
const btnRight = document.getElementById('btnRight');
btnLeft.addEventListener('touchstart', (e) => { e.preventDefault(); player.dy = config.paddleSpeed; });
btnLeft.addEventListener('touchend', (e) => { e.preventDefault(); player.dy = 0; });
btnRight.addEventListener('touchstart', (e) => { e.preventDefault(); player.dy = -config.paddleSpeed; });
btnRight.addEventListener('touchend', (e) => { e.preventDefault(); player.dy = 0; });
// --- UI Logic ---
function togglePause() {
if (gameState === 'PLAYING') {
gameState = 'PAUSED';
pauseScreen.classList.remove('hidden');
} else if (gameState === 'PAUSED') {
gameState = 'PLAYING';
pauseScreen.classList.add('hidden');
}
}
document.getElementById('startBtn').addEventListener('click', () => {
player.score = 0;
ai.score = 0;
init();
startScreen.classList.add('hidden');
gameState = 'PLAYING';
});
document.getElementById('resumeBtn').addEventListener('click', togglePause);
document.getElementById('quitBtn').addEventListener('click', () => {
gameState = 'MENU';
startScreen.classList.remove('hidden');
pauseScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
player.score = 0;
ai.score = 0;
updateScoreDisplay();
});
document.getElementById('restartBtn').addEventListener('click', () => {
player.score = 0;
ai.score = 0;
init();
gameOverScreen.classList.add('hidden');
gameState = 'PLAYING';
});
// --- Start ---
init();
gameLoop();
</script>
</body>
</html>