Spaces:
Running

File size: 28,244 Bytes
c411cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tailwind Pacman</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        @keyframes pacman-mouth {
            0% { clip-path: polygon(50% 50%, 100% 0%, 100% 100%); }
            50% { clip-path: polygon(50% 50%, 100% 25%, 100% 75%); }
            100% { clip-path: polygon(50% 50%, 100% 0%, 100% 100%); }
        }
        
        @keyframes ghost-float {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-5px); }
        }
        
        @keyframes scared-ghost {
            0%, 100% { background-color: #2563eb; }
            50% { background-color: #93c5fd; }
        }
        
        .pacman {
            animation: pacman-mouth 0.5s infinite;
            clip-path: polygon(50% 50%, 100% 0%, 100% 100%);
        }
        
        .ghost {
            animation: ghost-float 2s infinite;
        }
        
        .scared {
            animation: scared-ghost 0.5s infinite;
        }
        
        #game-board {
            position: relative;
            background-color: #111827;
            border: 4px solid #f59e0b;
        }
        
        .wall {
            background-color: #1e40af;
        }
        
        .pellet {
            width: 8px;
            height: 8px;
            background-color: #fef3c7;
            border-radius: 50%;
        }
        
        .power-pellet {
            width: 16px;
            height: 16px;
            background-color: #fef3c7;
            border-radius: 50%;
            animation: pulse 1s infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.3); opacity: 0.8; }
        }
    </style>
</head>
<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center justify-center p-4">
    <div class="text-center mb-6">
        <h1 class="text-4xl font-bold text-yellow-300 mb-2">Tailwind Pacman</h1>
        <div class="flex justify-center items-center gap-8 mb-4">
            <div class="flex items-center">
                <div class="w-6 h-6 bg-yellow-300 rounded-full mr-2"></div>
                <span>Score: <span id="score">0</span></span>
            </div>
            <div class="flex items-center">
                <div class="w-6 h-6 bg-red-500 rounded-full mr-2"></div>
                <span>Lives: <span id="lives">3</span></span>
            </div>
        </div>
        <button id="start-btn" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-6 rounded-full transition">
            Start Game
        </button>
    </div>
    
    <div id="game-board" class="grid grid-cols-28 grid-rows-31 gap-0 w-[560px] h-[620px] relative overflow-hidden">
        <!-- Game elements will be generated by JavaScript -->
    </div>
    
    <div class="mt-6 text-center">
        <p class="text-gray-400">Use arrow keys to move. Eat all pellets to win!</p>
        <p class="text-gray-400 mt-2">Eat power pellets to scare ghosts!</p>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // Game constants
            const GRID_SIZE = 28;
            const CELL_SIZE = 20;
            const PACMAN_SPEED = 2;
            const GHOST_SPEED = 1.5;
            const SCARED_DURATION = 10000; // 10 seconds
            
            // Game state
            let score = 0;
            let lives = 3;
            let gameRunning = false;
            let scaredTimer = null;
            let ghostsScared = false;
            let pelletsRemaining = 0;
            
            // DOM elements
            const gameBoard = document.getElementById('game-board');
            const scoreDisplay = document.getElementById('score');
            const livesDisplay = document.getElementById('lives');
            const startBtn = document.getElementById('start-btn');
            
            // Game elements
            let pacman = null;
            let ghosts = [];
            let pellets = [];
            let powerPellets = [];
            let walls = [];
            
            // Movement
            let direction = 'right';
            let nextDirection = 'right';
            let moving = false;
            
            // Maze layout (1 = wall, 0 = path, p = pellet, P = power pellet)
            const mazeLayout = [
                '1111111111111111111111111111',
                '1pppppppppppp1pppppppppppp1',
                '1p1111p11111p1p11111p1111p1',
                '1P1111p11111p1p11111p1111P1',
                '1p1111p11111p1p11111p1111p1',
                '1pppppppppppppppppppppppp1',
                '1p1111p1p111111111p1p1111p1',
                '1p1111p1p111111111p1p1111p1',
                '1ppppppp1pppp1pppp1ppppppp1',
                '1111111p1110p1p0111p1111111',
                '0000011p1110p1p0111p1100000',
                '0000011p100000000011p1100000',
                '0000011p100111111011p1100000',
                '1111111p100100001011p1111111',
                '0000000ppp00p00000ppp0000000',
                '1111111p100111111011p1111111',
                '0000011p100000000011p1100000',
                '0000011p100111111011p1100000',
                '1111111p100p11111011p1111111',
                '1ppppppppppp1ppppppppppppp1',
                '1p1111p11111p1p11111p1111p1',
                '1p1111p11111p1p11111p1111p1',
                '1Ppppp1pppppp0pppppp1ppppP1',
                '111p1p1p1p1111111p1p1p1p1111',
                '111p1p1p1p1111111p1p1p1p1111',
                '1ppppp1pppppp1pppppp1pppppp1',
                '1p1111111111p1p11111111111p1',
                '1p1111111111p1p11111111111p1',
                '1ppppppppppppppppppppppppp1',
                '1111111111111111111111111111'
            ];
            
            // Initialize game
            function initGame() {
                clearGame();
                createMaze();
                createPacman();
                createGhosts();
                updateDisplay();
            }
            
            // Clear game elements
            function clearGame() {
                gameBoard.innerHTML = '';
                ghosts = [];
                pellets = [];
                powerPellets = [];
                walls = [];
                pelletsRemaining = 0;
                direction = 'right';
                nextDirection = 'right';
                moving = false;
                clearTimeout(scaredTimer);
                ghostsScared = false;
            }
            
            // Create maze from layout
            function createMaze() {
                for (let y = 0; y < mazeLayout.length; y++) {
                    for (let x = 0; x < mazeLayout[y].length; x++) {
                        const cell = document.createElement('div');
                        cell.className = 'absolute';
                        cell.style.width = `${CELL_SIZE}px`;
                        cell.style.height = `${CELL_SIZE}px`;
                        cell.style.left = `${x * CELL_SIZE}px`;
                        cell.style.top = `${y * CELL_SIZE}px`;
                        
                        switch (mazeLayout[y][x]) {
                            case '1':
                                cell.classList.add('wall');
                                walls.push({ x, y });
                                break;
                            case 'p':
                                createPellet(x, y);
                                pelletsRemaining++;
                                break;
                            case 'P':
                                createPowerPellet(x, y);
                                pelletsRemaining++;
                                break;
                        }
                        
                        gameBoard.appendChild(cell);
                    }
                }
            }
            
            // Create a pellet
            function createPellet(x, y) {
                const pellet = document.createElement('div');
                pellet.className = 'pellet absolute';
                pellet.style.left = `${x * CELL_SIZE + CELL_SIZE / 2 - 4}px`;
                pellet.style.top = `${y * CELL_SIZE + CELL_SIZE / 2 - 4}px`;
                gameBoard.appendChild(pellet);
                pellets.push({ element: pellet, x, y });
            }
            
            // Create a power pellet
            function createPowerPellet(x, y) {
                const powerPellet = document.createElement('div');
                powerPellet.className = 'power-pellet absolute';
                powerPellet.style.left = `${x * CELL_SIZE + CELL_SIZE / 2 - 8}px`;
                powerPellet.style.top = `${y * CELL_SIZE + CELL_SIZE / 2 - 8}px`;
                gameBoard.appendChild(powerPellet);
                powerPellets.push({ element: powerPellet, x, y });
            }
            
            // Create Pacman
            function createPacman() {
                pacman = document.createElement('div');
                pacman.className = 'pacman bg-yellow-300 absolute rounded-full';
                pacman.style.width = `${CELL_SIZE}px`;
                pacman.style.height = `${CELL_SIZE}px`;
                pacman.style.left = `${14 * CELL_SIZE}px`;
                pacman.style.top = `${23 * CELL_SIZE}px`;
                pacman.style.transform = 'rotate(0deg)';
                gameBoard.appendChild(pacman);
            }
            
            // Create ghosts
            function createGhosts() {
                const ghostColors = ['bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400'];
                const ghostPositions = [
                    { x: 13, y: 11 },
                    { x: 14, y: 11 },
                    { x: 13, y: 14 },
                    { x: 14, y: 14 }
                ];
                
                ghostPositions.forEach((pos, index) => {
                    const ghost = document.createElement('div');
                    ghost.className = `ghost ${ghostColors[index]} absolute rounded-t-full`;
                    ghost.style.width = `${CELL_SIZE}px`;
                    ghost.style.height = `${CELL_SIZE}px`;
                    ghost.style.left = `${pos.x * CELL_SIZE}px`;
                    ghost.style.top = `${pos.y * CELL_SIZE}px`;
                    
                    // Add ghost eyes
                    const eyes = document.createElement('div');
                    eyes.className = 'flex justify-between absolute w-full px-1 top-1';
                    eyes.innerHTML = `
                        <div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
                            <div class="w-1 h-1 bg-black rounded-full"></div>
                        </div>
                        <div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
                            <div class="w-1 h-1 bg-black rounded-full"></div>
                        </div>
                    `;
                    ghost.appendChild(eyes);
                    
                    gameBoard.appendChild(ghost);
                    
                    ghosts.push({
                        element: ghost,
                        x: pos.x,
                        y: pos.y,
                        direction: ['up', 'down', 'left', 'right'][Math.floor(Math.random() * 4)],
                        speed: GHOST_SPEED,
                        isScared: false
                    });
                });
            }
            
            // Update score and lives display
            function updateDisplay() {
                scoreDisplay.textContent = score;
                livesDisplay.textContent = lives;
            }
            
            // Start the game
            function startGame() {
                if (gameRunning) return;
                
                initGame();
                gameRunning = true;
                startBtn.textContent = 'Restart Game';
                moving = true;
                
                // Game loop
                const gameLoop = setInterval(() => {
                    if (!gameRunning) {
                        clearInterval(gameLoop);
                        return;
                    }
                    
                    movePacman();
                    moveGhosts();
                    checkCollisions();
                    
                    // Win condition
                    if (pelletsRemaining === 0) {
                        gameOver(true);
                    }
                }, 16); // ~60fps
            }
            
            // Move Pacman
            function movePacman() {
                if (!moving) return;
                
                let newX = Math.floor(parseInt(pacman.style.left) / CELL_SIZE);
                let newY = Math.floor(parseInt(pacman.style.top) / CELL_SIZE);
                
                // Check if Pacman is centered in a cell to change direction
                const pacmanLeft = parseInt(pacman.style.left);
                const pacmanTop = parseInt(pacman.style.top);
                const centeredX = pacmanLeft % CELL_SIZE === 0;
                const centeredY = pacmanTop % CELL_SIZE === 0;
                
                if (centeredX && centeredY) {
                    // Try to change direction if possible
                    const tempDirection = direction;
                    direction = nextDirection;
                    
                    if (canMove(newX, newY, direction)) {
                        // Direction changed successfully
                    } else if (canMove(newX, newY, tempDirection)) {
                        direction = tempDirection;
                    } else {
                        direction = tempDirection;
                        moving = false;
                        return;
                    }
                    
                    // Update Pacman's rotation
                    switch (direction) {
                        case 'up': pacman.style.transform = 'rotate(-90deg)'; break;
                        case 'down': pacman.style.transform = 'rotate(90deg)'; break;
                        case 'left': pacman.style.transform = 'rotate(180deg)'; break;
                        case 'right': pacman.style.transform = 'rotate(0deg)'; break;
                    }
                }
                
                // Move Pacman
                switch (direction) {
                    case 'up':
                        pacman.style.top = `${parseInt(pacman.style.top) - PACMAN_SPEED}px`;
                        break;
                    case 'down':
                        pacman.style.top = `${parseInt(pacman.style.top) + PACMAN_SPEED}px`;
                        break;
                    case 'left':
                        pacman.style.left = `${parseInt(pacman.style.left) - PACMAN_SPEED}px`;
                        break;
                    case 'right':
                        pacman.style.left = `${parseInt(pacman.style.left) + PACMAN_SPEED}px`;
                        break;
                }
                
                // Handle tunnel/wrap-around
                if (parseInt(pacman.style.left) < -CELL_SIZE) {
                    pacman.style.left = `${GRID_SIZE * CELL_SIZE}px`;
                } else if (parseInt(pacman.style.left) > GRID_SIZE * CELL_SIZE) {
                    pacman.style.left = `${-CELL_SIZE}px`;
                }
                
                // Check for pellet collision
                checkPelletCollision();
            }
            
            // Move ghosts
            function moveGhosts() {
                ghosts.forEach(ghost => {
                    let ghostX = Math.floor(parseInt(ghost.element.style.left) / CELL_SIZE);
                    let ghostY = Math.floor(parseInt(ghost.element.style.top) / CELL_SIZE);
                    
                    // Check if ghost is centered in a cell to change direction
                    const ghostLeft = parseInt(ghost.element.style.left);
                    const ghostTop = parseInt(ghost.element.style.top);
                    const centeredX = ghostLeft % CELL_SIZE === 0;
                    const centeredY = ghostTop % CELL_SIZE === 0;
                    
                    if (centeredX && centeredY) {
                        // Get possible directions (not opposite current direction)
                        const possibleDirections = ['up', 'down', 'left', 'right'].filter(dir => {
                            if (dir === 'up' && ghost.direction === 'down') return false;
                            if (dir === 'down' && ghost.direction === 'up') return false;
                            if (dir === 'left' && ghost.direction === 'right') return false;
                            if (dir === 'right' && ghost.direction === 'left') return false;
                            
                            return canMove(ghostX, ghostY, dir);
                        });
                        
                        // Choose random direction
                        if (possibleDirections.length > 0) {
                            ghost.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)];
                        }
                    }
                    
                    // Move ghost
                    switch (ghost.direction) {
                        case 'up':
                            ghost.element.style.top = `${parseInt(ghost.element.style.top) - ghost.speed}px`;
                            break;
                        case 'down':
                            ghost.element.style.top = `${parseInt(ghost.element.style.top) + ghost.speed}px`;
                            break;
                        case 'left':
                            ghost.element.style.left = `${parseInt(ghost.element.style.left) - ghost.speed}px`;
                            break;
                        case 'right':
                            ghost.element.style.left = `${parseInt(ghost.element.style.left) + ghost.speed}px`;
                            break;
                    }
                    
                    // Handle tunnel/wrap-around
                    if (parseInt(ghost.element.style.left) < -CELL_SIZE) {
                        ghost.element.style.left = `${GRID_SIZE * CELL_SIZE}px`;
                    } else if (parseInt(ghost.element.style.left) > GRID_SIZE * CELL_SIZE) {
                        ghost.element.style.left = `${-CELL_SIZE}px`;
                    }
                });
            }
            
            // Check if movement is possible
            function canMove(x, y, dir) {
                let newX = x;
                let newY = y;
                
                switch (dir) {
                    case 'up': newY--; break;
                    case 'down': newY++; break;
                    case 'left': newX--; break;
                    case 'right': newX++; break;
                }
                
                // Check boundaries
                if (newX < 0 || newX >= GRID_SIZE || newY < 0 || newY >= mazeLayout.length) {
                    return false;
                }
                
                // Check walls
                return mazeLayout[newY][newX] !== '1';
            }
            
            // Check pellet collisions
            function checkPelletCollision() {
                const pacmanX = Math.round(parseInt(pacman.style.left) / CELL_SIZE);
                const pacmanY = Math.round(parseInt(pacman.style.top) / CELL_SIZE);
                
                // Check regular pellets
                pellets.forEach((pellet, index) => {
                    if (pellet.x === pacmanX && pellet.y === pacmanY) {
                        pellet.element.remove();
                        pellets.splice(index, 1);
                        score += 10;
                        pelletsRemaining--;
                        updateDisplay();
                    }
                });
                
                // Check power pellets
                powerPellets.forEach((pellet, index) => {
                    if (pellet.x === pacmanX && pellet.y === pacmanY) {
                        pellet.element.remove();
                        powerPellets.splice(index, 1);
                        score += 50;
                        pelletsRemaining--;
                        scareGhosts();
                        updateDisplay();
                    }
                });
            }
            
            // Scare ghosts (make them vulnerable)
            function scareGhosts() {
                ghostsScared = true;
                ghosts.forEach(ghost => {
                    ghost.isScared = true;
                    ghost.element.classList.remove('bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400');
                    ghost.element.classList.add('scared');
                    ghost.speed = GHOST_SPEED * 0.7; // Slow down when scared
                });
                
                // Reset scared state after duration
                clearTimeout(scaredTimer);
                scaredTimer = setTimeout(() => {
                    ghostsScared = false;
                    ghosts.forEach(ghost => {
                        ghost.isScared = false;
                        ghost.element.classList.remove('scared');
                        ghost.element.classList.add('bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400');
                        ghost.speed = GHOST_SPEED;
                    });
                }, SCARED_DURATION);
            }
            
            // Check collisions with ghosts
            function checkCollisions() {
                const pacmanX = Math.round(parseInt(pacman.style.left) / CELL_SIZE);
                const pacmanY = Math.round(parseInt(pacman.style.top) / CELL_SIZE);
                
                ghosts.forEach((ghost, index) => {
                    const ghostX = Math.round(parseInt(ghost.element.style.left) / CELL_SIZE);
                    const ghostY = Math.round(parseInt(ghost.element.style.top) / CELL_SIZE);
                    
                    if (pacmanX === ghostX && pacmanY === ghostY) {
                        if (ghost.isScared) {
                            // Eat ghost
                            ghost.element.remove();
                            ghosts.splice(index, 1);
                            score += 200;
                            updateDisplay();
                            
                            // Respawn ghost after delay
                            setTimeout(() => {
                                const ghost = document.createElement('div');
                                ghost.className = `ghost bg-red-500 absolute rounded-t-full`;
                                ghost.style.width = `${CELL_SIZE}px`;
                                ghost.style.height = `${CELL_SIZE}px`;
                                ghost.style.left = `${14 * CELL_SIZE}px`;
                                ghost.style.top = `${11 * CELL_SIZE}px`;
                                
                                const eyes = document.createElement('div');
                                eyes.className = 'flex justify-between absolute w-full px-1 top-1';
                                eyes.innerHTML = `
                                    <div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
                                        <div class="w-1 h-1 bg-black rounded-full"></div>
                                    </div>
                                    <div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
                                        <div class="w-1 h-1 bg-black rounded-full"></div>
                                    </div>
                                `;
                                ghost.appendChild(eyes);
                                
                                gameBoard.appendChild(ghost);
                                
                                ghosts.push({
                                    element: ghost,
                                    x: 14,
                                    y: 11,
                                    direction: ['up', 'down', 'left', 'right'][Math.floor(Math.random() * 4)],
                                    speed: GHOST_SPEED,
                                    isScared: false
                                });
                            }, 3000);
                        } else {
                            // Lose life
                            lives--;
                            updateDisplay();
                            
                            if (lives <= 0) {
                                gameOver(false);
                            } else {
                                // Reset positions
                                pacman.style.left = `${14 * CELL_SIZE}px`;
                                pacman.style.top = `${23 * CELL_SIZE}px`;
                                direction = 'right';
                                nextDirection = 'right';
                                pacman.style.transform = 'rotate(0deg)';
                                
                                ghosts.forEach(g => {
                                    g.element.style.left = `${14 * CELL_SIZE}px`;
                                    g.element.style.top = `${11 * CELL_SIZE}px`;
                                });
                                
                                // Pause briefly
                                moving = false;
                                setTimeout(() => {
                                    moving = true;
                                }, 1000);
                            }
                        }
                    }
                });
            }
            
            // Game over
            function gameOver(won) {
                gameRunning = false;
                moving = false;
                
                if (won) {
                    alert(`Congratulations! You won with a score of ${score}!`);
                } else {
                    alert(`Game Over! Your score was ${score}.`);
                }
            }
            
            // Event listeners
            startBtn.addEventListener('click', startGame);
            
            document.addEventListener('keydown', (e) => {
                if (!gameRunning) return;
                
                switch (e.key) {
                    case 'ArrowUp':
                        nextDirection = 'up';
                        break;
                    case 'ArrowDown':
                        nextDirection = 'down';
                        break;
                    case 'ArrowLeft':
                        nextDirection = 'left';
                        break;
                    case 'ArrowRight':
                        nextDirection = 'right';
                        break;
                }
                
                // Start moving if not already
                if (!moving) {
                    direction = nextDirection;
                    moving = true;
                }
            });
        });
    </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=Engelko/pacman" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>