Pure javascript imitating WeChat airplane playing games _ javascript skills

Source: Internet
Author: User
Pure JavaScript simulates aircraft-hitting games, and draws on web games. The interface design is a portrait-like mobile phone screen style, with smooth gaming effect, through this article, I will share with you how to simulate airplane games in pure js. For more information, see Chinese Valentine's Day ~, The following small series of Valentine's Day gifts, pure javascript, mimic the air CAPTCHA games, share with those in the world.

First, we will show you:

View demo source code download

Pure JavaScript simulates aircraft-hitting games, and draws on web games. The interface design is a portrait-like mobile phone screen style, with smooth gaming effect. With score statistics, the JS encapsulation class includes creating aircraft, aircraft movement behavior control, creating bullet classes, and generating random numbers between min and max, determine whether the plane is removed from the boundary. If the plane is removed from the boundary, the mousemove event will be canceled. Otherwise, the mousemove event will be added to add a pause event for the continue button on the pause interface, create an enemy aircraft class, collision judgment, and complete interface initialization. One enemy plane is a small plane, and one is ours.

// Obtain the main interface

The Code is as follows:


Var mainDiv = document. getElementById ("mainp ");


// Obtain the start Interface

The Code is as follows:


Var startp = document. getElementById ("startp ");

// Obtain the score display page in the game

The Code is as follows:


Var scorep = document. getElementById ("scorep ");


// Score acquisition page

The Code is as follows:


Var scorelabel = document. getElementById ("label ");


// Obtain the pause page

The Code is as follows:


Var suspendp = document. getElementById ("suspendp ");


// Obtain the game end interface

The Code is as follows:


Var endp = document. getElementById ("endp ");


// Obtain the score statistics page after the game ends

The Code is as follows:


Var planscore = document. getElementById ("planscore ");


// Initialize the score

Var scores =;/* Create aircraft class */function plan (hp, X, Y, sizeX, sizeY, score, dietime, sudu, boomimage, imagesrc) {this. planX = X; this. planY = Y; this. imagenode = null; this. planhp = hp; this. planscore = score; this. plansizeX = sizeX; this. plansizeY = sizeY; this. planboomimage = boomimage; this. planisdie = false; this. plandietimes =; this. plandietime = dietime; this. plansudu = sudu; // behavior/* Mobile Behavior */this. planmove = function () {if (scores <=) {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu + "px";} else if (scores> & scores <=) {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu ++ "px";} else if (scores> & scores <=) {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu ++ "px";} else if (scores> & scores <=) {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu ++ "px";} else if (scores> & scores <=) {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu ++ "px";} else {this. imagenode. style. top = this. imagenode. offsetTop + this. plansudu ++ "px" ;}} this. init = function () {this. imagenode = document. element ("img"); this. imagenode. style. left = this. planX + "px"; this. imagenode. style. top = this. planY + "px"; this. imagenode. src = imagesrc; mainDiv. appendChild (this. imagenode);} this. init ();}/* Create a bullet class */function bullet (X, Y, sizeX, sizeY, imagesrc) {this. bulletX = X; this. bulletY = Y; this. bulletimage = null; this. bulletattach =; this. bulletsizeX = sizeX; this. bulletsizeY = sizeY; // behavior/* Mobile Behavior */this. bulletmove = function () {this. bulletimage. style. top = this. bulletimage. offsetTop-+ "px";} this. init = function () {this. bulletimage = document. element ("img"); this. bulletimage. style. left = this. bulletX + "px"; this. bulletimage. style. top = this. bulletY + "px"; this. bulletimage. src = imagesrc; mainDiv. appendChild (this. bulletimage);} this. init ();}/* create a single row bullet class */function oddbullet (X, Y) {bullet. call (this, X, Y, "image/bullet.png");}/* Create an enemy class */function enemy (hp, a, B, sizeX, sizeY, score, dietime, sudu, boomimage, imagesrc) {plan. call (this, hp, random (a, B),-, sizeX, sizeY, score, dietime, sudu, boomimage, imagesrc );} // generate the random number function random (min, max) {return Math between min and max. floor (min + Math. random () * (max-min);}/* Create local aircraft class */function ourplan (X, Y) {var imagesrc = "image/my airplane .gif"; plan. call (this, X, Y, "image/local aircraft explosion .gif", imagesrc); this. imagenode. setAttribute ('id', 'ourplan');}/* Create local aircraft */var selfplan = new ourplan (,); // move event var ourplan = document. getElementById ('ourplan'); var yidong = function () {var oevent = window. event | arguments []; var chufa = oevent. srcElement | oevent.tar get; var selfplanX = oevent. clientX-; var selfplanY = oevent. clientY; ourPlan. style. left = selfplanX-selfplan.plansizeX/+ "px"; ourPlan. style. top = selfplanY-selfplan.plansizeY/+ "px"; // document. getElementsByTagName ('img ') []. style. left = selfplanX-selfplan.plansizeX/+ "px"; // document. getElementsByTagName ('img ') [] .. style. top = selfplanY-selfplan.plansizeY/+ "px";}/* pause event */var number =; var zanting = function () {if (number =) {suspendp. style. display = "block"; if (document. removeEventListener) {mainDiv. removeEventListener ("mousemove", yidong, true); bodyobj. removeEventListener ("mousemove", bianjie, true);} else if (document. detachEvent) {mainDiv. detachEvent ("onmousemove", yidong); bodyobj. detachEvent ("onmousemove", bianjie) ;}clearinterval (set); number =;} else {suspendp. style. display = "none"; if (document. addEventListener) {mainDiv. addEventListener ("mousemove", yidong, true); bodyobj. addEventListener ("mousemove", bianjie, true);} else if (document. attachEvent) {mainDiv. attachEvent ("onmousemove", yidong); bodyobj. attachEvent ("onmousemove", bianjie) ;}set = setInterval (start,); number =;}// determines whether the plane has been removed from the boundary. If the plane has been removed from the boundary, cancel the mousemove event. Otherwise, add the mousemove event var bianjie = function () {var oevent = window. event | arguments []; var bodyobjX = oevent. clientX; var bodyobjY = oevent. clientY; if (bodyobjX <| bodyobjX> | bodyobjY <| bodyobjY>) {if (document. removeEventListener) {mainDiv. removeEventListener ("mousemove", yidong, true);} else if (document. detachEvent) {mainDiv. detachEvent ("onmousemove", yidong) ;}} else {if (document. addEventListener) {mainDiv. addEventListener ("mousemove", yidong, true);} else if (document. attachEvent) {mainDiv. attachEvent ("nomousemove", yidong );}}}

// Pause the interface and restart the event.

// Function chongxinkaishi () {// location. reload (true); // startp. style. display = "none"; // mainp. style. display = "block"; //} var bodyobj = document. getElementsByTagName ("body") []; if (document. addEventListener) {// Add the mainDiv for the plane to move and pause. addEventListener ("mousemove", yidong, true); // Add a pause event selfplan for this plane. imagenode. addEventListener ("click", zanting, true); // Add a bodyobj for the body to determine whether the plane is removed from the bodyobj. addEventListener ("mousemove", bianjie, true); // Add the pause event suspendp for the continue button on the pause page. getElementsByTagName ("button") []. addEventListener ("click", zanting, true); // suspendp. getElementsByTagName ("button") []. addEventListener ("click", chongxinkaishi, true); // adds the suspendp event to the homepage button of the paused interface. getElementsByTagName ("button") []. addEventListener ("click", jixu, true);} else if (document. attachEvent) {// Add a mobile mainDiv for the plane. attachEvent ("onmousemove", yidong); // Add a pause event selfplan for this plane. imagenode. attachEvent ("onclick", zanting); // Add a bodyobj for the body to determine whether the plane is removed from the bodyobj. attachEvent ("onmousemove", bianjie); // Add the pause event suspendp to the continue button on the pause page. getElementsByTagName ("button") []. attachEvent ("onclick", zanting); // suspendp. getElementsByTagName ("button") []. attachEvent ("click", chongxinkaishi, true); // adds the suspendp event to the homepage button of the paused interface. getElementsByTagName ("button") []. attachEvent ("click", jixu, true);} // initialize the selfplan to hide the local aircraft. imagenode. style. display = "none";/* array of enemy objects */var enemys = [];/* array of bullet objects */var bullets = []; var mark =; var backgroundPositionY =;/* start function */function start () {mainDiv. style. backgroundPositionY = backgroundPositionY + "px"; backgroundPositionY + = .; if (backgroundPositionY =) {backgroundPositionY =;} mark ++;/* Create an enemy plane */if (mark =) {mark ++; // if (mark % =) {enemys. push (new enemy (, random (,), "image/ .gif", "image/enemy_fly_.png "));} // large aircraft if (mark =) {enemys. push (new enemy (, "image/ .gif", "image/enemy_fly_.png"); mark =;} // airplane else {enemys. push (new enemy (, random (,), "image/fliggy .gif", "image/enemy_fly_.png");} mark = ;} /* Move enemy planes */var enemyslen = enemys. length; for (var I =; I
 
  
) {MainDiv. removeChild (enemys [I]. imagenode); enemys. splice (I,); enemyslen --;} // after a period of time, if (enemys [I]. planisdie = true) {enemys [I]. plandietimes + =; if (enemys [I]. plandietimes = enemys [I]. plandietime) {mainDiv. removeChild (enemys [I]. imagenode); enemys. splice (I,); enemyslen -- ;}}/* Create a bullet */if (mark % =) {bullets. push (new oddbullet (parseInt (selfplan. imagenode. style. left) +, parseInt (selfplan. imagenode. style. top)-);}/* Mobile bullet */var bulletslen = bullets. length; for (var I =; I
  
   
= Selfplan. imagenode. offsetLeft & enemys [j]. imagenode. offsetLeft <= selfplan. imagenode. offsetLeft + selfplan. plansizeX) {if (enemys [j]. imagenode. offsetTop + enemys [j]. plansizeY> = selfplan. imagenode. offsetTop + & enemys [j]. imagenode. offsetTop <= selfplan. imagenode. offsetTop-+ selfplan. plansizeY) {// hit the local plane. The game is over and the score is selfplan. imagenode. src = "image/local aircraft explosion .gif"; endp. style. display = "block"; planscore. innerHTML = scores; if (document. removeEventListener) {mainDiv. removeEventListener ("mousemove", yidong, true); bodyobj. removeEventListener ("mousemove", bianjie, true);} else if (document. detachEvent ){
  
 

The above code is simple. The pure javascript is used to simulate the airplane hitting game. There are many other methods at that time. You are welcome to share them with us.

Related Article

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.