Spaces:
Running
Running
Add 2 files
Browse files- README.md +6 -4
- index.html +466 -19
README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title: 2048
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: blue
|
| 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: 2048-by-deepsite
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: blue
|
| 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,466 @@
|
|
| 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>2048 Game</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
| 9 |
+
<style>
|
| 10 |
+
@keyframes tile-appear {
|
| 11 |
+
0% {
|
| 12 |
+
transform: scale(0.5);
|
| 13 |
+
opacity: 0;
|
| 14 |
+
}
|
| 15 |
+
100% {
|
| 16 |
+
transform: scale(1);
|
| 17 |
+
opacity: 1;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
@keyframes tile-merge {
|
| 22 |
+
0% {
|
| 23 |
+
transform: scale(1);
|
| 24 |
+
}
|
| 25 |
+
50% {
|
| 26 |
+
transform: scale(1.2);
|
| 27 |
+
}
|
| 28 |
+
100% {
|
| 29 |
+
transform: scale(1);
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.tile-new {
|
| 34 |
+
animation: tile-appear 0.2s ease-out;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.tile-merged {
|
| 38 |
+
animation: tile-merge 0.2s ease-out;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
.game-container {
|
| 42 |
+
perspective: 1000px;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.board {
|
| 46 |
+
transform-style: preserve-3d;
|
| 47 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.tile {
|
| 51 |
+
transition: all 0.1s ease-in-out;
|
| 52 |
+
font-weight: 700;
|
| 53 |
+
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
|
| 54 |
+
}
|
| 55 |
+
</style>
|
| 56 |
+
</head>
|
| 57 |
+
<body class="bg-gray-100 min-h-screen flex flex-col items-center justify-center font-sans">
|
| 58 |
+
<div class="game-container w-full max-w-md px-4">
|
| 59 |
+
<div class="flex justify-between items-center mb-6">
|
| 60 |
+
<h1 class="text-4xl font-bold text-gray-800">2048</h1>
|
| 61 |
+
<div class="flex items-center space-x-4">
|
| 62 |
+
<div class="bg-gray-800 text-white px-4 py-2 rounded-lg text-center">
|
| 63 |
+
<div class="text-xs text-gray-300">SCORE</div>
|
| 64 |
+
<div id="score" class="text-xl font-bold">0</div>
|
| 65 |
+
</div>
|
| 66 |
+
<button id="restart" class="bg-amber-500 hover:bg-amber-600 text-white px-4 py-2 rounded-lg transition">
|
| 67 |
+
<i class="fas fa-redo mr-1"></i> New Game
|
| 68 |
+
</button>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
|
| 72 |
+
<div class="mb-4 flex justify-between items-center">
|
| 73 |
+
<p class="text-gray-600">Join the numbers and get to the <span class="font-bold">2048 tile!</span></p>
|
| 74 |
+
<div id="best-score" class="bg-gray-800 text-white px-3 py-1 rounded text-sm">
|
| 75 |
+
Best: 0
|
| 76 |
+
</div>
|
| 77 |
+
</div>
|
| 78 |
+
|
| 79 |
+
<div class="board bg-gray-300 p-3 rounded-xl relative mb-6">
|
| 80 |
+
<div class="grid grid-cols-4 gap-3">
|
| 81 |
+
<!-- Empty cells for the board background -->
|
| 82 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 83 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 84 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 85 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 86 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 87 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 88 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 89 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 90 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 91 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 92 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 93 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 94 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 95 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 96 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 97 |
+
<div class="bg-gray-200 rounded-lg aspect-square"></div>
|
| 98 |
+
</div>
|
| 99 |
+
|
| 100 |
+
<!-- Tiles will be dynamically added here -->
|
| 101 |
+
<div id="tiles-container" class="absolute inset-0 grid grid-cols-4 gap-3 p-3"></div>
|
| 102 |
+
</div>
|
| 103 |
+
|
| 104 |
+
<div class="text-center text-gray-500 text-sm">
|
| 105 |
+
<p>Use arrow keys or swipe to move the tiles. When two tiles with the same number touch, they merge into one!</p>
|
| 106 |
+
</div>
|
| 107 |
+
</div>
|
| 108 |
+
|
| 109 |
+
<!-- Game Over Modal -->
|
| 110 |
+
<div id="game-over-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
|
| 111 |
+
<div class="bg-white rounded-xl p-6 max-w-sm w-full mx-4 text-center">
|
| 112 |
+
<h2 class="text-2xl font-bold text-gray-800 mb-2">Game Over!</h2>
|
| 113 |
+
<p class="text-gray-600 mb-4">Your score: <span id="final-score" class="font-bold">0</span></p>
|
| 114 |
+
<div class="flex space-x-3 justify-center">
|
| 115 |
+
<button id="try-again" class="bg-amber-500 hover:bg-amber-600 text-white px-4 py-2 rounded-lg transition">
|
| 116 |
+
Try Again
|
| 117 |
+
</button>
|
| 118 |
+
<button id="close-modal" class="bg-gray-200 hover:bg-gray-300 text-gray-800 px-4 py-2 rounded-lg transition">
|
| 119 |
+
Close
|
| 120 |
+
</button>
|
| 121 |
+
</div>
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
|
| 125 |
+
<!-- Win Modal -->
|
| 126 |
+
<div id="win-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
|
| 127 |
+
<div class="bg-white rounded-xl p-6 max-w-sm w-full mx-4 text-center">
|
| 128 |
+
<h2 class="text-2xl font-bold text-gray-800 mb-2">You Win! 🎉</h2>
|
| 129 |
+
<p class="text-gray-600 mb-4">You reached 2048 with a score of <span id="win-score" class="font-bold">0</span></p>
|
| 130 |
+
<div class="flex space-x-3 justify-center">
|
| 131 |
+
<button id="continue-playing" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg transition">
|
| 132 |
+
Keep Going
|
| 133 |
+
</button>
|
| 134 |
+
<button id="new-game-win" class="bg-amber-500 hover:bg-amber-600 text-white px-4 py-2 rounded-lg transition">
|
| 135 |
+
New Game
|
| 136 |
+
</button>
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
</div>
|
| 140 |
+
|
| 141 |
+
<script>
|
| 142 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 143 |
+
// Game state
|
| 144 |
+
let board = Array(16).fill(0);
|
| 145 |
+
let score = 0;
|
| 146 |
+
let bestScore = localStorage.getItem('2048-best-score') || 0;
|
| 147 |
+
let gameOver = false;
|
| 148 |
+
let won = false;
|
| 149 |
+
let touchStartX = 0;
|
| 150 |
+
let touchStartY = 0;
|
| 151 |
+
|
| 152 |
+
// DOM elements
|
| 153 |
+
const tilesContainer = document.getElementById('tiles-container');
|
| 154 |
+
const scoreElement = document.getElementById('score');
|
| 155 |
+
const bestScoreElement = document.getElementById('best-score');
|
| 156 |
+
const restartButton = document.getElementById('restart');
|
| 157 |
+
const gameOverModal = document.getElementById('game-over-modal');
|
| 158 |
+
const winModal = document.getElementById('win-modal');
|
| 159 |
+
const finalScoreElement = document.getElementById('final-score');
|
| 160 |
+
const winScoreElement = document.getElementById('win-score');
|
| 161 |
+
const tryAgainButton = document.getElementById('try-again');
|
| 162 |
+
const closeModalButton = document.getElementById('close-modal');
|
| 163 |
+
const continuePlayingButton = document.getElementById('continue-playing');
|
| 164 |
+
const newGameWinButton = document.getElementById('new-game-win');
|
| 165 |
+
|
| 166 |
+
// Tile colors based on value
|
| 167 |
+
const tileColors = {
|
| 168 |
+
0: 'bg-gray-200',
|
| 169 |
+
2: 'bg-amber-50 text-gray-800',
|
| 170 |
+
4: 'bg-amber-100 text-gray-800',
|
| 171 |
+
8: 'bg-amber-300 text-white',
|
| 172 |
+
16: 'bg-amber-400 text-white',
|
| 173 |
+
32: 'bg-orange-400 text-white',
|
| 174 |
+
64: 'bg-orange-500 text-white',
|
| 175 |
+
128: 'bg-yellow-300 text-white',
|
| 176 |
+
256: 'bg-yellow-400 text-white',
|
| 177 |
+
512: 'bg-yellow-500 text-white',
|
| 178 |
+
1024: 'bg-yellow-600 text-white',
|
| 179 |
+
2048: 'bg-yellow-700 text-white',
|
| 180 |
+
4096: 'bg-red-500 text-white',
|
| 181 |
+
8192: 'bg-red-600 text-white'
|
| 182 |
+
};
|
| 183 |
+
|
| 184 |
+
// Initialize the game
|
| 185 |
+
function initGame() {
|
| 186 |
+
board = Array(16).fill(0);
|
| 187 |
+
score = 0;
|
| 188 |
+
gameOver = false;
|
| 189 |
+
won = false;
|
| 190 |
+
updateScore();
|
| 191 |
+
addRandomTile();
|
| 192 |
+
addRandomTile();
|
| 193 |
+
renderBoard();
|
| 194 |
+
|
| 195 |
+
// Hide modals if they're open
|
| 196 |
+
gameOverModal.classList.add('hidden');
|
| 197 |
+
winModal.classList.add('hidden');
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
// Add a random tile (2 or 4) to an empty cell
|
| 201 |
+
function addRandomTile() {
|
| 202 |
+
const emptyCells = board.reduce((acc, val, index) => {
|
| 203 |
+
if (val === 0) acc.push(index);
|
| 204 |
+
return acc;
|
| 205 |
+
}, []);
|
| 206 |
+
|
| 207 |
+
if (emptyCells.length > 0) {
|
| 208 |
+
const randomIndex = emptyCells[Math.floor(Math.random() * emptyCells.length)];
|
| 209 |
+
board[randomIndex] = Math.random() < 0.9 ? 2 : 4;
|
| 210 |
+
return true;
|
| 211 |
+
}
|
| 212 |
+
return false;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// Render the board
|
| 216 |
+
function renderBoard() {
|
| 217 |
+
tilesContainer.innerHTML = '';
|
| 218 |
+
|
| 219 |
+
board.forEach((value, index) => {
|
| 220 |
+
if (value !== 0) {
|
| 221 |
+
const tile = document.createElement('div');
|
| 222 |
+
tile.className = `tile flex items-center justify-center rounded-lg aspect-square ${tileColors[value] || 'bg-red-700 text-white'}`;
|
| 223 |
+
tile.textContent = value;
|
| 224 |
+
|
| 225 |
+
// Calculate position based on index
|
| 226 |
+
const row = Math.floor(index / 4);
|
| 227 |
+
const col = index % 4;
|
| 228 |
+
|
| 229 |
+
tile.style.gridRow = row + 1;
|
| 230 |
+
tile.style.gridColumn = col + 1;
|
| 231 |
+
|
| 232 |
+
// Add animation class for new tiles (handled in game logic)
|
| 233 |
+
tilesContainer.appendChild(tile);
|
| 234 |
+
}
|
| 235 |
+
});
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
// Update the score display
|
| 239 |
+
function updateScore() {
|
| 240 |
+
scoreElement.textContent = score;
|
| 241 |
+
bestScoreElement.textContent = `Best: ${bestScore}`;
|
| 242 |
+
|
| 243 |
+
// Update best score if current score is higher
|
| 244 |
+
if (score > bestScore) {
|
| 245 |
+
bestScore = score;
|
| 246 |
+
localStorage.setItem('2048-best-score', bestScore);
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
// Check if the game is over
|
| 251 |
+
function checkGameOver() {
|
| 252 |
+
// If there are empty cells, game is not over
|
| 253 |
+
if (board.some(cell => cell === 0)) return false;
|
| 254 |
+
|
| 255 |
+
// Check for possible merges in rows
|
| 256 |
+
for (let row = 0; row < 4; row++) {
|
| 257 |
+
for (let col = 0; col < 3; col++) {
|
| 258 |
+
const index = row * 4 + col;
|
| 259 |
+
if (board[index] === board[index + 1]) {
|
| 260 |
+
return false;
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
// Check for possible merges in columns
|
| 266 |
+
for (let col = 0; col < 4; col++) {
|
| 267 |
+
for (let row = 0; row < 3; row++) {
|
| 268 |
+
const index = row * 4 + col;
|
| 269 |
+
if (board[index] === board[index + 4]) {
|
| 270 |
+
return false;
|
| 271 |
+
}
|
| 272 |
+
}
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
return true;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// Check if the player has won (reached 2048)
|
| 279 |
+
function checkWin() {
|
| 280 |
+
return board.some(cell => cell === 2048);
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
// Move tiles in a direction
|
| 284 |
+
function move(direction) {
|
| 285 |
+
if (gameOver) return false;
|
| 286 |
+
|
| 287 |
+
let moved = false;
|
| 288 |
+
const newBoard = [...board];
|
| 289 |
+
|
| 290 |
+
// Helper function to process a line (row or column)
|
| 291 |
+
const processLine = (line) => {
|
| 292 |
+
// Remove zeros
|
| 293 |
+
let filtered = line.filter(val => val !== 0);
|
| 294 |
+
let result = [];
|
| 295 |
+
let merged = false;
|
| 296 |
+
|
| 297 |
+
// Merge adjacent equal values
|
| 298 |
+
for (let i = 0; i < filtered.length; i++) {
|
| 299 |
+
if (i < filtered.length - 1 && filtered[i] === filtered[i + 1] && !merged) {
|
| 300 |
+
result.push(filtered[i] * 2);
|
| 301 |
+
score += filtered[i] * 2;
|
| 302 |
+
merged = true;
|
| 303 |
+
i++; // Skip next element
|
| 304 |
+
} else {
|
| 305 |
+
result.push(filtered[i]);
|
| 306 |
+
merged = false;
|
| 307 |
+
}
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
// Pad with zeros
|
| 311 |
+
while (result.length < 4) {
|
| 312 |
+
result.push(0);
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
// Check if the line changed
|
| 316 |
+
if (line.some((val, i) => val !== result[i])) {
|
| 317 |
+
moved = true;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
return result;
|
| 321 |
+
};
|
| 322 |
+
|
| 323 |
+
// Process based on direction
|
| 324 |
+
if (direction === 'left') {
|
| 325 |
+
for (let row = 0; row < 4; row++) {
|
| 326 |
+
const line = newBoard.slice(row * 4, row * 4 + 4);
|
| 327 |
+
const processed = processLine(line);
|
| 328 |
+
for (let col = 0; col < 4; col++) {
|
| 329 |
+
newBoard[row * 4 + col] = processed[col];
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
} else if (direction === 'right') {
|
| 333 |
+
for (let row = 0; row < 4; row++) {
|
| 334 |
+
const line = newBoard.slice(row * 4, row * 4 + 4).reverse();
|
| 335 |
+
const processed = processLine(line).reverse();
|
| 336 |
+
for (let col = 0; col < 4; col++) {
|
| 337 |
+
newBoard[row * 4 + col] = processed[col];
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
} else if (direction === 'up') {
|
| 341 |
+
for (let col = 0; col < 4; col++) {
|
| 342 |
+
const line = [
|
| 343 |
+
newBoard[col],
|
| 344 |
+
newBoard[col + 4],
|
| 345 |
+
newBoard[col + 8],
|
| 346 |
+
newBoard[col + 12]
|
| 347 |
+
];
|
| 348 |
+
const processed = processLine(line);
|
| 349 |
+
for (let row = 0; row < 4; row++) {
|
| 350 |
+
newBoard[row * 4 + col] = processed[row];
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
} else if (direction === 'down') {
|
| 354 |
+
for (let col = 0; col < 4; col++) {
|
| 355 |
+
const line = [
|
| 356 |
+
newBoard[col + 12],
|
| 357 |
+
newBoard[col + 8],
|
| 358 |
+
newBoard[col + 4],
|
| 359 |
+
newBoard[col]
|
| 360 |
+
];
|
| 361 |
+
const processed = processLine(line).reverse();
|
| 362 |
+
for (let row = 0; row < 4; row++) {
|
| 363 |
+
newBoard[row * 4 + col] = processed[row];
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
// If any movement occurred, update the board
|
| 369 |
+
if (moved) {
|
| 370 |
+
board = newBoard;
|
| 371 |
+
updateScore();
|
| 372 |
+
addRandomTile();
|
| 373 |
+
renderBoard();
|
| 374 |
+
|
| 375 |
+
// Check for win condition (only once)
|
| 376 |
+
if (!won && checkWin()) {
|
| 377 |
+
won = true;
|
| 378 |
+
winScoreElement.textContent = score;
|
| 379 |
+
winModal.classList.remove('hidden');
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
// Check for game over
|
| 383 |
+
if (checkGameOver()) {
|
| 384 |
+
gameOver = true;
|
| 385 |
+
finalScoreElement.textContent = score;
|
| 386 |
+
gameOverModal.classList.remove('hidden');
|
| 387 |
+
}
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
return moved;
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
// Handle keyboard events
|
| 394 |
+
function handleKeyDown(e) {
|
| 395 |
+
if (e.key === 'ArrowUp') {
|
| 396 |
+
move('up');
|
| 397 |
+
} else if (e.key === 'ArrowDown') {
|
| 398 |
+
move('down');
|
| 399 |
+
} else if (e.key === 'ArrowLeft') {
|
| 400 |
+
move('left');
|
| 401 |
+
} else if (e.key === 'ArrowRight') {
|
| 402 |
+
move('right');
|
| 403 |
+
}
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
// Handle touch events for mobile
|
| 407 |
+
function handleTouchStart(e) {
|
| 408 |
+
touchStartX = e.touches[0].clientX;
|
| 409 |
+
touchStartY = e.touches[0].clientY;
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
function handleTouchEnd(e) {
|
| 413 |
+
if (!touchStartX || !touchStartY) return;
|
| 414 |
+
|
| 415 |
+
const touchEndX = e.changedTouches[0].clientX;
|
| 416 |
+
const touchEndY = e.changedTouches[0].clientY;
|
| 417 |
+
|
| 418 |
+
const dx = touchEndX - touchStartX;
|
| 419 |
+
const dy = touchEndY - touchStartY;
|
| 420 |
+
|
| 421 |
+
// Determine the direction based on the greatest change
|
| 422 |
+
if (Math.abs(dx) > Math.abs(dy)) {
|
| 423 |
+
if (dx > 0) {
|
| 424 |
+
move('right');
|
| 425 |
+
} else {
|
| 426 |
+
move('left');
|
| 427 |
+
}
|
| 428 |
+
} else {
|
| 429 |
+
if (dy > 0) {
|
| 430 |
+
move('down');
|
| 431 |
+
} else {
|
| 432 |
+
move('up');
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
// Reset touch coordinates
|
| 437 |
+
touchStartX = 0;
|
| 438 |
+
touchStartY = 0;
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
// Event listeners
|
| 442 |
+
document.addEventListener('keydown', handleKeyDown);
|
| 443 |
+
restartButton.addEventListener('click', initGame);
|
| 444 |
+
tryAgainButton.addEventListener('click', initGame);
|
| 445 |
+
closeModalButton.addEventListener('click', () => gameOverModal.classList.add('hidden'));
|
| 446 |
+
continuePlayingButton.addEventListener('click', () => winModal.classList.add('hidden'));
|
| 447 |
+
newGameWinButton.addEventListener('click', initGame);
|
| 448 |
+
|
| 449 |
+
// Touch events for mobile
|
| 450 |
+
document.addEventListener('touchstart', handleTouchStart, false);
|
| 451 |
+
document.addEventListener('touchend', handleTouchEnd, false);
|
| 452 |
+
|
| 453 |
+
// Prevent scrolling on touch move
|
| 454 |
+
document.addEventListener('touchmove', (e) => {
|
| 455 |
+
if (Math.abs(e.touches[0].clientX - touchStartX) > 10 ||
|
| 456 |
+
Math.abs(e.touches[0].clientY - touchStartY) > 10) {
|
| 457 |
+
e.preventDefault();
|
| 458 |
+
}
|
| 459 |
+
}, { passive: false });
|
| 460 |
+
|
| 461 |
+
// Start the game
|
| 462 |
+
initGame();
|
| 463 |
+
});
|
| 464 |
+
</script>
|
| 465 |
+
<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=thinkall/2048-by-deepsite" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
|
| 466 |
+
</html>
|