A simple game with native JS implementation----Flappybird

Source: Internet
Author: User

This is a particularly simple with native JS implementation of a bird game, relatively simple, suitable for beginners to practice

This is the HTML structure

<! DOCTYPE html>

<meta charset= "UTF-8" >
<title></title>

<body>

<div id= "Game" >
<div id= "Bird" ></div>
</div>
<script src= "Flappybird.js" ></script>
<script src= "Animate.js" ></script>
</body>

The entire case structure is not many, the following is the CSS style

body {            margin:0;            padding:0;        }        #game {            width:800px;            height:600px;            border:1px solid #000;            Background:url (images/sky.png);            Overflow:hidden;            position:relative;        }        #game. PipeD {            background:url (images/pipe1.png) top center;            Position:absolute;        }        #game. Pipeu {            background:url (images/pipe2.png) bottom Center;            Position:absolute;        }        #bird {            width:34px;            height:25px;            /*border-radius:10px;*/            /*background-color:red;*/            position:absolute;            top:100px;            left:100px;            Background:url (images/birds.png) -8px-10px no-repeat;        }

The following is the native JS code, this small case also used their own early package of a small animation method

function animate (obj, JSON, fn) {clearinterval (Obj.timer);        Obj.timer = setinterval (function () {var flag = true;                For (var K-in JSON) {if (k = = = "Opacity") {var leader = GetStyle (obj, k) * 100;                var target = json[k] * 100;                var step = (Target-leader)/10; Step = step > 0?                Math.ceil (STEP): Math.floor (step);                Leader = leader + step;            Obj.style[k] = leader/100;            } else if (k = = = "ZIndex") {obj.style.zIndex = json[k];                } else {var leader = parseint (GetStyle (obj, k)) | | 0;                var target = json[k];                var step = (Target-leader)/10; Step = step > 0?                Math.ceil (STEP): Math.floor (step);                Leader = leader + step;            Obj.style[k] = leader + "px";            } if (leader!== target) {flag = false;   }        }     if (flag) {clearinterval (Obj.timer);            if (FN) {fn (); }}}, 15);}    function GetStyle (obj, attr) {if (window.getComputedStyle) {return window.getComputedStyle (obj) [attr];    } else {return obj.currentstyle[attr]; }}

Here is the JS code to control the game

var birdelement = document.getElementById ("bird"); var game = document.getElementById ("game"); var gameover = False;var g = 1;var i = 0;var Timer=null;var bird = {x:birdelement.offsetleft, y:birdelement.offsettop, Speedx:5, speed y:0, Entity:birdelement};var sky = {X:0};//var Timer=setinterval (function () {//BirdElement.style.backgroundPo    sitionx=-52*i+ "px";//i++;//if (i===3) {//i=0;//}//},100); SetInterval (function () {//] run the code at the end of the game        if (!gameover) {//entire game background X-axis movement distance sky.x = Sky.x-bird.speedx;        Game.style.backgroundPositionX = sky.x + "px";        The y-axis coordinates bird.speedy = Bird.speedy + G when the bird falls;        Set a variable to receive the y-axis coordinates when the bird drops, to set the speed at which the bird drops var step = bird.speedy;        BIRD.Y = Bird.y + step;        Use a variable to set the minimum height of the bird's whereabouts to determine if the game is over var overY = game.offsetheight-birdelement.offsetheight;            The bird's y-axis coordinates are greater than the minimum height, so the game stops if (Bird.y > OverY) {bird.y = OverY;        Stop ();The y-axis coordinates of the bird are less than 0, indicating that the top border is encountered, so the game ends if (Bird.y < 0) {bird.y = 0;        Stop ();    }//Set the location where the bird appears when the game starts bird.entity.style.top = bird.y + "px";    }}, 25);//Add keyboard event, implement keyboard up and down key control bird Document.onkeyup = function (e) {if (E.keycode = = =) {Bird.speedy =-10;    }}function pipe (Positonx) {//pipe coordinates this.x = Positonx;    This.uppipey = 0;    This.width = 52;    This.uppipeh = parseint (Math.random () * 175 + 100);    This.downpipey = This.uppipeh + 200;    This.downpipeh = Game.offsetheight-this.downpipey;    Dynamic Add pipe var divup = document.createelement ("div");    Divup.classname = "Pipeu";    DivUp.style.width = this.width + "px";    DivUp.style.height = This.uppipeh + "px";    DivUp.style.left = this.x + "px";    DivUp.style.top = This.uppipey + "px";    Game.appendchild (DIVUP);    var divdown = document.createelement ("div");    Divdown.classname = "PipeD";    DivDown.style.width = this.width + "px"; DivDown.style.height = THIS.DOwnpipeh + "px";    DivDown.style.left = this.x + "px";    DivDown.style.top = This.downpipey + "px";    Game.appendchild (Divdown);    Because the timer is confusing the point of this, so the point of this is saved in advance, and here the this point refers to the instance of the method called Var.        Set the timer to move the tube backwards this.timer=setinterval (function () {that.x = that.x-1;        Simple implementation of seamless pipe rolling if (That.x < -52) {that.x = 800;            } if (!gameover) {divUp.style.left = that.x + "px";        DivDown.style.left = that.x + "px"; }//Set variables, perform game collision detection, and stop game var Downcrash = (bird.x + > That.x) && (Bird.x < That.x +) &amp        ;& (Bird.y + > That.downpipey);        var Upcrash = (bird.x + > That.x) && (Bird.x < That.x +) && (Bird.y < That.uppipeh);            if (Downcrash | | upcrash) {//gameover = true;        Stop (); }}, 10);} Execute the above function method var arr=[];for (var i = 0; i < 4; i++) {arr[i]=new Pipe (i * 200 + 400);} Encapsulate a way to stop the game, FuNction Stop () {gameover=true;    Clearinterval (timer);    for (Var i=0;i<arr.length;i++) {clearinterval (Arr[i].timer); }}

The notes are written in the code, and a simple little game is done.

  

A simple game with native JS implementation----Flappybird

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.