Spaces:
Running
Running
Add 2 files
Browse files- README.md +7 -5
- index.html +467 -19
README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: snake-frenzy
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
tags:
|
| 9 |
+
- deepsite
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
|
@@ -1,19 +1,467 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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>Snake Game - Reach 100!</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<style>
|
| 9 |
+
#game-board {
|
| 10 |
+
background-color: #1a2e05;
|
| 11 |
+
border: 4px solid #3a5f0b;
|
| 12 |
+
box-shadow: 0 0 20px rgba(58, 95, 11, 0.5);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.snake-segment {
|
| 16 |
+
background-color: #8bc34a;
|
| 17 |
+
border: 1px solid #689f38;
|
| 18 |
+
border-radius: 20%;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.food {
|
| 22 |
+
background-color: #ff5252;
|
| 23 |
+
border: 2px solid #ff1744;
|
| 24 |
+
border-radius: 50%;
|
| 25 |
+
animation: pulse 0.5s infinite alternate;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
@keyframes pulse {
|
| 29 |
+
from { transform: scale(0.95); }
|
| 30 |
+
to { transform: scale(1.05); }
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.game-over-overlay, .win-overlay {
|
| 34 |
+
background-color: rgba(0, 0, 0, 0.7);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.difficulty-btn.active {
|
| 38 |
+
background-color: #4CAF50;
|
| 39 |
+
color: white;
|
| 40 |
+
transform: scale(1.05);
|
| 41 |
+
box-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.progress-bar {
|
| 45 |
+
height: 10px;
|
| 46 |
+
background-color: #2d3748;
|
| 47 |
+
border-radius: 5px;
|
| 48 |
+
overflow: hidden;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
.progress-fill {
|
| 52 |
+
height: 100%;
|
| 53 |
+
background: linear-gradient(90deg, #4CAF50, #8BC34A);
|
| 54 |
+
transition: width 0.3s ease;
|
| 55 |
+
}
|
| 56 |
+
</style>
|
| 57 |
+
</head>
|
| 58 |
+
<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center justify-center p-4">
|
| 59 |
+
<div class="text-center mb-6">
|
| 60 |
+
<h1 class="text-4xl font-bold text-green-400 mb-2">Snake Game</h1>
|
| 61 |
+
<p class="text-gray-300">Reach 100 points to win!</p>
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
<div class="flex gap-4 mb-4">
|
| 65 |
+
<button id="easy-btn" class="difficulty-btn active bg-gray-700 hover:bg-gray-600 px-4 py-2 rounded-lg transition">
|
| 66 |
+
Easy
|
| 67 |
+
</button>
|
| 68 |
+
<button id="medium-btn" class="difficulty-btn bg-gray-700 hover:bg-gray-600 px-4 py-2 rounded-lg transition">
|
| 69 |
+
Medium
|
| 70 |
+
</button>
|
| 71 |
+
<button id="hard-btn" class="difficulty-btn bg-gray-700 hover:bg-gray-600 px-4 py-2 rounded-lg transition">
|
| 72 |
+
Hard
|
| 73 |
+
</button>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<div class="relative">
|
| 77 |
+
<div class="flex justify-between items-center mb-4 w-full max-w-md">
|
| 78 |
+
<div class="bg-gray-800 px-4 py-2 rounded-lg">
|
| 79 |
+
<span class="text-gray-300 mr-2">Score:</span>
|
| 80 |
+
<span id="score" class="text-green-400 font-bold text-xl">0</span>
|
| 81 |
+
<span class="text-gray-400">/100</span>
|
| 82 |
+
</div>
|
| 83 |
+
<button id="restart-btn" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg transition">
|
| 84 |
+
Restart Game
|
| 85 |
+
</button>
|
| 86 |
+
</div>
|
| 87 |
+
|
| 88 |
+
<div class="progress-bar w-full max-w-md mb-2">
|
| 89 |
+
<div id="progress-fill" class="progress-fill" style="width: 0%"></div>
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<canvas id="game-board" width="400" height="400" class="rounded-lg"></canvas>
|
| 93 |
+
|
| 94 |
+
<div id="game-over" class="game-over-overlay absolute inset-0 flex flex-col items-center justify-center rounded-lg hidden">
|
| 95 |
+
<div class="bg-gray-800 p-6 rounded-lg text-center">
|
| 96 |
+
<h2 class="text-2xl font-bold text-red-400 mb-4">Game Over!</h2>
|
| 97 |
+
<p class="text-xl mb-4">Your score: <span id="final-score" class="text-green-400">0</span>/100</p>
|
| 98 |
+
<button id="play-again-btn" class="bg-green-600 hover:bg-green-700 text-white px-6 py-2 rounded-lg transition">
|
| 99 |
+
Play Again
|
| 100 |
+
</button>
|
| 101 |
+
</div>
|
| 102 |
+
</div>
|
| 103 |
+
|
| 104 |
+
<div id="win-screen" class="win-overlay absolute inset-0 flex flex-col items-center justify-center rounded-lg hidden">
|
| 105 |
+
<div class="bg-gray-800 p-6 rounded-lg text-center">
|
| 106 |
+
<h2 class="text-2xl font-bold text-green-400 mb-4">You Win! 🎉</h2>
|
| 107 |
+
<p class="text-xl mb-4">You reached 100 points!</p>
|
| 108 |
+
<button id="win-play-again-btn" class="bg-green-600 hover:bg-green-700 text-white px-6 py-2 rounded-lg transition">
|
| 109 |
+
Play Again
|
| 110 |
+
</button>
|
| 111 |
+
</div>
|
| 112 |
+
</div>
|
| 113 |
+
</div>
|
| 114 |
+
|
| 115 |
+
<div class="mt-6 text-gray-400 text-sm text-center max-w-md">
|
| 116 |
+
<p>Controls: Arrow keys or WASD (W=Up, A=Left, S=Down, D=Right)</p>
|
| 117 |
+
<p class="mt-1">Press any control key to start the game</p>
|
| 118 |
+
</div>
|
| 119 |
+
|
| 120 |
+
<script>
|
| 121 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 122 |
+
const canvas = document.getElementById('game-board');
|
| 123 |
+
const ctx = canvas.getContext('2d');
|
| 124 |
+
const scoreDisplay = document.getElementById('score');
|
| 125 |
+
const finalScoreDisplay = document.getElementById('final-score');
|
| 126 |
+
const restartBtn = document.getElementById('restart-btn');
|
| 127 |
+
const playAgainBtn = document.getElementById('play-again-btn');
|
| 128 |
+
const winPlayAgainBtn = document.getElementById('win-play-again-btn');
|
| 129 |
+
const gameOverDisplay = document.getElementById('game-over');
|
| 130 |
+
const winDisplay = document.getElementById('win-screen');
|
| 131 |
+
const progressFill = document.getElementById('progress-fill');
|
| 132 |
+
|
| 133 |
+
// Difficulty buttons
|
| 134 |
+
const easyBtn = document.getElementById('easy-btn');
|
| 135 |
+
const mediumBtn = document.getElementById('medium-btn');
|
| 136 |
+
const hardBtn = document.getElementById('hard-btn');
|
| 137 |
+
|
| 138 |
+
const gridSize = 20;
|
| 139 |
+
const tileCount = canvas.width / gridSize;
|
| 140 |
+
const WIN_SCORE = 100;
|
| 141 |
+
|
| 142 |
+
let snake = [];
|
| 143 |
+
let food = {};
|
| 144 |
+
let score = 0;
|
| 145 |
+
let direction = 'right';
|
| 146 |
+
let nextDirection = 'right';
|
| 147 |
+
let gameSpeed;
|
| 148 |
+
let gameRunning = false;
|
| 149 |
+
let gameLoop;
|
| 150 |
+
let currentDifficulty = 'easy';
|
| 151 |
+
|
| 152 |
+
// Difficulty settings
|
| 153 |
+
const difficultySettings = {
|
| 154 |
+
easy: { speed: 150, foodScore: 1 },
|
| 155 |
+
medium: { speed: 100, foodScore: 2 },
|
| 156 |
+
hard: { speed: 70, foodScore: 3 }
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
// Initialize game
|
| 160 |
+
function initGame() {
|
| 161 |
+
snake = [
|
| 162 |
+
{x: 10, y: 10},
|
| 163 |
+
{x: 9, y: 10},
|
| 164 |
+
{x: 8, y: 10}
|
| 165 |
+
];
|
| 166 |
+
|
| 167 |
+
direction = 'right';
|
| 168 |
+
nextDirection = 'right';
|
| 169 |
+
score = 0;
|
| 170 |
+
scoreDisplay.textContent = score;
|
| 171 |
+
progressFill.style.width = '0%';
|
| 172 |
+
|
| 173 |
+
// Set game speed based on difficulty
|
| 174 |
+
gameSpeed = difficultySettings[currentDifficulty].speed;
|
| 175 |
+
|
| 176 |
+
placeFood();
|
| 177 |
+
gameRunning = true;
|
| 178 |
+
gameOverDisplay.classList.add('hidden');
|
| 179 |
+
winDisplay.classList.add('hidden');
|
| 180 |
+
|
| 181 |
+
if (gameLoop) clearInterval(gameLoop);
|
| 182 |
+
gameLoop = setInterval(gameStep, gameSpeed);
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
// Game step
|
| 186 |
+
function gameStep() {
|
| 187 |
+
if (!gameRunning) return;
|
| 188 |
+
|
| 189 |
+
direction = nextDirection;
|
| 190 |
+
|
| 191 |
+
// Move snake
|
| 192 |
+
const head = {x: snake[0].x, y: snake[0].y};
|
| 193 |
+
|
| 194 |
+
switch (direction) {
|
| 195 |
+
case 'up':
|
| 196 |
+
head.y--;
|
| 197 |
+
break;
|
| 198 |
+
case 'down':
|
| 199 |
+
head.y++;
|
| 200 |
+
break;
|
| 201 |
+
case 'left':
|
| 202 |
+
head.x--;
|
| 203 |
+
break;
|
| 204 |
+
case 'right':
|
| 205 |
+
head.x++;
|
| 206 |
+
break;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
// Check collision with walls
|
| 210 |
+
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
|
| 211 |
+
gameOver();
|
| 212 |
+
return;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// Check collision with self
|
| 216 |
+
for (let i = 0; i < snake.length; i++) {
|
| 217 |
+
if (snake[i].x === head.x && snake[i].y === head.y) {
|
| 218 |
+
gameOver();
|
| 219 |
+
return;
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
// Check if snake ate food
|
| 224 |
+
if (head.x === food.x && head.y === food.y) {
|
| 225 |
+
// Don't remove tail (snake grows)
|
| 226 |
+
placeFood();
|
| 227 |
+
score += difficultySettings[currentDifficulty].foodScore;
|
| 228 |
+
scoreDisplay.textContent = score;
|
| 229 |
+
updateProgress();
|
| 230 |
+
|
| 231 |
+
// Check for win condition
|
| 232 |
+
if (score >= WIN_SCORE) {
|
| 233 |
+
winGame();
|
| 234 |
+
return;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
// Increase speed slightly every 10 points
|
| 238 |
+
if (score % 10 === 0) {
|
| 239 |
+
clearInterval(gameLoop);
|
| 240 |
+
gameSpeed = Math.max(gameSpeed - 5, 50);
|
| 241 |
+
gameLoop = setInterval(gameStep, gameSpeed);
|
| 242 |
+
}
|
| 243 |
+
} else {
|
| 244 |
+
// Remove tail
|
| 245 |
+
snake.pop();
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
// Add new head
|
| 249 |
+
snake.unshift(head);
|
| 250 |
+
|
| 251 |
+
// Draw everything
|
| 252 |
+
draw();
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Update progress bar
|
| 256 |
+
function updateProgress() {
|
| 257 |
+
const progress = (score / WIN_SCORE) * 100;
|
| 258 |
+
progressFill.style.width = `${progress}%`;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
// Draw game
|
| 262 |
+
function draw() {
|
| 263 |
+
// Clear canvas
|
| 264 |
+
ctx.fillStyle = '#1a2e05';
|
| 265 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 266 |
+
|
| 267 |
+
// Draw snake
|
| 268 |
+
snake.forEach((segment, index) => {
|
| 269 |
+
// Head is slightly different color
|
| 270 |
+
const isHead = index === 0;
|
| 271 |
+
ctx.fillStyle = isHead ? '#a5d6a7' : '#8bc34a';
|
| 272 |
+
ctx.strokeStyle = isHead ? '#689f38' : '#5a9216';
|
| 273 |
+
|
| 274 |
+
ctx.beginPath();
|
| 275 |
+
ctx.roundRect(
|
| 276 |
+
segment.x * gridSize,
|
| 277 |
+
segment.y * gridSize,
|
| 278 |
+
gridSize,
|
| 279 |
+
gridSize,
|
| 280 |
+
4
|
| 281 |
+
);
|
| 282 |
+
ctx.fill();
|
| 283 |
+
ctx.stroke();
|
| 284 |
+
});
|
| 285 |
+
|
| 286 |
+
// Draw food
|
| 287 |
+
ctx.fillStyle = '#ff5252';
|
| 288 |
+
ctx.strokeStyle = '#ff1744';
|
| 289 |
+
|
| 290 |
+
ctx.beginPath();
|
| 291 |
+
ctx.arc(
|
| 292 |
+
food.x * gridSize + gridSize/2,
|
| 293 |
+
food.y * gridSize + gridSize/2,
|
| 294 |
+
gridSize/2 - 2,
|
| 295 |
+
0,
|
| 296 |
+
Math.PI * 2
|
| 297 |
+
);
|
| 298 |
+
ctx.fill();
|
| 299 |
+
ctx.stroke();
|
| 300 |
+
|
| 301 |
+
// Draw grid (optional)
|
| 302 |
+
ctx.strokeStyle = 'rgba(58, 95, 11, 0.2)';
|
| 303 |
+
ctx.lineWidth = 0.5;
|
| 304 |
+
|
| 305 |
+
for (let i = 0; i < tileCount; i++) {
|
| 306 |
+
// Vertical lines
|
| 307 |
+
ctx.beginPath();
|
| 308 |
+
ctx.moveTo(i * gridSize, 0);
|
| 309 |
+
ctx.lineTo(i * gridSize, canvas.height);
|
| 310 |
+
ctx.stroke();
|
| 311 |
+
|
| 312 |
+
// Horizontal lines
|
| 313 |
+
ctx.beginPath();
|
| 314 |
+
ctx.moveTo(0, i * gridSize);
|
| 315 |
+
ctx.lineTo(canvas.width, i * gridSize);
|
| 316 |
+
ctx.stroke();
|
| 317 |
+
}
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
// Place food randomly
|
| 321 |
+
function placeFood() {
|
| 322 |
+
let validPosition = false;
|
| 323 |
+
|
| 324 |
+
while (!validPosition) {
|
| 325 |
+
food = {
|
| 326 |
+
x: Math.floor(Math.random() * tileCount),
|
| 327 |
+
y: Math.floor(Math.random() * tileCount)
|
| 328 |
+
};
|
| 329 |
+
|
| 330 |
+
// Check if food is on snake
|
| 331 |
+
validPosition = true;
|
| 332 |
+
for (let i = 0; i < snake.length; i++) {
|
| 333 |
+
if (snake[i].x === food.x && snake[i].y === food.y) {
|
| 334 |
+
validPosition = false;
|
| 335 |
+
break;
|
| 336 |
+
}
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
// Game over
|
| 342 |
+
function gameOver() {
|
| 343 |
+
gameRunning = false;
|
| 344 |
+
clearInterval(gameLoop);
|
| 345 |
+
finalScoreDisplay.textContent = score;
|
| 346 |
+
gameOverDisplay.classList.remove('hidden');
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// Win game
|
| 350 |
+
function winGame() {
|
| 351 |
+
gameRunning = false;
|
| 352 |
+
clearInterval(gameLoop);
|
| 353 |
+
winDisplay.classList.remove('hidden');
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// Event listeners
|
| 357 |
+
document.addEventListener('keydown', (e) => {
|
| 358 |
+
// Prevent default for arrow keys to avoid page scrolling
|
| 359 |
+
const controlKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'w', 'a', 's', 'd'];
|
| 360 |
+
if (controlKeys.includes(e.key)) {
|
| 361 |
+
e.preventDefault();
|
| 362 |
+
|
| 363 |
+
// Start game on first key press if not running
|
| 364 |
+
if (!gameRunning && snake.length === 0) {
|
| 365 |
+
initGame();
|
| 366 |
+
return;
|
| 367 |
+
}
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
// Change direction (can't go opposite current direction)
|
| 371 |
+
switch (e.key.toLowerCase()) {
|
| 372 |
+
case 'arrowup':
|
| 373 |
+
case 'w':
|
| 374 |
+
if (direction !== 'down') nextDirection = 'up';
|
| 375 |
+
break;
|
| 376 |
+
case 'arrowdown':
|
| 377 |
+
case 's':
|
| 378 |
+
if (direction !== 'up') nextDirection = 'down';
|
| 379 |
+
break;
|
| 380 |
+
case 'arrowleft':
|
| 381 |
+
case 'a':
|
| 382 |
+
if (direction !== 'right') nextDirection = 'left';
|
| 383 |
+
break;
|
| 384 |
+
case 'arrowright':
|
| 385 |
+
case 'd':
|
| 386 |
+
if (direction !== 'left') nextDirection = 'right';
|
| 387 |
+
break;
|
| 388 |
+
}
|
| 389 |
+
});
|
| 390 |
+
|
| 391 |
+
// Difficulty buttons
|
| 392 |
+
function setDifficulty(difficulty) {
|
| 393 |
+
currentDifficulty = difficulty;
|
| 394 |
+
|
| 395 |
+
// Update active button styling
|
| 396 |
+
easyBtn.classList.remove('active');
|
| 397 |
+
mediumBtn.classList.remove('active');
|
| 398 |
+
hardBtn.classList.remove('active');
|
| 399 |
+
|
| 400 |
+
if (difficulty === 'easy') easyBtn.classList.add('active');
|
| 401 |
+
if (difficulty === 'medium') mediumBtn.classList.add('active');
|
| 402 |
+
if (difficulty === 'hard') hardBtn.classList.add('active');
|
| 403 |
+
|
| 404 |
+
// Restart game if already running
|
| 405 |
+
if (gameRunning) {
|
| 406 |
+
initGame();
|
| 407 |
+
}
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
easyBtn.addEventListener('click', () => setDifficulty('easy'));
|
| 411 |
+
mediumBtn.addEventListener('click', () => setDifficulty('medium'));
|
| 412 |
+
hardBtn.addEventListener('click', () => setDifficulty('hard'));
|
| 413 |
+
|
| 414 |
+
restartBtn.addEventListener('click', initGame);
|
| 415 |
+
playAgainBtn.addEventListener('click', initGame);
|
| 416 |
+
winPlayAgainBtn.addEventListener('click', initGame);
|
| 417 |
+
|
| 418 |
+
// Touch controls for mobile
|
| 419 |
+
let touchStartX = 0;
|
| 420 |
+
let touchStartY = 0;
|
| 421 |
+
|
| 422 |
+
canvas.addEventListener('touchstart', (e) => {
|
| 423 |
+
if (!gameRunning && snake.length === 0) {
|
| 424 |
+
initGame();
|
| 425 |
+
return;
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
touchStartX = e.touches[0].clientX;
|
| 429 |
+
touchStartY = e.touches[0].clientY;
|
| 430 |
+
e.preventDefault();
|
| 431 |
+
}, false);
|
| 432 |
+
|
| 433 |
+
canvas.addEventListener('touchmove', (e) => {
|
| 434 |
+
if (!gameRunning) return;
|
| 435 |
+
|
| 436 |
+
const touchEndX = e.touches[0].clientX;
|
| 437 |
+
const touchEndY = e.touches[0].clientY;
|
| 438 |
+
|
| 439 |
+
const dx = touchEndX - touchStartX;
|
| 440 |
+
const dy = touchEndY - touchStartY;
|
| 441 |
+
|
| 442 |
+
// Determine primary direction of swipe
|
| 443 |
+
if (Math.abs(dx) > Math.abs(dy)) {
|
| 444 |
+
// Horizontal swipe
|
| 445 |
+
if (dx > 0 && direction !== 'left') {
|
| 446 |
+
nextDirection = 'right';
|
| 447 |
+
} else if (dx < 0 && direction !== 'right') {
|
| 448 |
+
nextDirection = 'left';
|
| 449 |
+
}
|
| 450 |
+
} else {
|
| 451 |
+
// Vertical swipe
|
| 452 |
+
if (dy > 0 && direction !== 'up') {
|
| 453 |
+
nextDirection = 'down';
|
| 454 |
+
} else if (dy < 0 && direction !== 'down') {
|
| 455 |
+
nextDirection = 'up';
|
| 456 |
+
}
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
e.preventDefault();
|
| 460 |
+
}, false);
|
| 461 |
+
|
| 462 |
+
// Initialize with easy difficulty
|
| 463 |
+
setDifficulty('easy');
|
| 464 |
+
});
|
| 465 |
+
</script>
|
| 466 |
+
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=andreaschandra/snake-frenzy" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
|
| 467 |
+
</html>
|