Advertisement
EzFlow997

YeYeJoris Flappy Bird

Mar 3rd, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. // Define game variables
  2.  
  3. let canvas, ctx;
  4.  
  5. let bird, gravity, pipes;
  6.  
  7. let gameOver, score, highScore;
  8.  
  9.  
  10.  
  11. // Initialize game
  12.  
  13. function init() {
  14.  
  15. canvas = document.createElement('canvas');
  16.  
  17. canvas.width = 800;
  18.  
  19. canvas.height = 600;
  20.  
  21. document.body.appendChild(canvas);
  22.  
  23. ctx = canvas.getContext('2d');
  24.  
  25.  
  26.  
  27. bird = { x: canvas.width / 4, y: canvas.height / 2, size: 20, speed: 0 };
  28.  
  29. gravity = 0.5;
  30.  
  31. pipes = [];
  32.  
  33. gameOver = false;
  34.  
  35. score = 0;
  36.  
  37.  
  38.  
  39. // Retrieve high score from local storage
  40.  
  41. highScore = localStorage.getItem('highScore') || 0;
  42.  
  43.  
  44.  
  45. // Event listener for jump
  46.  
  47. document.addEventListener('keydown', function(event) {
  48.  
  49. if (event.code === 'Space' && !gameOver) {
  50.  
  51. bird.speed = -8; // Adjust jump strength if needed
  52.  
  53. }
  54.  
  55. });
  56.  
  57.  
  58.  
  59. // Start game loop
  60.  
  61. setInterval(update, 1000 / 60);
  62.  
  63. }
  64.  
  65.  
  66.  
  67. // Update game state
  68.  
  69. function update() {
  70.  
  71. // Clear canvas
  72.  
  73. ctx.fillStyle = 'lightblue'; // Background color
  74.  
  75. ctx.fillRect(0, 0, canvas.width, canvas.height);
  76.  
  77.  
  78.  
  79. // Move bird
  80.  
  81. bird.speed += gravity;
  82.  
  83. bird.y += bird.speed;
  84.  
  85.  
  86.  
  87. // Draw bird
  88.  
  89. ctx.fillStyle = 'yellow';
  90.  
  91. ctx.beginPath();
  92.  
  93. ctx.arc(bird.x, bird.y, bird.size, 0, Math.PI * 2);
  94.  
  95. ctx.fill();
  96.  
  97.  
  98.  
  99. // Generate pipes
  100.  
  101. if (pipes.length === 0 || pipes[pipes.length - 1].x < canvas.width - 300) {
  102.  
  103. pipes.push({ x: canvas.width, gapY: Math.random() * (canvas.height - 300) + 150 });
  104.  
  105. }
  106.  
  107.  
  108.  
  109. // Move pipes
  110.  
  111. pipes.forEach(function(pipe) {
  112.  
  113. pipe.x -= 2;
  114.  
  115.  
  116.  
  117. // Check collision with pipes
  118.  
  119. if (bird.x + (bird.size/2) >= pipe.x && bird.x - (bird.size/2) <= pipe.x + 50) {
  120.  
  121. if (bird.y - (bird.size/2) <= pipe.gapY || bird.y + (bird.size/2) >= pipe.gapY + 200) {
  122.  
  123. gameOver = true; // Collision with pipes
  124. console.log("BIRD Y "+bird.y+"\nBIRD SIZE "+bird.size+"\nPIPE GAP "+pipe.gapY)
  125.  
  126. }
  127.  
  128. }
  129.  
  130.  
  131.  
  132. // Pass pipes
  133.  
  134. if (pipe.x + 50 < bird.x && !pipe.passed && gameOver = false) {
  135.  
  136. pipe.passed = true;
  137.  
  138. score++;
  139.  
  140. }
  141.  
  142. });
  143.  
  144.  
  145.  
  146. // Remove off-screen pipes
  147.  
  148. pipes = pipes.filter(pipe => pipe.x > -50);
  149.  
  150.  
  151.  
  152. // Draw pipes
  153.  
  154. ctx.fillStyle = 'green';
  155.  
  156. pipes.forEach(function(pipe) {
  157.  
  158. ctx.fillRect(pipe.x, 0, 50, pipe.gapY);
  159.  
  160. ctx.fillRect(pipe.x, pipe.gapY + 200, 50, canvas.height - pipe.gapY - 200);
  161.  
  162. });
  163.  
  164.  
  165.  
  166. // Draw score
  167.  
  168. ctx.fillStyle = 'white';
  169.  
  170. ctx.font = '24px Arial';
  171.  
  172. ctx.fillText('Score: ' + score, 10, 30);
  173.  
  174.  
  175.  
  176. // Update high score
  177.  
  178. if (score > highScore) {
  179.  
  180. highScore = score;
  181.  
  182. localStorage.setItem('highScore', highScore);
  183.  
  184. }
  185.  
  186.  
  187.  
  188. // Draw high score
  189.  
  190. ctx.fillText('High Score: ' + highScore, 10, 60);
  191.  
  192.  
  193.  
  194. // Check game over
  195.  
  196. if (bird.y > canvas.height || gameOver) {
  197.  
  198. gameOver = true;
  199.  
  200. ctx.fillStyle = 'black';
  201.  
  202. ctx.font = '40px Arial';
  203.  
  204. ctx.fillText('Game Over', canvas.width / 2 - 100, canvas.height / 2);
  205.  
  206. }
  207.  
  208. }
  209.  
  210.  
  211.  
  212. // Start the game when the page loads
  213.  
  214. window.onload = init;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement