File size: 22,001 Bytes
66e0807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dino Runner Game</title>
  <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      min-height: 100vh;
      display: flex;
      flex-direction: column;
      align-items: center;
      background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
      font-family: 'Press Start 2P', cursive;
      overflow: hidden;
    }

    header {
      width: 100%;
      padding: 1rem;
      text-align: center;
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(10px);
      border-bottom: 2px solid rgba(255, 255, 255, 0.2);
    }

    header a {
      color: #00d4ff;
      text-decoration: none;
      font-size: 0.7rem;
      transition: all 0.3s ease;
    }

    header a:hover {
      color: #fff;
      text-shadow: 0 0 10px #00d4ff;
    }

    .game-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      flex: 1;
      padding: 2rem;
      width: 100%;
      max-width: 900px;
    }

    h1 {
      color: #fff;
      font-size: clamp(1rem, 4vw, 2rem);
      margin-bottom: 1rem;
      text-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
      text-align: center;
    }

    .score-board {
      display: flex;
      gap: 2rem;
      margin-bottom: 1rem;
      flex-wrap: wrap;
      justify-content: center;
    }

    .score-item {
      color: #fff;
      font-size: clamp(0.6rem, 2vw, 0.9rem);
      padding: 0.5rem 1rem;
      background: rgba(255, 255, 255, 0.1);
      border-radius: 10px;
      border: 1px solid rgba(255, 255, 255, 0.2);
    }

    .score-item span {
      color: #00d4ff;
    }

    #gameCanvas {
      border: 4px solid #00d4ff;
      border-radius: 15px;
      box-shadow: 0 0 30px rgba(0, 212, 255, 0.3),
        inset 0 0 50px rgba(0, 0, 0, 0.5);
      background: linear-gradient(180deg, #87CEEB 0%, #E0F6FF 60%, #90EE90 60%, #228B22 100%);
      max-width: 100%;
      cursor: pointer;
    }

    .instructions {
      color: rgba(255, 255, 255, 0.7);
      font-size: clamp(0.5rem, 1.5vw, 0.7rem);
      margin-top: 1rem;
      text-align: center;
      line-height: 2;
    }

    .instructions kbd {
      background: rgba(255, 255, 255, 0.2);
      padding: 0.3rem 0.6rem;
      border-radius: 5px;
      border: 1px solid rgba(255, 255, 255, 0.3);
    }

    .game-over-overlay {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.8);
      display: none;
      justify-content: center;
      align-items: center;
      z-index: 100;
    }

    .game-over-content {
      text-align: center;
      color: #fff;
      padding: 2rem;
      background: linear-gradient(135deg, #1a1a2e, #16213e);
      border-radius: 20px;
      border: 3px solid #00d4ff;
      box-shadow: 0 0 50px rgba(0, 212, 255, 0.5);
    }

    .game-over-content h2 {
      font-size: clamp(1.5rem, 5vw, 2.5rem);
      margin-bottom: 1rem;
      color: #ff4757;
    }

    .game-over-content p {
      font-size: clamp(0.7rem, 2vw, 1rem);
      margin-bottom: 1.5rem;
    }

    .restart-btn {
      font-family: 'Press Start 2P', cursive;
      font-size: clamp(0.6rem, 2vw, 0.9rem);
      padding: 1rem 2rem;
      background: linear-gradient(135deg, #00d4ff, #0099cc);
      color: #fff;
      border: none;
      border-radius: 10px;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    .restart-btn:hover {
      transform: scale(1.05);
      box-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
    }

    .start-screen {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.9);
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 100;
    }

    .start-content {
      text-align: center;
      color: #fff;
      padding: 2rem;
    }

    .start-content h2 {
      font-size: clamp(1.5rem, 5vw, 3rem);
      margin-bottom: 2rem;
      color: #00d4ff;
      text-shadow: 0 0 30px rgba(0, 212, 255, 0.7);
    }

    .dino-art {
      font-size: clamp(3rem, 10vw, 6rem);
      margin-bottom: 2rem;
      animation: bounce 1s infinite;
    }

    @keyframes bounce {

      0%,
      100% {
        transform: translateY(0);
      }

      50% {
        transform: translateY(-20px);
      }
    }

    .start-btn {
      font-family: 'Press Start 2P', cursive;
      font-size: clamp(0.8rem, 2.5vw, 1.2rem);
      padding: 1.5rem 3rem;
      background: linear-gradient(135deg, #00d4ff, #0099cc);
      color: #fff;
      border: none;
      border-radius: 15px;
      cursor: pointer;
      transition: all 0.3s ease;
      animation: pulse 2s infinite;
    }

    @keyframes pulse {

      0%,
      100% {
        box-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
      }

      50% {
        box-shadow: 0 0 40px rgba(0, 212, 255, 0.8);
      }
    }

    .start-btn:hover {
      transform: scale(1.1);
    }

    @media (max-width: 600px) {
      .game-container {
        padding: 1rem;
      }

      .score-board {
        gap: 1rem;
      }
    }
  </style>
</head>

<body>
  <header>
    <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">Built with anycoder</a>
  </header>

  <div class="start-screen" id="startScreen">
    <div class="start-content">
      <h2>🦖 DINO RUNNER 🦖</h2>
      <div class="dino-art">🦕</div>
      <button class="start-btn" id="startBtn">START GAME</button>
    </div>
  </div>

  <div class="game-container">
    <h1>🦖 DINO RUNNER 🦖</h1>

    <div class="score-board">
      <div class="score-item">SCORE: <span id="score">0</span></div>
      <div class="score-item">HIGH SCORE: <span id="highScore">0</span></div>
    </div>

    <canvas id="gameCanvas" width="800" height="300"></canvas>

    <div class="instructions">
      Press <kbd>SPACE</kbd> or <kbd></kbd> to jump | <kbd></kbd> to duck | <kbd>TAP</kbd> on mobile
    </div>
  </div>

  <div class="game-over-overlay" id="gameOverOverlay">
    <div class="game-over-content">
      <h2>GAME OVER!</h2>
      <p>Your Score: <span id="finalScore">0</span></p>
      <button class="restart-btn" id="restartBtn">PLAY AGAIN</button>
    </div>
  </div>

  <script>
    const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const scoreElement = document.getElementById('score');
        const highScoreElement = document.getElementById('highScore');
        const finalScoreElement = document.getElementById('finalScore');
        const gameOverOverlay = document.getElementById('gameOverOverlay');
        const startScreen = document.getElementById('startScreen');
        const startBtn = document.getElementById('startBtn');
        const restartBtn = document.getElementById('restartBtn');

        // Responsive canvas
        function resizeCanvas() {
            const maxWidth = Math.min(800, window.innerWidth - 40);
            const ratio = 300 / 800;
            canvas.style.width = maxWidth + 'px';
            canvas.style.height = (maxWidth * ratio) + 'px';
        }
        resizeCanvas();
        window.addEventListener('resize', resizeCanvas);

        // Game variables
        let gameRunning = false;
        let gameSpeed = 6;
        let score = 0;
        let highScore = localStorage.getItem('dinoHighScore') || 0;
        highScoreElement.textContent = highScore;

        // Dino
        const dino = {
            x: 50,
            y: 220,
            width: 50,
            height: 60,
            velocityY: 0,
            jumping: false,
            ducking: false,
            duckHeight: 35,
            normalHeight: 60,
            groundY: 220,
            frame: 0,
            frameCount: 0
        };

        // Obstacles
        let obstacles = [];
        let obstacleTimer = 0;
        const obstacleInterval = 100;

        // Clouds
        let clouds = [];

        // Ground
        let groundX = 0;

        // Physics
        const gravity = 0.8;
        const jumpForce = -15;

        // Initialize clouds
        function initClouds() {
            clouds = [];
            for (let i = 0; i < 5; i++) {
                clouds.push({
                    x: Math.random() * canvas.width,
                    y: 30 + Math.random() * 60,
                    width: 60 + Math.random() * 40,
                    speed: 1 + Math.random()
                });
            }
        }

        // Draw cloud
        function drawCloud(cloud) {
            ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
            ctx.beginPath();
            ctx.arc(cloud.x, cloud.y, 20, 0, Math.PI * 2);
            ctx.arc(cloud.x + 25, cloud.y - 10, 25, 0, Math.PI * 2);
            ctx.arc(cloud.x + 50, cloud.y, 20, 0, Math.PI * 2);
            ctx.arc(cloud.x + 25, cloud.y + 5, 22, 0, Math.PI * 2);
            ctx.fill();
        }

        // Draw dino
        function drawDino() {
            const height = dino.ducking ? dino.duckHeight : dino.normalHeight;
            const y = dino.ducking ? dino.groundY + (dino.normalHeight - dino.duckHeight) : dino.y;

            // Body
            ctx.fillStyle = '#2d5016';
            ctx.beginPath();
            ctx.roundRect(dino.x, y, dino.width - 10, height, 10);
            ctx.fill();

            // Head
            ctx.fillStyle = '#3d6b1e';
            if (!dino.ducking) {
                ctx.beginPath();
                ctx.roundRect(dino.x + 15, y - 25, 35, 30, 8);
                ctx.fill();

                // Eye
                ctx.fillStyle = '#fff';
                ctx.beginPath();
                ctx.arc(dino.x + 40, y - 12, 6, 0, Math.PI * 2);
                ctx.fill();
                ctx.fillStyle = '#000';
                ctx.beginPath();
                ctx.arc(dino.x + 42, y - 12, 3, 0, Math.PI * 2);
                ctx.fill();

                // Mouth
                ctx.strokeStyle = '#1a3d0c';
                ctx.lineWidth = 2;
                ctx.beginPath();
                ctx.moveTo(dino.x + 45, y - 5);
                ctx.lineTo(dino.x + 50, y - 5);
                ctx.stroke();
            } else {
                // Ducking head
                ctx.beginPath();
                ctx.roundRect(dino.x + 30, y - 5, 30, 20, 5);
                ctx.fill();

                // Eye when ducking
                ctx.fillStyle = '#fff';
                ctx.beginPath();
                ctx.arc(dino.x + 50, y + 5, 4, 0, Math.PI * 2);
                ctx.fill();
                ctx.fillStyle = '#000';
                ctx.beginPath();
                ctx.arc(dino.x + 51, y + 5, 2, 0, Math.PI * 2);
                ctx.fill();
            }

            // Legs animation
            ctx.fillStyle = '#2d5016';
            dino.frameCount++;
            if (dino.frameCount > 5) {
                dino.frame = (dino.frame + 1) % 2;
                dino.frameCount = 0;
            }

            if (!dino.jumping) {
                const legOffset = dino.frame === 0 ? 5 : -5;
                ctx.fillRect(dino.x + 8, y + height - 5, 8, 15 + legOffset);
                ctx.fillRect(dino.x + 25, y + height - 5, 8, 15 - legOffset);
            } else {
                ctx.fillRect(dino.x + 8, y + height - 5, 8, 12);
                ctx.fillRect(dino.x + 25, y + height - 5, 8, 12);
            }

            // Tail
            ctx.fillStyle = '#3d6b1e';
            ctx.beginPath();
            ctx.moveTo(dino.x, y + 20);
            ctx.lineTo(dino.x - 20, y + 15);
            ctx.lineTo(dino.x, y + 35);
            ctx.fill();
        }

        // Draw cactus obstacle
        function drawCactus(obstacle) {
            ctx.fillStyle = '#228B22';
            
            // Main stem
            ctx.beginPath();
            ctx.roundRect(obstacle.x + 10, obstacle.y, 20, obstacle.height, 5);
            ctx.fill();

            // Left arm
            ctx.beginPath();
            ctx.roundRect(obstacle.x, obstacle.y + 15, 12, 25, 4);
            ctx.fill();
            ctx.beginPath();
            ctx.roundRect(obstacle.x, obstacle.y + 10, 8, 15, 3);
            ctx.fill();

            // Right arm
            ctx.beginPath();
            ctx.roundRect(obstacle.x + 28, obstacle.y + 25, 12, 20, 4);
            ctx.fill();
            ctx.beginPath();
            ctx.roundRect(obstacle.x + 32, obstacle.y + 20, 8, 12, 3);
            ctx.fill();

            // Spikes
            ctx.fillStyle = '#1a6b1a';
            for (let i = 0; i < 5; i++) {
                ctx.beginPath();
                ctx.arc(obstacle.x + 20, obstacle.y + i * 12, 2, 0, Math.PI * 2);
                ctx.fill();
            }
        }

        // Draw bird obstacle
        function drawBird(obstacle) {
            ctx.fillStyle = '#8B4513';
            
            // Body
            ctx.beginPath();
            ctx.ellipse(obstacle.x + 20, obstacle.y + 15, 20, 12, 0, 0, Math.PI * 2);
            ctx.fill();

            // Head
            ctx.beginPath();
            ctx.arc(obstacle.x + 40, obstacle.y + 10, 10, 0, Math.PI * 2);
            ctx.fill();

            // Beak
            ctx.fillStyle = '#FFA500';
            ctx.beginPath();
            ctx.moveTo(obstacle.x + 50, obstacle.y + 10);
            ctx.lineTo(obstacle.x + 60, obstacle.y + 12);
            ctx.lineTo(obstacle.x + 50, obstacle.y + 14);
            ctx.fill();

            // Eye
            ctx.fillStyle = '#fff';
            ctx.beginPath();
            ctx.arc(obstacle.x + 43, obstacle.y + 8, 4, 0, Math.PI * 2);
            ctx.fill();
            ctx.fillStyle = '#000';
            ctx.beginPath();
            ctx.arc(obstacle.x + 44, obstacle.y + 8, 2, 0, Math.PI * 2);
            ctx.fill();

            // Wings animation
            ctx.fillStyle = '#6B3E26';
            const wingY = Math.sin(Date.now() / 100) * 10;
            ctx.beginPath();
            ctx.moveTo(obstacle.x + 10, obstacle.y + 15);
            ctx.lineTo(obstacle.x - 10, obstacle.y + wingY);
            ctx.lineTo(obstacle.x + 20, obstacle.y + 15);
            ctx.fill();
        }

        // Draw ground
        function drawGround() {
            ctx.fillStyle = '#8B4513';
            ctx.fillRect(0, 280, canvas.width, 20);

            ctx.fillStyle = '#654321';
            groundX -= gameSpeed;
            if (groundX <= -20) groundX = 0;

            for (let i = groundX; i < canvas.width; i += 20) {
                ctx.fillRect(i, 282, 15, 3);
                ctx.fillRect(i + 7, 288, 10, 2);
            }

            // Grass
            ctx.fillStyle = '#228B22';
            ctx.fillRect(0, 275, canvas.width, 5);
        }

        // Spawn obstacle
        function spawnObstacle() {
            const type = Math.random() > 0.7 ? 'bird' : 'cactus';
            const obstacle = {
                x: canvas.width,
                type: type,
                width: type === 'cactus' ? 40 : 60,
                height: type === 'cactus' ? 50 + Math.random() * 20 : 30
            };
            
            if (type === 'cactus') {
                obstacle.y = 280 - obstacle.height;
            } else {
                obstacle.y = Math.random() > 0.5 ? 180 : 230;
            }
            
            obstacles.push(obstacle);
        }

        // Check collision
        function checkCollision() {
            const dinoHeight = dino.ducking ? dino.duckHeight : dino.normalHeight;
            const dinoY = dino.ducking ? dino.groundY + (dino.normalHeight - dino.duckHeight) : dino.y;

            for (let obstacle of obstacles) {
                if (dino.x + dino.width - 15 > obstacle.x &&
                    dino.x + 10 < obstacle.x + obstacle.width &&
                    dinoY + dinoHeight > obstacle.y &&
                    dinoY < obstacle.y + obstacle.height) {
                    return true;
                }
            }
            return false;
        }

        // Game over
        function gameOver() {
            gameRunning = false;
            finalScoreElement.textContent = score;
            
            if (score > highScore) {
                highScore = score;
                localStorage.setItem('dinoHighScore', highScore);
                highScoreElement.textContent = highScore;
            }
            
            gameOverOverlay.style.display = 'flex';
        }

        // Reset game
        function resetGame() {
            score = 0;
            gameSpeed = 6;
            obstacles = [];
            obstacleTimer = 0;
            dino.y = dino.groundY;
            dino.velocityY = 0;
            dino.jumping = false;
            dino.ducking = false;
            initClouds();
            gameOverOverlay.style.display = 'none';
            gameRunning = true;
            gameLoop();
        }

        // Update game
        function update() {
            // Dino physics
            if (dino.jumping) {
                dino.velocityY += gravity;
                dino.y += dino.velocityY;

                if (dino.y >= dino.groundY) {
                    dino.y = dino.groundY;
                    dino.jumping = false;
                    dino.velocityY = 0;
                }
            }

            // Update obstacles
            obstacleTimer++;
            if (obstacleTimer >= obstacleInterval - Math.min(score / 10, 50)) {
                spawnObstacle();
                obstacleTimer = 0;
            }

            for (let i = obstacles.length - 1; i >= 0; i--) {
                obstacles[i].x -= gameSpeed;
                
                if (obstacles[i].x + obstacles[i].width < 0) {
                    obstacles.splice(i, 1);
                    score++;
                    scoreElement.textContent = score;
                }
            }

            // Update clouds
            for (let cloud of clouds) {
                cloud.x -= cloud.speed;
                if (cloud.x + cloud.width < 0) {
                    cloud.x = canvas.width + 50;
                    cloud.y = 30 + Math.random() * 60;
                }
            }

            // Increase speed
            if (score > 0 && score % 10 === 0) {
                gameSpeed = 6 + Math.floor(score / 10) * 0.5;
            }

            // Check collision
            if (checkCollision()) {
                gameOver();
            }
        }

        // Draw game
        function draw() {
            // Sky gradient
            const gradient = ctx.createLinearGradient(0, 0, 0, 280);
            gradient.addColorStop(0, '#87CEEB');
            gradient.addColorStop(0.6, '#E0F6FF');
            gradient.addColorStop(0.6, '#90EE90');
            gradient.addColorStop(1, '#228B22');
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            // Sun
            ctx.fillStyle = '#FFD700';
            ctx.beginPath();
            ctx.arc(700, 50, 30, 0, Math.PI * 2);
            ctx.fill();
            ctx.fillStyle = 'rgba(255, 215, 0, 0.3)';
            ctx.beginPath();
            ctx.arc(700, 50, 45, 0, Math.PI * 2);
            ctx.fill();

            // Clouds
            for (let cloud of clouds) {
                drawCloud(cloud);
            }

            // Ground
            drawGround();

            // Obstacles
            for (let obstacle of obstacles) {
                if (obstacle.type === 'cactus') {
                    drawCactus(obstacle);
                } else {
                    drawBird(obstacle);
                }
            }

            // Dino
            drawDino();
        }

        // Game loop
        function gameLoop() {
            if (!gameRunning) return;
            
            update();
            draw();
            requestAnimationFrame(gameLoop);
        }

        // Controls
        function jump() {
            if (!dino.jumping && !dino.ducking) {
                dino.jumping = true;
                dino.velocityY = jumpForce;
            }
        }

        function duck(isDucking) {
            if (!dino.jumping) {
                dino.ducking = isDucking;
            }
        }

        // Event listeners
        document.addEventListener('keydown', (e) => {
            if (!gameRunning) return;
            
            if (e.code === 'Space' || e.code === 'ArrowUp') {
                e.preventDefault();
                jump();
            }
            if (e.code === 'ArrowDown') {
                e.preventDefault();
                duck(true);
            }
        });

        document.addEventListener('keyup', (e) => {
            if (e.code === 'ArrowDown') {
                duck(false);
            }
        });

        // Touch controls
        canvas.addEventListener('touchstart', (e) => {
            e.preventDefault();
            if (gameRunning) {
                const touch = e.touches[0];
                const rect = canvas.getBoundingClientRect();
                const y = touch.clientY - rect.top;
                
                if (y > rect.height / 2) {
                    duck(true);
                } else {
                    jump();
                }
            }
        });

        canvas.addEventListener('touchend', (e) => {
            duck(false);
        });

        canvas.addEventListener('click', () => {
            if (gameRunning) {
                jump();
            }
        });

        // Start and restart buttons
        startBtn.addEventListener('click', () => {
            startScreen.style.display = 'none';
            resetGame();
        });

        restartBtn.addEventListener('click', resetGame);

        // Initialize
        initClouds();
        draw();
  </script>
</body>

</html>