Flappy Bird游戏使用JavaScript编写

Flappy Bird游戏使用JavaScript编写

Flappy Bird是一款无尽的游戏,玩家可以控制一只鸟。玩家需要防止鸟与管道等障碍物碰撞。每次鸟穿过管道时,得分增加一点。游戏在鸟与管道碰撞或因重力坠落时结束。下面的部分描述了构建此游戏所需的步骤。

HTML部分: 在此部分,创建和加载游戏的元素。选择背景、鸟、障碍物和得分元素的图像。接下来,我们创建并链接style.css和index.js文件。

<!DOCTYPE html> 
<html> 
  
<head> 
    <link rel="stylesheet" href="style.css"> 
</head> 
  
<body> 
    <div class="background"></div> 
    <img class="bird" src="logo.png" alt="bird-img"> 
    <div class="message"> 
        Press Enter To Start Game 
    </div> 
    <div class="score"> 
        <span class="score_title"></span> 
        <span class="score_val"></span> 
    </div> 
    <script src="gfg.js"></script> 
</body> 
  
</html> 

CSS部分: 根据需要,此部分修改游戏对象的大小、位置和样式。

* { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
} 
  
body { 
    height: 100vh; 
    width: 100vw; 
} 
  
.background { 
    height: 100vh; 
    width: 100vw; 
    background-color: skyblue; 
} 
  
.bird { 
    height: 100px; 
    width: 160px; 
    position: fixed; 
    top: 40vh; 
    left: 30vw; 
    z-index: 100; 
} 
  
.pipe_sprite { 
    position: fixed; 
    top: 40vh; 
    left: 100vw; 
    height: 70vh; 
    width: 6vw; 
    background-color: green; 
} 
  
.message { 
    position: fixed; 
    z-index: 10; 
    height: 10vh; 
    font-size: 10vh; 
    font-weight: 100; 
    color: black; 
    top: 12vh; 
    left: 20vw; 
    text-align: center; 
} 
  
.score { 
    position: fixed; 
    z-index: 10; 
    height: 10vh; 
    font-size: 10vh; 
    font-weight: 100; 
    color: goldenrod; 
    top: 0; 
    left: 0; 
} 
  
.score_val { 
    color: gold; 
} 

JavaScript部分: 该部分包含了控制游戏状态和移动对象的代码部分。在该部分中,需要按照以下步骤进行操作。

  • 在JavaScript文件中获取对鸟和背景图像的引用。
  • 为背景滚动速度、鸟的飞行速度和重力设置一些值。
  • 创建无限滚动背景。
  • 添加事件监听器,监听“enter”键按下事件,将游戏状态改为播放状态,并通过每帧减少鸟的y坐标上的重力值,施加重力。
  • 在视图宽度结尾处生成障碍(管道),使其初始时不可见,但随着背景的移动,通过减少管道的x坐标来移动,这样看起来就像鸟在移动。
  • 与地面和管道发生碰撞,如果鸟发生碰撞,则将游戏状态改为结束状态,并显示重新开始游戏的消息。
  • 在每次成功通过管道之后,增加分数值。
// Background scrolling speed 
let move_speed = 3; 
    
// Gravity constant value 
let gravity = 0.5; 
    
// Getting reference to the bird element 
let bird = document.querySelector('.bird'); 
    
// Getting bird element properties 
let bird_props = bird.getBoundingClientRect(); 
let background = 
    document.querySelector('.background') 
            .getBoundingClientRect(); 
    
// Getting reference to the score element 
let score_val = 
    document.querySelector('.score_val'); 
let message = 
    document.querySelector('.message'); 
let score_title = 
    document.querySelector('.score_title'); 
    
// Setting initial game state to start 
let game_state = 'Start'; 
    
// Add an eventlistener for key presses 
document.addEventListener('keydown', (e) => { 
    
  // Start the game if enter key is pressed 
  if (e.key == 'Enter' && 
      game_state != 'Play') { 
    document.querySelectorAll('.pipe_sprite') 
              .forEach((e) => { 
      e.remove(); 
    }); 
    bird.style.top = '40vh'; 
    game_state = 'Play'; 
    message.innerHTML = ''; 
    score_title.innerHTML = 'Score : '; 
    score_val.innerHTML = '0'; 
    play(); 
  } 
}); 
function play() { 
  function move() { 
      
    // Detect if game has ended 
    if (game_state != 'Play') return; 
      
    // Getting reference to all the pipe elements 
    let pipe_sprite = document.querySelectorAll('.pipe_sprite'); 
    pipe_sprite.forEach((element) => { 
        
      let pipe_sprite_props = element.getBoundingClientRect(); 
      bird_props = bird.getBoundingClientRect(); 
        
      // Delete the pipes if they have moved out 
      // of the screen hence saving memory 
      if (pipe_sprite_props.right <= 0) { 
        element.remove(); 
      } else { 
        // Collision detection with bird and pipes 
        if ( 
          bird_props.left < pipe_sprite_props.left + 
          pipe_sprite_props.width && 
          bird_props.left + 
          bird_props.width > pipe_sprite_props.left && 
          bird_props.top < pipe_sprite_props.top + 
          pipe_sprite_props.height && 
          bird_props.top + 
          bird_props.height > pipe_sprite_props.top 
        ) { 
            
          // Change game state and end the game 
          // if collision occurs 
          game_state = 'End'; 
          message.innerHTML = 'Press Enter To Restart'; 
          message.style.left = '28vw'; 
          return; 
        } else { 
          // Increase the score if player 
          // has the successfully dodged the  
          if ( 
            pipe_sprite_props.right < bird_props.left && 
            pipe_sprite_props.right +  
            move_speed >= bird_props.left && 
            element.increase_score == '1'
          ) { 
            score_val.innerHTML = +score_val.innerHTML + 1; 
          } 
          element.style.left =  
            pipe_sprite_props.left - move_speed + 'px'; 
        } 
      } 
    }); 
  
    requestAnimationFrame(move); 
  } 
  requestAnimationFrame(move); 
  
  let bird_dy = 0; 
  function apply_gravity() { 
    if (game_state != 'Play') return; 
    bird_dy = bird_dy + gravity; 
    document.addEventListener('keydown', (e) => { 
      if (e.key == 'ArrowUp' || e.key == ' ') { 
        bird_dy = -7.6; 
      } 
    }); 
  
    // Collision detection with bird and 
    // window top and bottom 
  
    if (bird_props.top <= 0 || 
        bird_props.bottom >= background.bottom) { 
      game_state = 'End'; 
      message.innerHTML = 'Press Enter To Restart'; 
      message.style.left = '28vw'; 
      return; 
    } 
    bird.style.top = bird_props.top + bird_dy + 'px'; 
    bird_props = bird.getBoundingClientRect(); 
    requestAnimationFrame(apply_gravity); 
  } 
  requestAnimationFrame(apply_gravity); 
  
  let pipe_seperation = 0; 
    
  // Constant value for the gap between two pipes 
  let pipe_gap = 35; 
  function create_pipe() { 
    if (game_state != 'Play') return; 
      
    // Create another set of pipes 
    // if distance between two pipe has exceeded 
    // a predefined value 
    if (pipe_seperation > 115) { 
      pipe_seperation = 0 
        
      // Calculate random position of pipes on y axis 
      let pipe_posi = Math.floor(Math.random() * 43) + 8; 
      let pipe_sprite_inv = document.createElement('div'); 
      pipe_sprite_inv.className = 'pipe_sprite'; 
      pipe_sprite_inv.style.top = pipe_posi - 70 + 'vh'; 
      pipe_sprite_inv.style.left = '100vw'; 
        
      // Append the created pipe element in DOM 
      document.body.appendChild(pipe_sprite_inv); 
      let pipe_sprite = document.createElement('div'); 
      pipe_sprite.className = 'pipe_sprite'; 
      pipe_sprite.style.top = pipe_posi + pipe_gap + 'vh'; 
      pipe_sprite.style.left = '100vw'; 
      pipe_sprite.increase_score = '1'; 
        
      // Append the created pipe element in DOM 
      document.body.appendChild(pipe_sprite); 
    } 
    pipe_seperation++; 
    requestAnimationFrame(create_pipe); 
  } 
  requestAnimationFrame(create_pipe); 
} 

输出:

Flappy Bird游戏使用JavaScript编写

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程