Spaces:
Running
Running
Add 2 files
Browse files- README.md +7 -5
- index.html +501 -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: snake2
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: gray
|
| 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,501 @@
|
|
| 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 - Direction Pad Control</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 9 |
+
<style>
|
| 10 |
+
.game-container {
|
| 11 |
+
position: relative;
|
| 12 |
+
background-color: #1a202c;
|
| 13 |
+
border-radius: 0.5rem;
|
| 14 |
+
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
.game-grid {
|
| 18 |
+
display: grid;
|
| 19 |
+
grid-template-columns: repeat(var(--grid-size), 1fr);
|
| 20 |
+
grid-template-rows: repeat(var(--grid-size), 1fr);
|
| 21 |
+
gap: 1px;
|
| 22 |
+
background-color: #2d3748;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.grid-cell {
|
| 26 |
+
aspect-ratio: 1/1;
|
| 27 |
+
background-color: #1a202c;
|
| 28 |
+
position: relative;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.snake {
|
| 32 |
+
background-color: #48bb78;
|
| 33 |
+
border-radius: 20%;
|
| 34 |
+
transition: all 0.1s ease;
|
| 35 |
+
z-index: 2;
|
| 36 |
+
position: relative;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
.snake-head {
|
| 40 |
+
background-color: #38a169;
|
| 41 |
+
border-radius: 30%;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.food {
|
| 45 |
+
background-color: #f56565;
|
| 46 |
+
border-radius: 50%;
|
| 47 |
+
animation: pulse 1.5s infinite;
|
| 48 |
+
z-index: 2;
|
| 49 |
+
position: relative;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
@keyframes pulse {
|
| 53 |
+
0% { transform: scale(0.95); }
|
| 54 |
+
50% { transform: scale(1.1); }
|
| 55 |
+
100% { transform: scale(0.95); }
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.game-overlay {
|
| 59 |
+
position: absolute;
|
| 60 |
+
top: 0;
|
| 61 |
+
left: 0;
|
| 62 |
+
right: 0;
|
| 63 |
+
bottom: 0;
|
| 64 |
+
display: flex;
|
| 65 |
+
flex-direction: column;
|
| 66 |
+
justify-content: center;
|
| 67 |
+
align-items: center;
|
| 68 |
+
background-color: rgba(26, 32, 44, 0.9);
|
| 69 |
+
border-radius: 0.5rem;
|
| 70 |
+
z-index: 10;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.dpad-container {
|
| 74 |
+
position: relative;
|
| 75 |
+
width: 150px;
|
| 76 |
+
height: 150px;
|
| 77 |
+
margin: 20px auto;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.dpad-outer {
|
| 81 |
+
position: absolute;
|
| 82 |
+
width: 100%;
|
| 83 |
+
height: 100%;
|
| 84 |
+
border-radius: 50%;
|
| 85 |
+
background: rgba(72, 187, 120, 0.1);
|
| 86 |
+
border: 2px solid rgba(72, 187, 120, 0.3);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.dpad-center {
|
| 90 |
+
position: absolute;
|
| 91 |
+
width: 50px;
|
| 92 |
+
height: 50px;
|
| 93 |
+
background: rgba(72, 187, 120, 0.2);
|
| 94 |
+
border-radius: 50%;
|
| 95 |
+
top: 50%;
|
| 96 |
+
left: 50%;
|
| 97 |
+
transform: translate(-50%, -50%);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.dpad-direction {
|
| 101 |
+
position: absolute;
|
| 102 |
+
width: 40px;
|
| 103 |
+
height: 40px;
|
| 104 |
+
background: rgba(72, 187, 120, 0.3);
|
| 105 |
+
border-radius: 50%;
|
| 106 |
+
display: flex;
|
| 107 |
+
justify-content: center;
|
| 108 |
+
align-items: center;
|
| 109 |
+
color: white;
|
| 110 |
+
font-size: 16px;
|
| 111 |
+
transition: all 0.1s ease;
|
| 112 |
+
cursor: pointer;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
.dpad-direction:active {
|
| 116 |
+
transform: scale(1.3);
|
| 117 |
+
background: rgba(72, 187, 120, 0.6);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.dpad-up {
|
| 121 |
+
top: 10px;
|
| 122 |
+
left: 50%;
|
| 123 |
+
transform: translateX(-50%);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
.dpad-down {
|
| 127 |
+
bottom: 10px;
|
| 128 |
+
left: 50%;
|
| 129 |
+
transform: translateX(-50%);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.dpad-left {
|
| 133 |
+
left: 10px;
|
| 134 |
+
top: 50%;
|
| 135 |
+
transform: translateY(-50%);
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
.dpad-right {
|
| 139 |
+
right: 10px;
|
| 140 |
+
top: 50%;
|
| 141 |
+
transform: translateY(-50%);
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.mobile-controls {
|
| 145 |
+
display: none;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
@media (max-width: 640px) {
|
| 149 |
+
.mobile-controls {
|
| 150 |
+
display: block;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.desktop-instructions {
|
| 154 |
+
display: none;
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
</style>
|
| 158 |
+
</head>
|
| 159 |
+
<body class="bg-gray-900 min-h-screen flex flex-col items-center justify-center p-4">
|
| 160 |
+
<div class="max-w-md w-full">
|
| 161 |
+
<h1 class="text-3xl font-bold text-center text-green-400 mb-2">🐍 方向键控制贪吃蛇</h1>
|
| 162 |
+
|
| 163 |
+
<div class="flex justify-between items-center mb-4">
|
| 164 |
+
<div class="text-white font-semibold">
|
| 165 |
+
分数: <span id="score" class="text-green-400">0</span>
|
| 166 |
+
</div>
|
| 167 |
+
<div class="text-white font-semibold">
|
| 168 |
+
最高分: <span id="high-score" class="text-yellow-400">0</span>
|
| 169 |
+
</div>
|
| 170 |
+
</div>
|
| 171 |
+
|
| 172 |
+
<div class="game-container">
|
| 173 |
+
<div id="game-board" class="game-grid w-full" style="--grid-size: 20;"></div>
|
| 174 |
+
|
| 175 |
+
<div id="game-overlay" class="game-overlay">
|
| 176 |
+
<h2 class="text-3xl font-bold text-white mb-4" id="overlay-title">方向键控制贪吃蛇</h2>
|
| 177 |
+
<p class="text-gray-300 mb-6 text-center px-4" id="overlay-message">
|
| 178 |
+
使用键盘方向键或下方控制盘控制蛇的移动方向
|
| 179 |
+
</p>
|
| 180 |
+
<button id="start-btn" class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-6 rounded-full transition">
|
| 181 |
+
开始游戏
|
| 182 |
+
</button>
|
| 183 |
+
</div>
|
| 184 |
+
</div>
|
| 185 |
+
|
| 186 |
+
<!-- Direction pad for mobile/touch devices -->
|
| 187 |
+
<div class="mobile-controls">
|
| 188 |
+
<div class="dpad-container">
|
| 189 |
+
<div class="dpad-outer"></div>
|
| 190 |
+
<div class="dpad-center"></div>
|
| 191 |
+
<div class="dpad-direction dpad-up" id="btn-touch-up">
|
| 192 |
+
<i class="fas fa-arrow-up"></i>
|
| 193 |
+
</div>
|
| 194 |
+
<div class="dpad-direction dpad-down" id="btn-touch-down">
|
| 195 |
+
<i class="fas fa-arrow-down"></i>
|
| 196 |
+
</div>
|
| 197 |
+
<div class="dpad-direction dpad-left" id="btn-touch-left">
|
| 198 |
+
<i class="fas fa-arrow-left"></i>
|
| 199 |
+
</div>
|
| 200 |
+
<div class="dpad-direction dpad-right" id="btn-touch-right">
|
| 201 |
+
<i class="fas fa-arrow-right"></i>
|
| 202 |
+
</div>
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
|
| 206 |
+
<div class="mt-4 text-center text-gray-400 text-sm desktop-instructions">
|
| 207 |
+
<p>使用键盘方向键控制方向 | 按空格键暂停</p>
|
| 208 |
+
</div>
|
| 209 |
+
</div>
|
| 210 |
+
|
| 211 |
+
<script>
|
| 212 |
+
// Game configuration
|
| 213 |
+
const config = {
|
| 214 |
+
gridSize: 20,
|
| 215 |
+
initialSpeed: 150,
|
| 216 |
+
speedIncrease: 5,
|
| 217 |
+
maxSpeed: 80,
|
| 218 |
+
minSpeed: 40
|
| 219 |
+
};
|
| 220 |
+
|
| 221 |
+
// Game state
|
| 222 |
+
let snake = [];
|
| 223 |
+
let food = {};
|
| 224 |
+
let direction = {x: 1, y: 0}; // Initial direction: right
|
| 225 |
+
let nextDirection = {x: 1, y: 0};
|
| 226 |
+
let score = 0;
|
| 227 |
+
let highScore = localStorage.getItem('snakeHighScore') || 0;
|
| 228 |
+
let gameInterval;
|
| 229 |
+
let isPaused = false;
|
| 230 |
+
let gameStarted = false;
|
| 231 |
+
let gameSpeed = config.initialSpeed;
|
| 232 |
+
|
| 233 |
+
// DOM elements
|
| 234 |
+
const gameBoard = document.getElementById('game-board');
|
| 235 |
+
const gameOverlay = document.getElementById('game-overlay');
|
| 236 |
+
const overlayTitle = document.getElementById('overlay-title');
|
| 237 |
+
const overlayMessage = document.getElementById('overlay-message');
|
| 238 |
+
const startBtn = document.getElementById('start-btn');
|
| 239 |
+
const scoreElement = document.getElementById('score');
|
| 240 |
+
const highScoreElement = document.getElementById('high-score');
|
| 241 |
+
|
| 242 |
+
// Touch direction buttons
|
| 243 |
+
const btnTouchUp = document.getElementById('btn-touch-up');
|
| 244 |
+
const btnTouchDown = document.getElementById('btn-touch-down');
|
| 245 |
+
const btnTouchLeft = document.getElementById('btn-touch-left');
|
| 246 |
+
const btnTouchRight = document.getElementById('btn-touch-right');
|
| 247 |
+
|
| 248 |
+
// Initialize the game
|
| 249 |
+
function initGame() {
|
| 250 |
+
// Set high score
|
| 251 |
+
highScoreElement.textContent = highScore;
|
| 252 |
+
|
| 253 |
+
// Create grid cells
|
| 254 |
+
gameBoard.innerHTML = '';
|
| 255 |
+
gameBoard.style.setProperty('--grid-size', config.gridSize);
|
| 256 |
+
|
| 257 |
+
for (let i = 0; i < config.gridSize * config.gridSize; i++) {
|
| 258 |
+
const cell = document.createElement('div');
|
| 259 |
+
cell.classList.add('grid-cell');
|
| 260 |
+
cell.dataset.index = i;
|
| 261 |
+
|
| 262 |
+
// Get position from index
|
| 263 |
+
const x = i % config.gridSize;
|
| 264 |
+
const y = Math.floor(i / config.gridSize);
|
| 265 |
+
cell.dataset.x = x;
|
| 266 |
+
cell.dataset.y = y;
|
| 267 |
+
|
| 268 |
+
gameBoard.appendChild(cell);
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// Event listeners
|
| 272 |
+
startBtn.addEventListener('click', startGame);
|
| 273 |
+
document.addEventListener('keydown', handleKeyDown);
|
| 274 |
+
|
| 275 |
+
// Touch direction event listeners
|
| 276 |
+
btnTouchUp.addEventListener('mousedown', () => changeDirection(0, -1));
|
| 277 |
+
btnTouchUp.addEventListener('touchstart', (e) => {
|
| 278 |
+
e.preventDefault();
|
| 279 |
+
changeDirection(0, -1);
|
| 280 |
+
});
|
| 281 |
+
|
| 282 |
+
btnTouchDown.addEventListener('mousedown', () => changeDirection(0, 1));
|
| 283 |
+
btnTouchDown.addEventListener('touchstart', (e) => {
|
| 284 |
+
e.preventDefault();
|
| 285 |
+
changeDirection(0, 1);
|
| 286 |
+
});
|
| 287 |
+
|
| 288 |
+
btnTouchLeft.addEventListener('mousedown', () => changeDirection(-1, 0));
|
| 289 |
+
btnTouchLeft.addEventListener('touchstart', (e) => {
|
| 290 |
+
e.preventDefault();
|
| 291 |
+
changeDirection(-1, 0);
|
| 292 |
+
});
|
| 293 |
+
|
| 294 |
+
btnTouchRight.addEventListener('mousedown', () => changeDirection(1, 0));
|
| 295 |
+
btnTouchRight.addEventListener('touchstart', (e) => {
|
| 296 |
+
e.preventDefault();
|
| 297 |
+
changeDirection(1, 0);
|
| 298 |
+
});
|
| 299 |
+
|
| 300 |
+
// Show start screen
|
| 301 |
+
showOverlay('方向键控制贪吃蛇', '使用键盘方向键或下方控制盘控制蛇的移动方向', '开始游戏');
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// Change direction
|
| 305 |
+
function changeDirection(x, y) {
|
| 306 |
+
// Prevent 180-degree turn (snake can't reverse direction)
|
| 307 |
+
if ((x !== -direction.x || y !== -direction.y) &&
|
| 308 |
+
(x !== 0 || y !== 0)) { // Ignore if both are 0
|
| 309 |
+
nextDirection = {x, y};
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
// Start the game
|
| 314 |
+
function startGame() {
|
| 315 |
+
// Reset game state
|
| 316 |
+
snake = [
|
| 317 |
+
{x: 5, y: 10},
|
| 318 |
+
{x: 4, y: 10},
|
| 319 |
+
{x: 3, y: 10}
|
| 320 |
+
];
|
| 321 |
+
direction = {x: 1, y: 0}; // Initial direction: right
|
| 322 |
+
nextDirection = {x: 1, y: 0};
|
| 323 |
+
score = 0;
|
| 324 |
+
gameSpeed = config.initialSpeed;
|
| 325 |
+
scoreElement.textContent = score;
|
| 326 |
+
|
| 327 |
+
// Generate first food
|
| 328 |
+
generateFood();
|
| 329 |
+
|
| 330 |
+
// Hide overlay
|
| 331 |
+
hideOverlay();
|
| 332 |
+
|
| 333 |
+
// Start game loop
|
| 334 |
+
if (gameInterval) clearInterval(gameInterval);
|
| 335 |
+
gameInterval = setInterval(gameLoop, gameSpeed);
|
| 336 |
+
gameStarted = true;
|
| 337 |
+
isPaused = false;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
// Game loop
|
| 341 |
+
function gameLoop() {
|
| 342 |
+
if (isPaused) return;
|
| 343 |
+
|
| 344 |
+
// Update direction at the start of each move
|
| 345 |
+
direction = {...nextDirection};
|
| 346 |
+
|
| 347 |
+
// Move snake
|
| 348 |
+
const head = {...snake[0]};
|
| 349 |
+
head.x += direction.x;
|
| 350 |
+
head.y += direction.y;
|
| 351 |
+
|
| 352 |
+
// Check for collisions
|
| 353 |
+
if (
|
| 354 |
+
head.x < 0 || head.x >= config.gridSize ||
|
| 355 |
+
head.y < 0 || head.y >= config.gridSize ||
|
| 356 |
+
snake.some((segment, index) => index > 0 && segment.x === head.x && segment.y === head.y)
|
| 357 |
+
) {
|
| 358 |
+
gameOver();
|
| 359 |
+
return;
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
// Add new head
|
| 363 |
+
snake.unshift(head);
|
| 364 |
+
|
| 365 |
+
// Check for food
|
| 366 |
+
if (head.x === food.x && head.y === food.y) {
|
| 367 |
+
// Increase score
|
| 368 |
+
score += 10;
|
| 369 |
+
scoreElement.textContent = score;
|
| 370 |
+
|
| 371 |
+
// Increase speed (up to a max)
|
| 372 |
+
if (gameSpeed > config.maxSpeed) {
|
| 373 |
+
gameSpeed -= config.speedIncrease;
|
| 374 |
+
clearInterval(gameInterval);
|
| 375 |
+
gameInterval = setInterval(gameLoop, gameSpeed);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
// Generate new food
|
| 379 |
+
generateFood();
|
| 380 |
+
} else {
|
| 381 |
+
// Remove tail if no food eaten
|
| 382 |
+
snake.pop();
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// Render the game
|
| 386 |
+
renderGame();
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
// Render the game
|
| 390 |
+
function renderGame() {
|
| 391 |
+
// Clear all cells
|
| 392 |
+
document.querySelectorAll('.grid-cell').forEach(cell => {
|
| 393 |
+
cell.classList.remove('snake', 'snake-head', 'food');
|
| 394 |
+
});
|
| 395 |
+
|
| 396 |
+
// Render snake
|
| 397 |
+
snake.forEach((segment, index) => {
|
| 398 |
+
const cellIndex = segment.y * config.gridSize + segment.x;
|
| 399 |
+
const cell = document.querySelector(`.grid-cell[data-index="${cellIndex}"]`);
|
| 400 |
+
|
| 401 |
+
if (cell) {
|
| 402 |
+
cell.classList.add(index === 0 ? 'snake-head' : 'snake');
|
| 403 |
+
}
|
| 404 |
+
});
|
| 405 |
+
|
| 406 |
+
// Render food
|
| 407 |
+
const foodIndex = food.y * config.gridSize + food.x;
|
| 408 |
+
const foodCell = document.querySelector(`.grid-cell[data-index="${foodIndex}"]`);
|
| 409 |
+
if (foodCell) {
|
| 410 |
+
foodCell.classList.add('food');
|
| 411 |
+
}
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// Generate food at random position
|
| 415 |
+
function generateFood() {
|
| 416 |
+
let newFood;
|
| 417 |
+
do {
|
| 418 |
+
newFood = {
|
| 419 |
+
x: Math.floor(Math.random() * config.gridSize),
|
| 420 |
+
y: Math.floor(Math.random() * config.gridSize)
|
| 421 |
+
};
|
| 422 |
+
} while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
|
| 423 |
+
|
| 424 |
+
food = newFood;
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
// Game over
|
| 428 |
+
function gameOver() {
|
| 429 |
+
clearInterval(gameInterval);
|
| 430 |
+
gameStarted = false;
|
| 431 |
+
|
| 432 |
+
// Update high score if needed
|
| 433 |
+
if (score > highScore) {
|
| 434 |
+
highScore = score;
|
| 435 |
+
localStorage.setItem('snakeHighScore', highScore);
|
| 436 |
+
highScoreElement.textContent = highScore;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
showOverlay('游戏结束', `你的分数: ${score}`, '再玩一次');
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
// Handle keyboard input
|
| 443 |
+
function handleKeyDown(e) {
|
| 444 |
+
// Only process if game is started or starting the game
|
| 445 |
+
if (!gameStarted && e.key === 'Enter') {
|
| 446 |
+
startGame();
|
| 447 |
+
return;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
if (!gameStarted) return;
|
| 451 |
+
|
| 452 |
+
switch (e.key) {
|
| 453 |
+
case ' ':
|
| 454 |
+
togglePause();
|
| 455 |
+
break;
|
| 456 |
+
case 'ArrowUp':
|
| 457 |
+
changeDirection(0, -1);
|
| 458 |
+
break;
|
| 459 |
+
case 'ArrowDown':
|
| 460 |
+
changeDirection(0, 1);
|
| 461 |
+
break;
|
| 462 |
+
case 'ArrowLeft':
|
| 463 |
+
changeDirection(-1, 0);
|
| 464 |
+
break;
|
| 465 |
+
case 'ArrowRight':
|
| 466 |
+
changeDirection(1, 0);
|
| 467 |
+
break;
|
| 468 |
+
}
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
// Toggle pause
|
| 472 |
+
function togglePause() {
|
| 473 |
+
if (!gameStarted) return;
|
| 474 |
+
|
| 475 |
+
isPaused = !isPaused;
|
| 476 |
+
|
| 477 |
+
if (isPaused) {
|
| 478 |
+
showOverlay('游戏暂停', '按空格键继续', '继续');
|
| 479 |
+
} else {
|
| 480 |
+
hideOverlay();
|
| 481 |
+
}
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
// Show overlay
|
| 485 |
+
function showOverlay(title, message, buttonText) {
|
| 486 |
+
overlayTitle.textContent = title;
|
| 487 |
+
overlayMessage.textContent = message;
|
| 488 |
+
startBtn.textContent = buttonText;
|
| 489 |
+
gameOverlay.style.display = 'flex';
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
// Hide overlay
|
| 493 |
+
function hideOverlay() {
|
| 494 |
+
gameOverlay.style.display = 'none';
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
// Initialize the game when the page loads
|
| 498 |
+
window.addEventListener('DOMContentLoaded', initGame);
|
| 499 |
+
</script>
|
| 500 |
+
<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=DavidHouse2024/snake2" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
|
| 501 |
+
</html>
|