JavaScript Aircraft Wars-----008 Points

Source: Internet
Author: User

/*Create enemy Aircraft:*/functionEnemy (Blood,speed,imgs,scroe) {//enemy aircraft left     This. left = 0; //top of enemy aircraft     This. top = 0; //enemy's blood volume     This. Blood =Blood; //Enemy aircraft Speed     This. Speed =Speed ; //Enemy Pictures Collection     This. IMGs = IMGs;//before the explosion and after the explosion    //score   This.scroe = Scroe;} Enemy.prototype={constructor:enemy, init:function(){        //Create a single element        varimg = document.createelement (' img ')); //assign a picture path to itImg.src= This. imgs[0]; //inserting into the gameEngine.game.appendChild (IMG); //initial image assigned to enemy aircraft         This. Self =img; //Get the height and width of the picture when the picture is finished loading        var_this = This;//This point in the function will change, so we'll save it in advance.Img.onload =function() {_this.left= parseint (Math.random () * (320-img.offsetwidth)); _this.top= -Img.offsetheight; Img.style.left= _this.left+ ' px '; Img.style.top= _this.top+ ' px ';        }; //generate the enemy number and put it into the bullet of the engine.         This. ID =Math.random (); engine.enemy[ This. id]= This; },    //The bullets move, the timers are given to the engines .Movefunction(){         This. top+= This. Speed;  This. Self.style.top = This. top+ ' px '; //Cross Judgment        if( This. top>568+ This. Self.offsetwidth) {             This. Destroy (); }        //judging the collision with the hero machine        if(Engine.iscompact ( This. self,hero.self)) {            //Destroy Yourself             This. Destroy (); //Hero MachineHero.die (); }}, Bang:function(){        varimg = document.createelement (' img ')); IMG.SRC= This. imgs[1]; Img.style.left= This. left+ ' px '; Img.style.top= This. top+ ' px '; Engine.game.appendChild (IMG) setTimeout (function() {img.remove (); },1000)}, Destroy:function(){        //destroyed        //from page hours         This. Self.remove ();  This. Bang (); //Statistical score       Engine.updatescroe (This.scroe); //disappear from memory        Deleteengine.enemy[ This. Id]; }}

The score per aircraft

/*create all types of aircraft*/functionSmallenemy () {vars = parseint (Math.random () *3+3); Enemy.call ( This, 1,s,[' image/enemy1.png ', ' image/enemy1-bang.gif '],10)}smallenemy.prototype={constructor:smallenemy, __proto__: Enemy.prototype};functionMiddleenemy () {vars = parseint (Math.random () *3+2); Enemy.call ( This, 5,s,[' image/enemy2.png ', ' image/enemy2-bang.gif '],20)}middleenemy.prototype={constructor:middleenemy, __proto__:enemy.prototype}functionLargeenemy () {vars = parseint (Math.random () *2+1); Enemy.call ( This, 10,s,[' image/enemy3.png ', ' image/enemy3-bang.gif '],50)}largeenemy.prototype={constructor:largeenemy, __proto__:enemy.prototype}

Update Score

/*Game engine*/varEngine = {    //the first game stateGameStatus:false,    //so the enemy .enemy:{},//Bulletsbullet:{},//scorescroe:0,    //Background ImageGame:document.querySelector ('. Game ')),    //Page Score   TextScroe:document.querySelector ('. Score '),//InitializeInit:function(){         This. Gamestart (); },    //Game StartGamestart:function(){        var_this = This; //Click on the image to determine the status of the game         This. Game.onclick =function(){            if(!_this.gamestatus) {_this.gamestatus=true; //Mobile Mobile_this.bgmove ();                _this.handlemove ();            _this.createplane (); }        }    },    //Background MoveBgmove:function(){        varY=0; var_this = This;  This. Bgtimer = SetInterval (function() {y+=2; _this.game.style[' Background-position-y ']=y+ ' px '; },50)}, Createplane:function(){        //create enemy aircraft and hero machinesHero.init (); //Create enemy aircraft        varTimer = SetInterval (function(){            varnum = parseint (math.random () *15) +1; Switch(num) { Case1:                 Case3:                 Case5:                 Case7:                 Case9:                    NewSmallenemy (). Init ();  Break;  Case2:                 Case4:                 Case6:                    NewMiddleenemy (). Init ();  Case8:                 Case12:                    NewLargeenemy (). Init (); }        },500)    },    //all the enemy planes and bullets are moving .Handlemove:function(){        var_this= This; varTimer = SetInterval (function(){            //Create all Bullets             for(varIinch_this.bullet) {_this.bullet[i].move ()}//C Create all enemy maneuvers             for(varIinch_this.enemy) {_this.enemy[i].move ()}},30)    },    //Collision DetectionIscompact:function(obj1,obj2) {varL1 = obj1.offsetleft>obj2.offsetleft+Obj2.offsetwidth; varL2 = obj2.offsetleft>obj1.offsetleft+Obj1.offsetwidth; varT1 = obj1.offsettop>obj2.offsettop+Obj2.offsetheight; varT2 = obj2.offsettop>obj1.offsettop+Obj1.offsetheight; if(l1| | l2| | t1| |T2) {            return false; }Else{            return true; }    },    //Update Score   updatescroe:function (Scroe) {This.scroe+=scroe; This.textscroe.innerhtml= "Score" +this . Scroe; }}; Engine.init ();

JavaScript Aircraft wars-----008 points

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.