anycoder-25d44456 / index.html
ishash's picture
Upload folder using huggingface_hub
1ae57f3 verified
Raw History Blame Contribute Delete
18.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Neon Snake | Ultimate Web Experience</title>
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Rajdhani:wght@500;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color: #050510;
--grid-color: rgba(255, 255, 255, 0.05);
--primary-color: #00f3ff;
--secondary-color: #bc13fe;
--accent-color: #ffe600;
--text-color: #ffffff;
--glass-bg: rgba(255, 255, 255, 0.05);
--glass-border: rgba(255, 255, 255, 0.1);
--font-display: 'Orbitron', sans-serif;
--font-body: 'Rajdhani', sans-serif;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
user-select: none; /* Prevent selection on touch */
-webkit-tap-highlight-color: transparent;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: var(--font-body);
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
background-image:
linear-gradient(var(--grid-color) 1px, transparent 1px),
linear-gradient(90deg, var(--grid-color) 1px, transparent 1px);
background-size: 40px 40px;
}
/* Header */
header {
width: 100%;
max-width: 600px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
margin-bottom: 10px;
background: var(--glass-bg);
backdrop-filter: blur(10px);
border: 1px solid var(--glass-border);
border-radius: 16px;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
z-index: 10;
}
h1 {
font-family: var(--font-display);
font-size: 1.5rem;
text-transform: uppercase;
letter-spacing: 2px;
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);
}
.score-board {
display: flex;
gap: 20px;
font-family: var(--font-display);
font-size: 1rem;
}
.score-item span {
color: var(--primary-color);
}
/* Game Container */
.game-container {
position: relative;
padding: 4px;
background: linear-gradient(45deg, var(--secondary-color), var(--primary-color));
border-radius: 8px;
box-shadow: 0 0 30px rgba(188, 19, 254, 0.2);
}
canvas {
display: block;
background-color: rgba(0, 0, 0, 0.8);
border-radius: 4px;
cursor: pointer;
}
/* Overlays (Start, Game Over, Pause) */
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(5, 5, 16, 0.85);
backdrop-filter: blur(5px);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 4px;
z-index: 20;
transition: opacity 0.3s ease;
}
.hidden {
opacity: 0;
pointer-events: none;
}
.overlay h2 {
font-family: var(--font-display);
font-size: 3rem;
margin-bottom: 10px;
color: var(--text-color);
text-shadow: 0 0 15px var(--primary-color);
}
.overlay p {
font-size: 1.2rem;
margin-bottom: 30px;
color: #ccc;
text-align: center;
max-width: 80%;
}
.btn {
padding: 12px 30px;
font-family: var(--font-display);
font-size: 1.1rem;
font-weight: bold;
color: var(--bg-color);
background: var(--primary-color);
border: none;
border-radius: 50px;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 1px;
box-shadow: 0 0 15px rgba(0, 243, 255, 0.4);
transition: all 0.2s ease;
}
.btn:hover {
transform: scale(1.05);
background: #fff;
box-shadow: 0 0 25px rgba(255, 255, 255, 0.6);
}
.btn:active {
transform: scale(0.95);
}
/* Mobile Controls */
.mobile-controls {
display: none; /* Hidden by default on desktop */
margin-top: 20px;
grid-template-areas:
". up ."
"left down right";
gap: 10px;
}
.ctrl-btn {
width: 60px;
height: 60px;
background: var(--glass-bg);
border: 1px solid var(--glass-border);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
color: var(--primary-color);
cursor: pointer;
transition: background 0.1s;
}
.ctrl-btn:active {
background: rgba(0, 243, 255, 0.2);
}
/* Responsive Breakpoints */
@media (max-width: 768px) {
.mobile-controls {
display: grid;
}
header {
padding: 10px;
margin-bottom: 20px;
}
h1 {
font-size: 1.2rem;
}
.game-container {
padding: 2px;
}
}
/* Footer Credit */
footer {
position: absolute;
bottom: 10px;
font-size: 0.8rem;
color: rgba(255, 255, 255, 0.4);
text-decoration: none;
transition: color 0.3s;
}
footer:hover {
color: var(--primary-color);
}
</style>
</head>
<body>
<header>
<h1>Neon Snake</h1>
<div class="score-board">
<div class="score-item">Score: <span id="scoreEl">0</span></div>
<div class="score-item">High: <span id="highScoreEl">0</span></div>
</div>
</header>
<main>
<div class="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<!-- Start Screen -->
<div id="startScreen" class="overlay">
<h2>NEON SNAKE</h2>
<p>Use Arrow Keys or Swipe to Move<br>Eat Energy to Grow</p>
<button class="btn" id="startBtn">Start Game</button>
</div>
<!-- Game Over Screen -->
<div id="gameOverScreen" class="overlay hidden">
<h2 style="color: #ff3366; text-shadow: 0 0 15px #ff3366;">GAME OVER</h2>
<p>Final Score: <span id="finalScore" style="color:white; font-weight:bold;">0</span></p>
<button class="btn" id="restartBtn">Try Again</button>
</div>
</div>
<!-- Mobile Controls -->
<div class="mobile-controls">
<div class="ctrl-btn" style="grid-area: up;" data-dir="up">▲</div>
<div class="ctrl-btn" style="grid-area: left;" data-dir="left">◀</div>
<div class="ctrl-btn" style="grid-area: down;" data-dir="down">▼</div>
<div class="ctrl-btn" style="grid-area: right;" data-dir="right">▶</div>
</div>
</main>
<footer>
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: inherit;">anycoder</a>
</footer>
<script>
// --- Configuration & State ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// UI Elements
const scoreEl = document.getElementById('scoreEl');
const highScoreEl = document.getElementById('highScoreEl');
const finalScoreEl = document.getElementById('finalScore');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const startBtn = document.getElementById('startBtn');
const restartBtn = document.getElementById('restartBtn');
const mobileBtns = document.querySelectorAll('.ctrl-btn');
// Game Constants
const GRID_SIZE = 20;
const TILE_COUNT = canvas.width / GRID_SIZE;
const GAME_SPEED = 100; // ms per frame
// Game State
let snake = [];
let food = { x: 0, y: 0 };
let dx = 0;
let dy = 0;
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameInterval;
let isGameRunning = false;
let nextDirection = { x: 0, y: 0 }; // Buffer for input to prevent self-collision on quick turns
// --- Initialization ---
highScoreEl.innerText = highScore;
function initGame() {
// Reset Snake to center
snake = [
{ x: 10, y: 10 },
{ x: 10, y: 11 },
{ x: 10, y: 12 }
];
// Initial movement (up)
dx = 0;
dy = -1;
nextDirection = { x: 0, y: -1 };
score = 0;
scoreEl.innerText = score;
placeFood();
isGameRunning = true;
startScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
if (gameInterval) clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, GAME_SPEED);
}
// --- Core Game Loop ---
function gameLoop() {
if (!isGameRunning) return;
update();
draw();
}
function update() {
// Apply buffered direction
dx = nextDirection.x;
dy = nextDirection.y;
// Calculate new head position
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// 1. Check Wall Collision
if (head.x < 0 || head.x >= TILE_COUNT || head.y < 0 || head.y >= TILE_COUNT) {
gameOver();
return;
}
// 2. Check Self Collision
for (let i = 0; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
return;
}
}
// Add new head
snake.unshift(head);
// 3. Check Food Collision
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreEl.innerText = score;
placeFood();
// Don't pop the tail, so snake grows
} else {
// Remove tail
snake.pop();
}
}
function draw() {
// Clear Screen
ctx.fillStyle = 'rgba(5, 5, 16, 0.8)'; // Clear with slight opacity for trail effect if desired, or solid
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Grid (Subtle)
ctx.strokeStyle = 'rgba(255, 255, 255, 0.03)';
ctx.lineWidth = 1;
for (let i = 0; i < TILE_COUNT; i++) {
ctx.beginPath();
ctx.moveTo(i * GRID_SIZE, 0);
ctx.lineTo(i * GRID_SIZE, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * GRID_SIZE);
ctx.lineTo(canvas.width, i * GRID_SIZE);
ctx.stroke();
}
// Draw Food
ctx.fillStyle = '#ff3366';
ctx.shadowColor = '#ff3366';
ctx.shadowBlur = 15;
ctx.beginPath();
ctx.arc(
food.x * GRID_SIZE + GRID_SIZE / 2,
food.y * GRID_SIZE + GRID_SIZE / 2,
GRID_SIZE / 2 - 2,
0, Math.PI * 2
);
ctx.fill();
ctx.shadowBlur = 0; // Reset shadow
// Draw Snake
snake.forEach((segment, index) => {
// Gradient for snake body
if (index === 0) {
// Head
ctx.fillStyle = '#ffffff';
ctx.shadowColor = '#00f3ff';
ctx.shadowBlur = 10;
} else {
// Body
// Opacity based on index to taper tail
const opacity = 1 - (index / (snake.length + 5));
ctx.fillStyle = `rgba(0, 243, 255, ${Math.max(0.3, opacity)})`;
ctx.shadowBlur = 0;
}
ctx.fillRect(
segment.x * GRID_SIZE + 1,
segment.y * GRID_SIZE + 1,
GRID_SIZE - 2,
GRID_SIZE - 2
);
});
}
// --- Helpers ---
function placeFood() {
let validPosition = false;
while (!validPosition) {
food.x = Math.floor(Math.random() * TILE_COUNT);
food.y = Math.floor(Math.random() * TILE_COUNT);
// Ensure food doesn't spawn on snake body
validPosition = true;
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
validPosition = false;
break;
}
}
}
}
function gameOver() {
isGameRunning = false;
clearInterval(gameInterval);
// Update High Score
if (score > highScore) {
highScore = score;
localStorage.setItem('snakeHighScore', highScore);
highScoreEl.innerText = highScore;
}
finalScoreEl.innerText = score;
gameOverScreen.classList.remove('hidden');
}
// --- Input Handling ---
function changeDirection(direction) {
if (!isGameRunning) return;
// Prevent reversing directly
if (direction === 'up' && dy === 0) {
nextDirection = { x: 0, y: -1 };
} else if (direction === 'down' && dy === 0) {
nextDirection = { x: 0, y: 1 };
} else if (direction === 'left' && dx === 0) {
nextDirection = { x: -1, y: 0 };
} else if (direction === 'right' && dx === 0) {
nextDirection = { x: 1, y: 0 };
}
}
// Keyboard
document.addEventListener('keydown', (e) => {
// Prevent scrolling with arrows
if(["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
e.preventDefault();
}
switch (e.key) {
case 'ArrowUp': changeDirection('up'); break;
case 'ArrowDown': changeDirection('down'); break;
case 'ArrowLeft': changeDirection('left'); break;
case 'ArrowRight': changeDirection('right'); break;
case 'Enter':
if (!isGameRunning && !startScreen.classList.contains('hidden')) initGame();
else if (!isGameRunning && !gameOverScreen.classList.contains('hidden')) initGame();
break;
}
});
// Touch / Swipe
let touchStartX = 0;
let touchStartY = 0;
document.addEventListener('touchstart', (e) => {
touchStartX = e.changedTouches[0].screenX;
touchStartY = e.changedTouches[0].screenY;
}, {passive: false});
document.addEventListener('touchmove', (e) => {
if(["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
e.preventDefault(); // Prevent scroll
}
}, {passive: false});
document.addEventListener('touchend', (e) => {
if (!isGameRunning) return;
const touchEndX = e.changedTouches[0].screenX;
const touchEndY = e.changedTouches[0].screenY;
const dx = touchEndX - touchStartX;
const dy = touchEndY - touchStartY;
if (Math.abs(dx) > Math.abs(dy)) {
// Horizontal
if (Math.abs(dx) > 30) { // Threshold
changeDirection(dx > 0 ? 'right' : 'left');
}
} else {
// Vertical
if (Math.abs(dy) > 30) {
changeDirection(dy > 0 ? 'down' : 'up');
}
}
}, {passive: false});
// On-screen Buttons
mobileBtns.forEach(btn => {
btn.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent double firing or zoom
const dir = btn.getAttribute('data-dir');
changeDirection(dir);
});
// Also support click for testing on desktop
btn.addEventListener('click', (e) => {
const dir = btn.getAttribute('data-dir');
changeDirection(dir);
});
});
// UI Buttons
startBtn.addEventListener('click', initGame);
restartBtn.addEventListener('click', initGame);
// Initial Draw (Background)
ctx.fillStyle = '#050510';
ctx.fillRect(0, 0, canvas.width, canvas.height);
</script>
</body>
</html>