最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現飛機大戰(zhàn)小游戲

 更新時間:2020年08月26日 10:58:30   作者:張張張的博客  
這篇文章主要為大家詳細介紹了js實現飛機大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js實現飛機大戰(zhàn)游戲的具體代碼,供大家參考,具體內容如下

1.html代碼

<html>
<head>
<title></title>
<meta http-equiv="content" content="text/html" charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>
 
<body>
<div id="contentdiv">
<div id="startdiv">
<button οnclick="begin()">開始游戲</button>
</div>
<div id="maindiv">
<div id="scorediv">
<label>分數:</label>
<label id="label">0</label>
</div>
<div id="suspenddiv">
<button>繼續(xù)</button><br/>
<button>重新開始</button><br/>
<button>回到主頁</button>
</div>
<div id="enddiv">
<p>飛機大戰(zhàn)分數</p>
<p id="planscore">0</p>
<div><button οnclick="jixu()">繼續(xù)</button></div>
</div>
</div>
</div>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

2.js主要代碼

//獲得主界面
var mainDiv=document.getElementById("maindiv");
//獲得開始界面
var startdiv=document.getElementById("startdiv");
//獲得游戲中分數顯示界面
var scorediv=document.getElementById("scorediv");
//獲得分數界面
var scorelabel=document.getElementById("label");
//獲得暫停界面
var suspenddiv=document.getElementById("suspenddiv");
//獲得游戲結束界面
var enddiv=document.getElementById("enddiv");
//獲得游戲結束后分數統計界面
var planscore=document.getElementById("planscore");
//初始化分數
var scores=0;
 
/*
創(chuàng)建飛機類
*/
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=0;
this.plandietime=dietime;
this.plansudu=sudu;
//行為
/*
移動行為
*/
this.planmove=function(){
if(scores<=50000){
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+"px";
}
else if(scores>50000&&scores<=100000){
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+1+"px";
}
else if(scores>100000&&scores<=150000){
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+2+"px";
}
else if(scores>150000&&scores<=200000){
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+3+"px";
}
else if(scores>200000&&scores<=300000){
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+4+"px";
}
else{
this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+5+"px";
}
}
this.init=function(){
this.imagenode=document.createElement("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();
}
 
/*
創(chuàng)建子彈類
*/
function bullet(X,Y,sizeX,sizeY,imagesrc){
this.bulletX=X;
this.bulletY=Y;
this.bulletimage=null;
this.bulletattach=1;
this.bulletsizeX=sizeX;
this.bulletsizeY=sizeY;
//行為
/*
移動行為
*/
this.bulletmove=function(){
this.bulletimage.style.top=this.bulletimage.offsetTop-20+"px";
}
this.init=function(){
this.bulletimage=document.createElement("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();
}
 
/*
創(chuàng)建單行子彈類
*/
function oddbullet(X,Y){
bullet.call(this,X,Y,6,14,"image/bullet1.png");
}
 
/*
創(chuàng)建敵機類
*/
function enemy(hp,a,b,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc){
plan.call(this,hp,random(a,b),-100,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc);
}
//產生min到max之間的隨機數
function random(min,max){
return Math.floor(min+Math.random()*(max-min));
}
 
/*
創(chuàng)建本方飛機類
*/
function ourplan(X,Y){
var imagesrc="image/我的飛機.gif";
plan.call(this,1,X,Y,66,80,0,660,0,"image/本方飛機爆炸.gif",imagesrc);
this.imagenode.setAttribute('id','ourplan');
}
 
/*
創(chuàng)建本方飛機
*/
var selfplan=new ourplan(120,485);
//移動事件
var ourPlan=document.getElementById('ourplan');
var yidong=function(){
var oevent=window.event||arguments[0];
var chufa=oevent.srcElement||oevent.target;
var selfplanX=oevent.clientX-500;
var selfplanY=oevent.clientY;
ourPlan.style.left=selfplanX-selfplan.plansizeX/2+"px";
ourPlan.style.top=selfplanY-selfplan.plansizeY/2+"px";
// document.getElementsByTagName('img')[0].style.left=selfplanX-selfplan.plansizeX/2+"px";
// document.getElementsByTagName('img')[0]..style.top=selfplanY-selfplan.plansizeY/2+"px";
}
/*
暫停事件
*/
var number=0;
var zanting=function(){
if(number==0){
suspenddiv.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=1;
}
else{
suspenddiv.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,20);
number=0;
}
}
//判斷本方飛機是否移出邊界,如果移出邊界,則取消mousemove事件,反之加上mousemove事件
var bianjie=function(){
var oevent=window.event||arguments[0];
var bodyobjX=oevent.clientX;
var bodyobjY=oevent.clientY;
if(bodyobjX<505||bodyobjX>815||bodyobjY<0||bodyobjY>568){
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);
}
}
}
//暫停界面重新開始事件
//function chongxinkaishi(){
// location.reload(true);
// startdiv.style.display="none";
// maindiv.style.display="block";
//}
var bodyobj=document.getElementsByTagName("body")[0];
if(document.addEventListener){
//為本方飛機添加移動和暫停
mainDiv.addEventListener("mousemove",yidong,true);
//為本方飛機添加暫停事件
selfplan.imagenode.addEventListener("click",zanting,true);
//為body添加判斷本方飛機移出邊界事件
bodyobj.addEventListener("mousemove",bianjie,true);
//為暫停界面的繼續(xù)按鈕添加暫停事件
suspenddiv.getElementsByTagName("button")[0].addEventListener("click",zanting,true);
// suspenddiv.getElementsByTagName("button")[1].addEventListener("click",chongxinkaishi,true);
//為暫停界面的返回主頁按鈕添加事件
suspenddiv.getElementsByTagName("button")[2].addEventListener("click",jixu,true);
}
else if(document.attachEvent){
//為本方飛機添加移動
mainDiv.attachEvent("onmousemove",yidong);
//為本方飛機添加暫停事件
selfplan.imagenode.attachEvent("onclick",zanting);
//為body添加判斷本方飛機移出邊界事件
bodyobj.attachEvent("onmousemove",bianjie);
//為暫停界面的繼續(xù)按鈕添加暫停事件
suspenddiv.getElementsByTagName("button")[0].attachEvent("onclick",zanting);
// suspenddiv.getElementsByTagName("button")[1].attachEvent("click",chongxinkaishi,true);
//為暫停界面的返回主頁按鈕添加事件
suspenddiv.getElementsByTagName("button")[2].attachEvent("click",jixu,true);
}
//初始化隱藏本方飛機
selfplan.imagenode.style.display="none";
 
/*
敵機對象數組
*/
var enemys=[];
 
/*
子彈對象數組
*/
var bullets=[];
var mark=0;
var mark1=0;
var backgroundPositionY=0;
/*
開始函數
*/
function start(){
mainDiv.style.backgroundPositionY=backgroundPositionY+"px";
backgroundPositionY+=0.5;
if(backgroundPositionY==568){
backgroundPositionY=0;
}
mark++;
/*
創(chuàng)建敵方飛機
*/
 
if(mark==20){
mark1++;
//中飛機
if(mark1%5==0){
enemys.push(new enemy(6,25,274,46,60,5000,360,random(1,3),"image/中飛機爆炸.gif","image/enemy3_fly_1.png"));
}
//大飛機
if(mark1==20){
enemys.push(new enemy(12,57,210,110,164,30000,540,1,"image/大飛機爆炸.gif","image/enemy2_fly_1.png"));
mark1=0;
}
//小飛機
else{
enemys.push(new enemy(1,19,286,34,24,1000,360,random(1,4),"image/小飛機爆炸.gif","image/enemy1_fly_1.png"));
}
mark=0;
}
 
/*
移動敵方飛機
*/
var enemyslen=enemys.length;
for(var i=0;i<enemyslen;i++){
if(enemys[i].planisdie!=true){
enemys[i].planmove();
}
/*
如果敵機超出邊界,刪除敵機
*/
if(enemys[i].imagenode.offsetTop>568){
mainDiv.removeChild(enemys[i].imagenode);
enemys.splice(i,1);
enemyslen--;
}
//當敵機死亡標記為true時,經過一段時間后清除敵機
if(enemys[i].planisdie==true){
enemys[i].plandietimes+=20;
if(enemys[i].plandietimes==enemys[i].plandietime){
mainDiv.removeChild(enemys[i].imagenode);
enemys.splice(i,1);
enemyslen--;
}
}
}
 
/*
創(chuàng)建子彈
*/
if(mark%5==0){
bullets.push(new oddbullet(parseInt(selfplan.imagenode.style.left)+31,parseInt(selfplan.imagenode.style.top)-10));
}
 
/*
移動子彈
*/
var bulletslen=bullets.length;
for(var i=0;i<bulletslen;i++){
bullets[i].bulletmove();
/*
如果子彈超出邊界,刪除子彈
*/
if(bullets[i].bulletimage.offsetTop<0){
mainDiv.removeChild(bullets[i].bulletimage);
bullets.splice(i,1);
bulletslen--;
}
}
 
/*
碰撞判斷
*/
for(var k=0;k<bulletslen;k++){
for(var j=0;j<enemyslen;j++){
//判斷碰撞本方飛機
if(enemys[j].planisdie==false){
if(enemys[j].imagenode.offsetLeft+enemys[j].plansizeX>=selfplan.imagenode.offsetLeft&&enemys[j].imagenode.offsetLeft<=selfplan.imagenode.offsetLeft+selfplan.plansizeX){
if(enemys[j].imagenode.offsetTop+enemys[j].plansizeY>=selfplan.imagenode.offsetTop+40&&enemys[j].imagenode.offsetTop<=selfplan.imagenode.offsetTop-20+selfplan.plansizeY){
//碰撞本方飛機,游戲結束,統計分數
selfplan.imagenode.src="image/本方飛機爆炸.gif";
enddiv.style.display="block";
planscore.innerHTML=scores;
if(document.removeEventListener){
mainDiv.removeEventListener("mousemove",yidong,true);
bodyobj.removeEventListener("mousemove",bianjie,true);
}
else if(document.detachEvent){
mainDiv.detachEvent("onmousemove",yidong);
bodyobj.removeEventListener("mousemove",bianjie,true);
}
clearInterval(set);
}
}
//判斷子彈與敵機碰撞
if((bullets[k].bulletimage.offsetLeft+bullets[k].bulletsizeX>enemys[j].imagenode.offsetLeft)&&(bullets[k].bulletimage.offsetLeft<enemys[j].imagenode.offsetLeft+enemys[j].plansizeX)){
if(bullets[k].bulletimage.offsetTop<=enemys[j].imagenode.offsetTop+enemys[j].plansizeY&&bullets[k].bulletimage.offsetTop+bullets[k].bulletsizeY>=enemys[j].imagenode.offsetTop){
//敵機血量減子彈攻擊力
enemys[j].planhp=enemys[j].planhp-bullets[k].bulletattach;
//敵機血量為0,敵機圖片換為爆炸圖片,死亡標記為true,計分
if(enemys[j].planhp==0){
scores=scores+enemys[j].planscore;
scorelabel.innerHTML=scores;
enemys[j].imagenode.src=enemys[j].planboomimage;
enemys[j].planisdie=true;
}
//刪除子彈
mainDiv.removeChild(bullets[k].bulletimage);
bullets.splice(k,1);
bulletslen--;
break;
}
}
}
}
}
}
/*
開始游戲按鈕點擊事件
*/
var set;
function begin(){
 
startdiv.style.display="none";
mainDiv.style.display="block";
selfplan.imagenode.style.display="block";
scorediv.style.display="block";
/*
調用開始函數
*/
set=setInterval(start,20);
}
//游戲結束后點擊繼續(xù)按鈕事件
function jixu(){
location.reload(true);
}
 
/*
完成界面的初始化
敵方小飛機一個
我方飛機一個
*/

3.css樣式

*{
margin: 0;
padding: 0;
}
#contentdiv{
position: absolute;
left: 500px;
}
#startdiv{
width: 320px;
height: 568px;
background-image: url(../image/開始背景.png);
}
button{
outline: none;
}
#startdiv button{
position: absolute;
top: 500px;
left: 82px;
width: 150px;
height: 30px;
border: 1px solid black;
border-radius: 30px;
background-color: #c4c9ca;
}
#maindiv{
width: 320px;
height: 568px;
background-image:url(../image/background_1.png) ;
display: none;
}
#maindiv img{
position: absolute;
z-index: 1;
}
#scorediv{
font-size: 16px;
font-weight: bold;
position: absolute;
top: 10px;
left: 10px;
display: none;
}
#scorediv{
font-size: 18px;
font-weight: bold;
}
#suspenddiv{
position: absolute;
top: 210px;
left: 82px;
display: none;
z-index: 2;
}
#suspenddiv button{
width: 150px;
height: 30px;
margin-bottom: 20px;
border: 1px solid black;
border-radius: 30px;
background-color: #c4c9ca;
}
#enddiv{
position: absolute;
top: 210px;
left: 75px;
border: 1px solid gray;
border-radius: 5px;
text-align: center;
background-color:#d7ddde;
display: none;
z-index: 2;
}
.plantext{
width: 160px;
height: 30px;
line-height: 30px;
font-size: 16px;
font-weight: bold;
}
#planscore{
width: 160px;
height: 80px;
line-height: 80px;
border-top: 1px solid gray;
border-bottom: 1px solid gray;
font-size: 16px;
font-weight: bold;
}
#enddiv div{
width: 160px;
height: 50px;
}
#enddiv div button{
margin-top:10px ;
width: 90px;
height: 30px;
border: 1px solid gray;
border-radius: 30px;
}

更多有趣的經典小游戲實現專題,分享給大家:

C++經典小游戲匯總

python經典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經典游戲 玩不停

java經典小游戲匯總

javascript經典小游戲匯總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 微信小程序網絡請求模塊封裝的具體實現

    微信小程序網絡請求模塊封裝的具體實現

    大家做小程序項目的時候肯定會遇到數據對接需求,下面這篇文章主要給大家介紹了關于微信小程序網絡請求模塊封裝的具體實現,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-03-03
  • 如何屏蔽防止別的網站嵌入框架代碼

    如何屏蔽防止別的網站嵌入框架代碼

    目前,國內很流行一種流氓行為:使用框架(Frame),將你的網頁嵌入它的網頁中,如何防止嵌入框架代碼呢,下面小編給大家整理了相關資料,如何屏蔽防止別的網站嵌入框架代碼,需要的朋友一起來學習下
    2015-08-08
  • 詳解A標簽中href=

    詳解A標簽中href=""的幾種用法

    這篇文章主要介紹了a標簽中href=""的幾種用法 ,需要的朋友可以參考下
    2017-08-08
  • laypage+SpringMVC實現后端分頁

    laypage+SpringMVC實現后端分頁

    這篇文章主要為大家詳細介紹了laypage+SpringMVC實現后端分頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 淺談javascript中遇到的字符串對象處理

    淺談javascript中遇到的字符串對象處理

    下面小編就為大家?guī)硪黄獪\談javascript中遇到的字符串對象處理。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • JS二叉樹的簡單實現方法示例

    JS二叉樹的簡單實現方法示例

    這篇文章主要介紹了JS二叉樹的簡單實現方法,結合具體實例形式分析了基于javascript定義二叉樹及二叉樹節(jié)點的遍歷、查找、添加、刪除及運算相關操作技巧,需要的朋友可以參考下
    2017-04-04
  • CheckBox多選取值及判斷CheckBox選中是否為空的實例

    CheckBox多選取值及判斷CheckBox選中是否為空的實例

    下面小編就為大家?guī)硪黄狢heckBox多選取值及判斷CheckBox選中是否為空的實例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • javascript匿名函數實例分析

    javascript匿名函數實例分析

    這篇文章主要介紹了javascript匿名函數,以實例形式詳細分析了javascript匿名函數的定義、用法及注意事項,具有很好的學習借鑒價值,需要的朋友可以參考下
    2014-11-11
  • javascript+HTML5自定義元素播放焦點圖動畫

    javascript+HTML5自定義元素播放焦點圖動畫

    這篇文章主要介紹了javascript+HTML5自定義元素播放焦點圖動畫的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Next.js服務端渲染超時導致生產事故的問題排查和解決辦法

    Next.js服務端渲染超時導致生產事故的問題排查和解決辦法

    Next.js是一個基于React的開源框架,用于構建服務端渲染(SSR)的Web應用程序,它簡化了許多復雜的配置,讓開發(fā)者可以專注于編寫應用邏輯而不是基礎設施,前幾天公司平臺首頁崩潰了 504 Time-out,這里記錄一下問題排查和解決的過程,需要的朋友可以參考下
    2025-03-03

最新評論

潞西市| 岳阳县| 佛冈县| 澳门| 柳州市| 安图县| 鸡泽县| 石阡县| 绥芬河市| 宝山区| 靖江市| 高淳县| 区。| 军事| 福贡县| 安新县| 浪卡子县| 田东县| 嘉祥县| 壶关县| 苏尼特左旗| 融水| 确山县| 金山区| 大田县| 获嘉县| 务川| 重庆市| 曲阳县| 南涧| 邵阳市| 沙湾县| 石台县| 阿尔山市| 精河县| 鄂托克前旗| 阳朔县| 苍山县| 鸡东县| 库尔勒市| 葫芦岛市|