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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Snake Game - Direction Pad Control</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .game-container {
            position: relative;
            background-color: #1a202c;
            border-radius: 0.5rem;
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        
        .game-grid {
            display: grid;
            grid-template-columns: repeat(var(--grid-size), 1fr);
            grid-template-rows: repeat(var(--grid-size), 1fr);
            gap: 1px;
            background-color: #2d3748;
        }
        
        .grid-cell {
            aspect-ratio: 1/1;
            background-color: #1a202c;
            position: relative;
        }
        
        .snake {
            background-color: #48bb78;
            border-radius: 20%;
            transition: all 0.1s ease;
            z-index: 2;
            position: relative;
        }
        
        .snake-head {
            background-color: #38a169;
            border-radius: 30%;
        }
        
        .food {
            background-color: #f56565;
            border-radius: 50%;
            animation: pulse 1.5s infinite;
            z-index: 2;
            position: relative;
        }
        
        @keyframes pulse {
            0% { transform: scale(0.95); }
            50% { transform: scale(1.1); }
            100% { transform: scale(0.95); }
        }
        
        .game-overlay {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background-color: rgba(26, 32, 44, 0.9);
            border-radius: 0.5rem;
            z-index: 10;
        }
        
        .dpad-container {
            position: relative;
            width: 150px;
            height: 150px;
            margin: 20px auto;
        }
        
        .dpad-outer {
            position: absolute;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background: rgba(72, 187, 120, 0.1);
            border: 2px solid rgba(72, 187, 120, 0.3);
        }
        
        .dpad-center {
            position: absolute;
            width: 50px;
            height: 50px;
            background: rgba(72, 187, 120, 0.2);
            border-radius: 50%;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        
        .dpad-direction {
            position: absolute;
            width: 40px;
            height: 40px;
            background: rgba(72, 187, 120, 0.3);
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 16px;
            transition: all 0.1s ease;
            cursor: pointer;
        }
        
        .dpad-direction:active {
            transform: scale(1.3);
            background: rgba(72, 187, 120, 0.6);
        }
        
        .dpad-up {
            top: 10px;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .dpad-down {
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .dpad-left {
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .dpad-right {
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .mobile-controls {
            display: none;
        }
        
        @media (max-width: 640px) {
            .mobile-controls {
                display: block;
            }
            
            .desktop-instructions {
                display: none;
            }
        }
    </style>
</head>
<body class="bg-gray-900 min-h-screen flex flex-col items-center justify-center p-4">
    <div class="max-w-md w-full">
        <h1 class="text-3xl font-bold text-center text-green-400 mb-2">🐍 方向键控制贪吃蛇</h1>
        
        <div class="flex justify-between items-center mb-4">
            <div class="text-white font-semibold">
                分数: <span id="score" class="text-green-400">0</span>
            </div>
            <div class="text-white font-semibold">
                最高分: <span id="high-score" class="text-yellow-400">0</span>
            </div>
        </div>
        
        <div class="game-container">
            <div id="game-board" class="game-grid w-full" style="--grid-size: 20;"></div>
            
            <div id="game-overlay" class="game-overlay">
                <h2 class="text-3xl font-bold text-white mb-4" id="overlay-title">方向键控制贪吃蛇</h2>
                <p class="text-gray-300 mb-6 text-center px-4" id="overlay-message">
                    使用键盘方向键或下方控制盘控制蛇的移动方向
                </p>
                <button id="start-btn" class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-6 rounded-full transition">
                    开始游戏
                </button>
            </div>
        </div>
        
        <!-- Direction pad for mobile/touch devices -->
        <div class="mobile-controls">
            <div class="dpad-container">
                <div class="dpad-outer"></div>
                <div class="dpad-center"></div>
                <div class="dpad-direction dpad-up" id="btn-touch-up">
                    <i class="fas fa-arrow-up"></i>
                </div>
                <div class="dpad-direction dpad-down" id="btn-touch-down">
                    <i class="fas fa-arrow-down"></i>
                </div>
                <div class="dpad-direction dpad-left" id="btn-touch-left">
                    <i class="fas fa-arrow-left"></i>
                </div>
                <div class="dpad-direction dpad-right" id="btn-touch-right">
                    <i class="fas fa-arrow-right"></i>
                </div>
            </div>
        </div>
        
        <div class="mt-4 text-center text-gray-400 text-sm desktop-instructions">
            <p>使用键盘方向键控制方向 | 按空格键暂停</p>
        </div>
    </div>

    <script>
        // Game configuration
        const config = {
            gridSize: 20,
            initialSpeed: 150,
            speedIncrease: 5,
            maxSpeed: 80,
            minSpeed: 40
        };
        
        // Game state
        let snake = [];
        let food = {};
        let direction = {x: 1, y: 0}; // Initial direction: right
        let nextDirection = {x: 1, y: 0};
        let score = 0;
        let highScore = localStorage.getItem('snakeHighScore') || 0;
        let gameInterval;
        let isPaused = false;
        let gameStarted = false;
        let gameSpeed = config.initialSpeed;
        
        // DOM elements
        const gameBoard = document.getElementById('game-board');
        const gameOverlay = document.getElementById('game-overlay');
        const overlayTitle = document.getElementById('overlay-title');
        const overlayMessage = document.getElementById('overlay-message');
        const startBtn = document.getElementById('start-btn');
        const scoreElement = document.getElementById('score');
        const highScoreElement = document.getElementById('high-score');
        
        // Touch direction buttons
        const btnTouchUp = document.getElementById('btn-touch-up');
        const btnTouchDown = document.getElementById('btn-touch-down');
        const btnTouchLeft = document.getElementById('btn-touch-left');
        const btnTouchRight = document.getElementById('btn-touch-right');
        
        // Initialize the game
        function initGame() {
            // Set high score
            highScoreElement.textContent = highScore;
            
            // Create grid cells
            gameBoard.innerHTML = '';
            gameBoard.style.setProperty('--grid-size', config.gridSize);
            
            for (let i = 0; i < config.gridSize * config.gridSize; i++) {
                const cell = document.createElement('div');
                cell.classList.add('grid-cell');
                cell.dataset.index = i;
                
                // Get position from index
                const x = i % config.gridSize;
                const y = Math.floor(i / config.gridSize);
                cell.dataset.x = x;
                cell.dataset.y = y;
                
                gameBoard.appendChild(cell);
            }
            
            // Event listeners
            startBtn.addEventListener('click', startGame);
            document.addEventListener('keydown', handleKeyDown);
            
            // Touch direction event listeners
            btnTouchUp.addEventListener('mousedown', () => changeDirection(0, -1));
            btnTouchUp.addEventListener('touchstart', (e) => {
                e.preventDefault();
                changeDirection(0, -1);
            });
            
            btnTouchDown.addEventListener('mousedown', () => changeDirection(0, 1));
            btnTouchDown.addEventListener('touchstart', (e) => {
                e.preventDefault();
                changeDirection(0, 1);
            });
            
            btnTouchLeft.addEventListener('mousedown', () => changeDirection(-1, 0));
            btnTouchLeft.addEventListener('touchstart', (e) => {
                e.preventDefault();
                changeDirection(-1, 0);
            });
            
            btnTouchRight.addEventListener('mousedown', () => changeDirection(1, 0));
            btnTouchRight.addEventListener('touchstart', (e) => {
                e.preventDefault();
                changeDirection(1, 0);
            });
            
            // Show start screen
            showOverlay('方向键控制贪吃蛇', '使用键盘方向键或下方控制盘控制蛇的移动方向', '开始游戏');
        }
        
        // Change direction
        function changeDirection(x, y) {
            // Prevent 180-degree turn (snake can't reverse direction)
            if ((x !== -direction.x || y !== -direction.y) && 
                (x !== 0 || y !== 0)) { // Ignore if both are 0
                nextDirection = {x, y};
            }
        }
        
        // Start the game
        function startGame() {
            // Reset game state
            snake = [
                {x: 5, y: 10},
                {x: 4, y: 10},
                {x: 3, y: 10}
            ];
            direction = {x: 1, y: 0}; // Initial direction: right
            nextDirection = {x: 1, y: 0};
            score = 0;
            gameSpeed = config.initialSpeed;
            scoreElement.textContent = score;
            
            // Generate first food
            generateFood();
            
            // Hide overlay
            hideOverlay();
            
            // Start game loop
            if (gameInterval) clearInterval(gameInterval);
            gameInterval = setInterval(gameLoop, gameSpeed);
            gameStarted = true;
            isPaused = false;
        }
        
        // Game loop
        function gameLoop() {
            if (isPaused) return;
            
            // Update direction at the start of each move
            direction = {...nextDirection};
            
            // Move snake
            const head = {...snake[0]};
            head.x += direction.x;
            head.y += direction.y;
            
            // Check for collisions
            if (
                head.x < 0 || head.x >= config.gridSize ||
                head.y < 0 || head.y >= config.gridSize ||
                snake.some((segment, index) => index > 0 && segment.x === head.x && segment.y === head.y)
            ) {
                gameOver();
                return;
            }
            
            // Add new head
            snake.unshift(head);
            
            // Check for food
            if (head.x === food.x && head.y === food.y) {
                // Increase score
                score += 10;
                scoreElement.textContent = score;
                
                // Increase speed (up to a max)
                if (gameSpeed > config.maxSpeed) {
                    gameSpeed -= config.speedIncrease;
                    clearInterval(gameInterval);
                    gameInterval = setInterval(gameLoop, gameSpeed);
                }
                
                // Generate new food
                generateFood();
            } else {
                // Remove tail if no food eaten
                snake.pop();
            }
            
            // Render the game
            renderGame();
        }
        
        // Render the game
        function renderGame() {
            // Clear all cells
            document.querySelectorAll('.grid-cell').forEach(cell => {
                cell.classList.remove('snake', 'snake-head', 'food');
            });
            
            // Render snake
            snake.forEach((segment, index) => {
                const cellIndex = segment.y * config.gridSize + segment.x;
                const cell = document.querySelector(`.grid-cell[data-index="${cellIndex}"]`);
                
                if (cell) {
                    cell.classList.add(index === 0 ? 'snake-head' : 'snake');
                }
            });
            
            // Render food
            const foodIndex = food.y * config.gridSize + food.x;
            const foodCell = document.querySelector(`.grid-cell[data-index="${foodIndex}"]`);
            if (foodCell) {
                foodCell.classList.add('food');
            }
        }
        
        // Generate food at random position
        function generateFood() {
            let newFood;
            do {
                newFood = {
                    x: Math.floor(Math.random() * config.gridSize),
                    y: Math.floor(Math.random() * config.gridSize)
                };
            } while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
            
            food = newFood;
        }
        
        // Game over
        function gameOver() {
            clearInterval(gameInterval);
            gameStarted = false;
            
            // Update high score if needed
            if (score > highScore) {
                highScore = score;
                localStorage.setItem('snakeHighScore', highScore);
                highScoreElement.textContent = highScore;
            }
            
            showOverlay('游戏结束', `你的分数: ${score}`, '再玩一次');
        }
        
        // Handle keyboard input
        function handleKeyDown(e) {
            // Only process if game is started or starting the game
            if (!gameStarted && e.key === 'Enter') {
                startGame();
                return;
            }
            
            if (!gameStarted) return;
            
            switch (e.key) {
                case ' ':
                    togglePause();
                    break;
                case 'ArrowUp':
                    changeDirection(0, -1);
                    break;
                case 'ArrowDown':
                    changeDirection(0, 1);
                    break;
                case 'ArrowLeft':
                    changeDirection(-1, 0);
                    break;
                case 'ArrowRight':
                    changeDirection(1, 0);
                    break;
            }
        }
        
        // Toggle pause
        function togglePause() {
            if (!gameStarted) return;
            
            isPaused = !isPaused;
            
            if (isPaused) {
                showOverlay('游戏暂停', '按空格键继续', '继续');
            } else {
                hideOverlay();
            }
        }
        
        // Show overlay
        function showOverlay(title, message, buttonText) {
            overlayTitle.textContent = title;
            overlayMessage.textContent = message;
            startBtn.textContent = buttonText;
            gameOverlay.style.display = 'flex';
        }
        
        // Hide overlay
        function hideOverlay() {
            gameOverlay.style.display = 'none';
        }
        
        // Initialize the game when the page loads
        window.addEventListener('DOMContentLoaded', initGame);
    </script>
<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>
</html>