Advertisement
nrzmalik

Game JavaScript Code

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