JavaScript+jQuery小鸟游戏

202 阅读2分钟

HTML+CSS

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>小鸟游戏</title>

    <style>

        * {

            margin0;

            padding0;

        }


        html,

        body {

            width100%;

            height100%;

        }


        .box {

            width800px;

            height600px;

            margin50px 300px;

            overflow: hidden;

            position: relative;

            backgroundurl(./img/bg.png) repeat 0px 0px;

        }

        #bird {

            width34px;

            height25px;

            backgroundurl(./img/bird.png) no-repeat -10px -8px;

            position: absolute;

            left100px;

            top300px;

        }
    </style>

</head>

<body>

    <div class="box">

        <div id="bird"></div>

        <button class="start">开始游戏</button>

    </div>

    <script src="./js/jquery-1.8.3.min.js"></script>

    <script src="./js/index.js"></script>

</body>
</html>

JS代码

//获取游戏背景和小鸟

var box = $('.box');

var birdEle = $('#bird');



//初始化背景图

var sky = {

  x: 0 //背景图初始位置为0

}



//初始化小鸟

var bird = {

  speedX: 5, //小鸟在X轴的速度

  SpeedY: 0, //小鸟在Y轴的速度

  //小鸟坐标

  x: birdEle.offset().left, //小鸟初始位置在绝对定位的位置

  y: birdEle.offset().top,

}



var runing = false; //游戏状态

$('.start').click(function(){

    runing = true;

})

setInterval(function () {

\


  if (runing) {

    //小鸟飞行(其实是背景在动)

    sky.x -5; //背景每次-5px,以实现向左运动的效果

    box.css({

        backgroundPositionX : sky.x + 'px'

    })

    //小鸟上下运动

    bird.SpeedY += 1; //每一次点击小鸟向上10px后开始自增也就是再自动向下

    bird.y += bird.SpeedY; //小鸟自动不断向下运动

    //判断游戏状态

    if (bird.y < 0) { //超出游戏背景顶部时游戏结束

      runing = false;

      bird.y = 0;

    }

    if (bird.y + birdEle[0].offsetHeight > 600) { //超出游戏背景底部时游戏结束

      runing = false;

      bird.y = 600 - birdEle[0].offsetHeight;

    }

    birdEle.css({

        top:bird.y + 'px'

    })

  }

}, 30);



//点击时小鸟向上运动

box.click(function(){

    if(runing){

        bird.SpeedY = -10; //点击一次向上运动10px

    }

})



//创建管道

function creatPipe(position) {

  var pipe = {};

  pipe.x = position;

  pipe.upHeight = 200 + parseInt(Math.random() * 100); //上管道高度为200 - 300px

  pipe.doHeight = 600 - pipe.upHeight - 200; //下管道高度

  pipe.doTop = pipe.upHeight + 200; //上下两管道之间200px



  //创建上管道

  var upPipe = document.createElement('div'); //新建div

  upPipe.style.width = '52px';

  upPipe.style.height = pipe.upHeight + 'px';

  upPipe.style.background = 'url(./img/guandown.png) no-repeat center bottom';

  upPipe.style.position = 'absolute';

  upPipe.style.top = '0px';

  upPipe.style.left = pipe.x + 'px';

  box[0].appendChild(upPipe); //将上管道追加到游戏页面中




  //创建下管道

  var doPipe = document.createElement('div'); //新建div

  doPipe.style.width = '52px';

  doPipe.style.height = pipe.doHeight + 'px';

  doPipe.style.background = 'url(./img/guanup.png) no-repeat center top';

  doPipe.style.position = 'absolute';

  doPipe.style.top = pipe.doTop + 'px';

  doPipe.style.left = pipe.x + 'px';

  box[0].appendChild(doPipe); //将下管道追加到游戏页面中



  //管道进行运动

  setInterval(function () {

    if (runing) { //游戏处于运行状态时管道再运动

      pipe.x -2; //x方向不断-2px,以实现管道向左运动的效果

      upPipe.style.left = pipe.x + 'px';

      doPipe.style.left = pipe.x + 'px';

      if (pipe.x < -52) { //管道移出最左侧时回到原位,实现不间断效果

        pipe.x = 800;

      }

      //上下管道临界值

      var uCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y < pipe.upHeight;

      var dCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y > pipe.upHeight + 200;

      if (uCheck || dCheck) { //碰到上管道或下管道临界值则游戏终止

        runing = false;

      }

    }

  }, 30)

}

creatPipe(400); //产生四组管道

creatPipe(600);

creatPipe(800);

creatPipe(1000);