Overall display:
The previous talk of implementing a lot of objects, this time we need to implement a number of logical methods, such as control aircraft movement, judge the bullet hit the enemy aircraft, enemy planes collided with heroes and so on. And we need timers to invoke these methods when we implement these functions. SetInterval (FUN,MS)
Calling SetInterval () returns a field similar to ID, which can specify the corresponding timer in Clearinterval (ID) and clear
First, start the game
/** * Panel 441*632 * Aircraft 66*80*//** * Start the game*/functionStartgame () {varWelcome = document.getElementById ("Welcome"); Myplane=NewCreateplane ("Img/own.png", 190, 550, 5, 5); Welcome.style.visibility= "hidden";//will enter the Welcome screen to hide//Ctrlmove ();/** Start Timer*/Ctrlmove= SetInterval (ctrlmove,20); Ctrlshot= SetInterval (ctrlshot,200); Bulletfly= SetInterval (bulletfly,20); Createenemy= SetInterval (enemy,200); Enemymove= SetInterval (enemymove,20); Hitplane= SetInterval (hitplane,30); exist= SetInterval (isexist,20);}
Second, control the Hero aircraft movement
/** Keyboard monitoring, used to judge the action of the Hero Wasd movement direction, J shot, K must kill * placed in the beginning game method*/Document.body.onkeydown=function(code) {if(Code.keycode = = 65) {leftbtn=true; } if(Code.keycode = = 68) {rightbtn=true; } if(Code.keycode = = 87) {topbtn=true; } if(Code.keycode = = 83) {bottombtn=true; } if(Code.keycode = = 74) {shot=true; } if(Code.keycode = = 75) {Shotboom=true; }} Document.body.onkeyup=function(code) {if(code.keycode==65) {leftbtn=false; } if(Code.keycode = = 68) {rightbtn=false; } if(Code.keycode = = 87) {topbtn=false; } if(Code.keycode = = 83) {bottombtn=false; } if(Code.keycode = = 74) {shot=false; } if(Code.keycode = = 75) {Shotboom=false; } }
/** * Control the function of hero movement*/functionCtrlmove () {if(leftbtn==true) {myplane.leftmove ()}if(rightbtn==true) {myplane.rightmove ()}if(topbtn==true) {myplane.topmove ()}if(bottombtn==true) {myplane.botoommove ()}//Get player ParametersgetInfo ();}
Third, the bullet hit the object
/** * Bullets hit the object **/functionHitplane () {/** Hero bullet hits enemy plane*/ for(i=0;i<bulletlist.length;i++) {//Traversal Hero Bullet Collection for(j=0;j<enemylist.length;j++) {//traverse the Enemy collection //get bullet coordinates and enemy coordinates . varBtop =parseint (bulletlist[i].bulletnode.style.top); varBleft =parseint (Bulletlist[i].bulletnode.style.left); varEtop =parseint (enemylist[j].enemynode.style.top); varEleft =parseint (Enemylist[j].enemynode.style.left); if(!enemylist[j].isdead) { if(bleft>=eleft-5&&bleft<eleft+34&&btop>etop&&btop<etop+34) {Bulletlist[i].ishit=true;//hit to change the bullet status to True if(--enemylist[j].blood<=0)//Judging if the enemy is dead .Enemylist[j].isdead=true; } } } } /** Boos bullets hit a hero*/ vartop =parseint (myPlane.planeNode.style.top); varleft =parseint (MyPlane.planeNode.style.left); for(i=0;i<boosbullet.length;i++){ varBtop =parseint (boosbullet[i].bulletnode.style.top); varBleft =parseint (Boosbullet[i].bulletnode.style.left); if(myplane.blood>=0){ if(btop+55>=top&&btop<top+80&&bleft>=left-22&&bleft<left+40) {Boosbullet[i].ishit=true; Myplane.blood--; } } } /** * bullets hit boos*/ for(i=0;i<bulletlist.length;i++){ varBtop =parseint (bulletlist[i].bulletnode.style.top); varBleft =parseint (Bulletlist[i].bulletnode.style.left); for(j=0;j<booslist.length;j++){ vartop=parseint (booslist[j].enemynode.style.top); varleft=parseint (Booslist[j].enemynode.style.left); if(!booslist[j].isdead) { if(bleft-5>=left&&bleft<left+90&&btop>=top&&btop<top+80) {console.log (booslist[j].blood) Bulletlist[i].ishit=true; if(--booslist[j].blood<=0) Booslist[j].isdead=true; } } } }}
Four, collision between objects
/** * Object Collision*/functionstrike () {varMleft =parseint (MyPlane.planeNode.style.left); varMtop =parseint (myPlane.planeNode.style.top); if(myplane.blood>0){ for(i=0;i<enemylist.length;i++) {//Traverse enemy Aircraft if(!enemylist[i].isdead) {//is the enemy dead ? varEleft =parseint (Enemylist[i].enemynode.style.left); varEtop =parseint (enemylist[i].enemynode.style.top); if(eleft>=mleft&&eleft<mleft+68&&etop>=mtop&&etop<mtop+80) {//Collision JudgmentEnemylist[i].isdead =true;//the enemy plane crashed into a hero and immediately died .ENEMYLIST[I].ENEMYNODE.SRC = "Img/enemybz.png";//Toggle Pictures, create explosive effectsmyplane.blood--;//Hero Blood Volume reduction } } } } /** Get props*/ if(myplane.blood>0) {//Judging if the hero is dead for(i=0;i<toollist.length;i++) {//traversing an array of props if(!toollist[i].getme) { varTleft =parseint (Toollist[i].toolnode.style.left); varTtop =parseint (toollist[i].toolnode.style.top); if(tleft>=mleft-15&&tleft<mleft+65&&ttop>=mtop&&ttop<mtop+80) {Toollist[i].getme=true; if(toollist[i].tooltype%2==1) {//Tooltype singular is plus must kill, even more blood if(myplane.boom<5) Myplane.boom++; }Else{Myplane.blood++; } } } } }}
V. Determine whether the object is dead
/** Determine if an object exists*/functionisexist () {/** Determine if the enemy is dead*/ for(i=0;i<enemylist.length;i++){ if(Enemylist[i].isdead) {//death of enemy aircraftENEMYLIST[I].ENEMYNODE.SRC = "Img/enemybz.png";//Replace the picture with an exploded picture if(enemylist[i].deadtime>=0) {//remove time, that is, the time the aircraft died to remove, to achieve the effects of the explosionenemylist[i].deadtime--; }Else{score+=Myplane.level; Killnum++; Mainobj.removechild (Enemylist[i].enemynode); //first remove nodeEnemylist.splice (i,1);//then unbind } } } /** Determine if a hero is dead*/ if(myplane.blood<=0) {mainobj.removechild (Myplane.planenode); Gameover (); } /** Determine if boos is dead*/ for(i=0;i<booslist.length;i++){ if(booslist[i].isdead) {if(booslist[i].deadtime<=0) {mainobj.removechild (Booslist[i].enemynode) Booslist.splice (i,1); Score+=100*Myplane.level; Pass++; Nextgame (); }Else{booslist[i].deadtime--; BOOSLIST[I].ENEMYNODE.SRC= "Img/dfjbz.png"; } } } /** Determine if a prop exists*/ for(i=0;i<toollist.length;i++){ if(toollist[i].getme) {mainobj.removechild (Toollist[i].toolnode); Toollist.splice (i,1); } }}
=============================================================================================================== ================
Second, add:
When I created the object (except for the Hero plane object), I put it into the arrar in order to bind the object and find it in terms of the aspect. Easy to operate.
The above for today's content, if you need to know more in-depth knowledge, please enter the Cicada Hall community: http://www.zhiliaotang.com/portal.php;
[Cicada Hall Study notes]_ Pure JS production "aircraft War" game _ 3rd (Implementation of the Logical method)