Advertisement
nrzmalik

Sky Picker Storyline eLearning Game

Jul 2nd, 2025
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 7.76 KB | Source Code | 0 0
  1. const FlappyBirdGame = {
  2.     state: {
  3.         isJumping: false,
  4.         fallSpeed: 0,
  5.         currentX: 0,
  6.         currentY: 0,
  7.         currentRotation: 0,
  8.         gameLoop: null,
  9.         animationFrame: null,
  10.         moveSpeed: 5,
  11.         lastJumpTime: 0,
  12.         jumpCooldown: 150,
  13.         maxFallSpeed: 1.8,
  14.         airResistance: 0.98,
  15.         keysPressed: {},
  16.         initialX: 0,
  17.         initialY: 0
  18.     },
  19.  
  20.     init() {
  21.         this.resetState();
  22.         this.setupEventListeners();
  23.         this.startGame();
  24.  
  25.         setTimeout(() => {
  26.             if (this.state.gameLoop) {
  27.                 this.state.isJumping = true;
  28.                 this.state.fallSpeed = -3.5;
  29.                 this.state.lastJumpTime = Date.now();
  30.             }
  31.         }, 100);
  32.     },
  33.  
  34.     resetState() {
  35.         this.state = {
  36.             ...this.state,
  37.             isJumping: false,
  38.             fallSpeed: 0,
  39.             currentRotation: 0,
  40.             gameLoop: null,
  41.             animationFrame: null,
  42.             keysPressed: {},
  43.             lastJumpTime: 0,
  44.             gameStarted: false
  45.         };
  46.         setVar("GameOver", false);
  47.     },
  48.  
  49.     setupEventListeners() {
  50.         document.addEventListener('keydown', this.handleKeyDown.bind(this));
  51.         document.addEventListener('keyup', this.handleKeyUp.bind(this));
  52.         document.addEventListener('touchstart', this.handleTouch.bind(this));
  53.     },
  54.  
  55.     handleKeyDown(event) {
  56.         this.state.keysPressed[event.code] = true;
  57.  
  58.         if (event.code === 'Space') {
  59.             event.preventDefault();
  60.             if (!this.state.gameStarted) {
  61.                 this.state.gameStarted = true;
  62.                 this.startGameLoop();
  63.             }
  64.             this.jump();
  65.         } else if (event.code === 'ArrowLeft' || event.code === 'ArrowRight') {
  66.             event.preventDefault();
  67.         }
  68.     },
  69.  
  70.     handleKeyUp(event) {
  71.         this.state.keysPressed[event.code] = false;
  72.     },
  73.  
  74.     handleTouch(event) {
  75.         event.preventDefault();
  76.         if (!this.state.gameStarted) {
  77.             this.state.gameStarted = true;
  78.             this.startGameLoop();
  79.         }
  80.         this.jump();
  81.     },
  82.  
  83.     startGame() {
  84.         const bird = document.querySelector('div[data-acc-text="Player"]');
  85.         if (!bird) return;
  86.  
  87.         const slide = document.querySelector('#slide');
  88.         const slideWidth = slide.offsetWidth;
  89.         const slideHeight = slide.offsetHeight;
  90.         const birdWidth = bird.offsetWidth;
  91.         const birdHeight = bird.offsetHeight;
  92.  
  93.         this.state.initialX = (slideWidth - birdWidth) / 2;
  94.         this.state.initialY = (slideHeight - birdHeight) / 2;
  95.         this.state.currentX = this.state.initialX;
  96.         this.state.currentY = this.state.initialY;
  97.  
  98.         this.updateBirdPosition(bird);
  99.         this.startGameLoop();
  100.  
  101.         this.state.isJumping = true;
  102.         this.state.fallSpeed = -3.5;
  103.     },
  104.  
  105.     stopGame() {
  106.         this.state.gameLoop = false;
  107.         this.state.gameStarted = false;
  108.         cancelAnimationFrame(this.state.animationFrame);
  109.     },
  110.  
  111.     jump() {
  112.         const currentTime = Date.now();
  113.  
  114.         if (currentTime - this.state.lastJumpTime >= this.state.jumpCooldown) {
  115.             this.state.isJumping = true;
  116.             this.state.lastJumpTime = currentTime;
  117.  
  118.             if (this.state.fallSpeed > 0) {
  119.                 this.state.fallSpeed = 0;
  120.             }
  121.         }
  122.     },
  123.  
  124.     forceDropBird() {
  125.         this.state.fallSpeed = 15;
  126.         this.state.isJumping = false;
  127.         this.state.currentRotation = 45;
  128.     },
  129.  
  130.     startGameLoop() {
  131.         if (!this.state.gameLoop) {
  132.             this.state.gameLoop = true;
  133.             this.state.fallSpeed = 1;
  134.             this.animate();
  135.         }
  136.     },
  137.  
  138.     animate() {
  139.         if (!this.state.gameLoop) return;
  140.  
  141.         const bird = document.querySelector('div[data-acc-text="Player"]');
  142.         if (!bird) return;
  143.  
  144.         this.updatePosition();
  145.         this.checkBoundaries(bird);
  146.         this.updateBirdPosition(bird);
  147.         this.checkCollisions(bird);
  148.  
  149.         this.state.animationFrame = requestAnimationFrame(this.animate.bind(this));
  150.     },
  151.  
  152.     updatePosition() {
  153.         if (this.state.keysPressed['ArrowLeft']) {
  154.             this.state.currentX -= this.state.moveSpeed;
  155.         }
  156.         if (this.state.keysPressed['ArrowRight']) {
  157.             this.state.currentX += this.state.moveSpeed;
  158.         }
  159.  
  160.         if (!this.state.isJumping) {
  161.             this.state.fallSpeed += 0.3;
  162.             this.state.fallSpeed *= this.state.airResistance;
  163.  
  164.             if (this.state.fallSpeed > this.state.maxFallSpeed) {
  165.                 this.state.fallSpeed = this.state.maxFallSpeed;
  166.             }
  167.         }
  168.  
  169.         this.state.currentY += this.state.fallSpeed;
  170.  
  171.         if (this.state.isJumping) {
  172.             this.state.fallSpeed = -3.5;
  173.             this.state.isJumping = false;
  174.         }
  175.     },
  176.  
  177.     checkCollisions(bird) {
  178.         const birdRect = bird.getBoundingClientRect();
  179.         this.checkGhostCollisions(birdRect);
  180.         this.checkCorrectCollisions(birdRect);
  181.     },
  182.  
  183.     checkGhostCollisions(birdRect) {
  184.         const ghosts = document.querySelectorAll('div[data-acc-text="Ghosts"]');
  185.         ghosts.forEach((ghost) => {
  186.             if (this.isColliding(birdRect, ghost.getBoundingClientRect()) &&
  187.                 !ghost.classList.contains('hidden')) {
  188.                 this.forceDropBird();
  189.                 setVar("GameOver", true);
  190.                 ghost.classList.remove('shown');
  191.                 ghost.classList.add('hidden');
  192.             }
  193.         });
  194.     },
  195.  
  196.     checkCorrectCollisions(birdRect) {
  197.         const correctItems = document.querySelectorAll('div[data-acc-text="Correct"]');
  198.         correctItems.forEach((item) => {
  199.             if (this.isColliding(birdRect, item.getBoundingClientRect()) &&
  200.                 !item.classList.contains('hidden')) {
  201.                 this.forceDropBird();
  202.                 let correct = getVar("correct");
  203.                 setVar("correct", correct + 1);
  204.                 item.classList.remove('shown');
  205.                 item.classList.add('hidden');
  206.             }
  207.         });
  208.     },
  209.  
  210.     isColliding(rect1, rect2) {
  211.         return !(rect1.right < rect2.left ||
  212.                 rect1.left > rect2.right ||
  213.                 rect1.bottom < rect2.top ||
  214.                 rect1.top > rect2.bottom);
  215.     },
  216.  
  217.     checkBoundaries(bird) {
  218.         const slide = document.querySelector('#slide');
  219.         const slideHeight = slide.offsetHeight;
  220.         const slideWidth = slide.offsetWidth;
  221.         const birdHeight = bird.offsetHeight;
  222.         const birdWidth = bird.offsetWidth;
  223.  
  224.         if (this.state.currentY > slideHeight - birdHeight) {
  225.             this.state.currentY = slideHeight - birdHeight;
  226.             this.state.fallSpeed = 0;
  227.             this.state.currentRotation = 0;
  228.             setVar("GameOver", true);
  229.             this.stopGame();
  230.         }
  231.         if (this.state.currentY < 0) {
  232.             this.state.currentY = 0;
  233.             this.state.fallSpeed = 0;
  234.             this.state.currentRotation = 0;
  235.         }
  236.  
  237.         if (this.state.currentX > slideWidth - birdWidth) {
  238.             this.state.currentX = slideWidth - birdWidth;
  239.         }
  240.         if (this.state.currentX < 0) {
  241.             this.state.currentX = 0;
  242.         }
  243.     },
  244.  
  245.     updateBirdPosition(bird) {
  246.         let targetRotation = this.state.isJumping ? -25 : Math.min(this.state.fallSpeed * 10, 45);
  247.  
  248.         this.state.currentRotation = this.state.currentRotation * 0.9 + targetRotation * 0.1;
  249.  
  250.         bird.style.transform = `translate(${this.state.currentX}px, ${this.state.currentY}px) rotate(${this.state.currentRotation}deg)`;
  251.         bird.style.transition = 'transform 0.1s ease-out';
  252.     }
  253. };
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement