sidprak commited on
Commit
26a5598
Β·
verified Β·
1 Parent(s): 1a1efd5
Files changed (1) hide show
  1. index.html +103 -1
index.html CHANGED
@@ -479,7 +479,109 @@
479
  document.addEventListener('keydown', (e) => {
480
  switch (e.key) {
481
  case 'ArrowUp':
482
- if (direction !== 'down') nextDirection = 'up';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  break;
484
  case 'ArrowDown':
485
  if (direction !==
 
479
  document.addEventListener('keydown', (e) => {
480
  switch (e.key) {
481
  case 'ArrowUp':
482
+ if (direction !== 'up') nextDirection = 'down';
483
+ break;
484
+ case 'ArrowLeft':
485
+ if (direction !== 'right') nextDirection = 'left';
486
+ break;
487
+ case 'ArrowRight':
488
+ if (direction !== 'left') nextDirection = 'right';
489
+ break;
490
+ case ' ':
491
+ isPaused = !isPaused;
492
+ if (isPaused) {
493
+ bgMusic.pause();
494
+ } else if (isSoundOn) {
495
+ bgMusic.play().catch(e => console.log("Autoplay prevented:", e));
496
+ }
497
+ break;
498
+ }
499
+ });
500
+
501
+ // Touch controls for mobile
502
+ canvas.addEventListener('touchstart', (e) => {
503
+ touchStartX = e.touches[0].clientX;
504
+ touchStartY = e.touches[0].clientY;
505
+ }, { passive: false });
506
+
507
+ canvas.addEventListener('touchmove', (e) => {
508
+ if (!touchStartX || !touchStartY) return;
509
+
510
+ const touchEndX = e.touches[0].clientX;
511
+ const touchEndY = e.touches[0].clientY;
512
+
513
+ const diffX = touchStartX - touchEndX;
514
+ const diffY = touchStartY - touchEndY;
515
+
516
+ if (Math.abs(diffX) > Math.abs(diffY)) {
517
+ // Horizontal swipe
518
+ if (diffX > 0 && direction !== 'right') {
519
+ nextDirection = 'left';
520
+ } else if (diffX < 0 && direction !== 'left') {
521
+ nextDirection = 'right';
522
+ }
523
+ } else {
524
+ // Vertical swipe
525
+ if (diffY > 0 && direction !== 'down') {
526
+ nextDirection = 'up';
527
+ } else if (diffY < 0 && direction !== 'up') {
528
+ nextDirection = 'down';
529
+ }
530
+ }
531
+
532
+ touchStartX = 0;
533
+ touchStartY = 0;
534
+ e.preventDefault();
535
+ }, { passive: false });
536
+
537
+ // Button event listeners
538
+ startBtn.addEventListener('click', () => {
539
+ startScreen.classList.add('hidden');
540
+ initGame();
541
+ });
542
+
543
+ restartBtn.addEventListener('click', () => {
544
+ gameOverScreen.classList.add('hidden');
545
+ initGame();
546
+ });
547
+
548
+ soundBtn.addEventListener('click', () => {
549
+ isSoundOn = !isSoundOn;
550
+ if (isSoundOn) {
551
+ soundBtn.innerHTML = '<i data-feather="volume-2"></i>';
552
+ bgMusic.play().catch(e => console.log("Autoplay prevented:", e));
553
+ } else {
554
+ soundBtn.innerHTML = '<i data-feather="volume-x"></i>';
555
+ bgMusic.pause();
556
+ }
557
+ feather.replace();
558
+ });
559
+
560
+ fullscreenBtn.addEventListener('click', () => {
561
+ if (!document.fullscreenElement) {
562
+ canvas.requestFullscreen().catch(err => {
563
+ alert(`Error attempting to enable fullscreen: ${err.message}`);
564
+ });
565
+ } else {
566
+ document.exitFullscreen();
567
+ }
568
+ });
569
+
570
+ // Initialize
571
+ window.addEventListener('load', () => {
572
+ resizeCanvas();
573
+ drawGame();
574
+ feather.replace();
575
+
576
+ // Try to autoplay music with user interaction
577
+ document.addEventListener('click', () => {
578
+ if (isSoundOn) {
579
+ bgMusic.play().catch(e => console.log("Autoplay prevented:", e));
580
+ }
581
+ }, { once: true });
582
+ });
583
+ </script>
584
+ 'down') nextDirection = 'up';
585
  break;
586
  case 'ArrowDown':
587
  if (direction !==