




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第javascript實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲本文實(shí)例為大家分享了javascript實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
文檔結(jié)構(gòu)如下
其中tool文件中只使用了隨機(jī)數(shù),audio中是存放的音樂(lè)文件,images中是己方和敵方飛機(jī)的圖片。
HTML部分
!DOCTYPEhtml
htmllang="en"
head
metacharset="UTF-8"
metahttp-equiv="X-UA-Compatible"content="IE=edge"
metaname="viewport"content="width=device-width,initial-scale=1.0"
titleDocument/title
linkrel="stylesheet"href="css/game.css"
/head
body
section
inputtype="button"value="GAMESTART"id="btn"
divid="socre"
pid="num"當(dāng)前分?jǐn)?shù)為:/p
pid="historynum"歷史最高:/p
/div
/section
scriptsrc="js/tool.js"/script
scriptsrc="js/game.js"/script
/body
/html
CSS部分
section{
background-image:url(../material/images/startBG.png);
background-repeat:no-repeat;
background-size:320px,570px;
width:320px;
height:570px;
margin:auto;
margin-top:30px;
position:relative;
overflow:hidden;
sectioninput{
width:150px;
position:absolute;
bottom:65px;
left:85px;
#socre{
display:none;
}
JS部分
主要是通過(guò)類(lèi)方法創(chuàng)建敵機(jī)和我方飛機(jī),再通過(guò)類(lèi)的繼承給予小/中/大/boss等敵機(jī)屬性和方法。
constsection=document.querySelector("section");
constenemy=document.getElementsByClassName("enemys");
let[flag1,flag2,flag3,flag4]=[false,false,false,false];
//小飛機(jī)
letsplane;
//中飛機(jī)
letmplane;
//大飛機(jī)
letbplane;
//boss
letboss;
letshoot;
letbossshoot;
letnumber;
letmove1;
//本地獲取數(shù)據(jù)
letarr=localStorage.getItem("scort");
arr=JSON.parse(arr);
varmp3;
vargameover;
varbossrun;
//游戲開(kāi)始
btn.addEventListener("click",function(){
//console.log(gameover);
if(gameover){
gameover.pause();
}
mp3="./material/audio/bgm.mp3";
mp3=newAudio(mp3);
mp3.play();//播放mp3這個(gè)音頻對(duì)象
//計(jì)算分?jǐn)?shù)
number=0;
num.innerText=`當(dāng)前分?jǐn)?shù)為:0`;
//從本地獲取分?jǐn)?shù)
arr=localStorage.getItem("scort");
arr=JSON.parse(arr);
constnewmyplane=document.getElementById("myplane");
if(newmyplane){
section.removeChild(newmyplane)
}
//判斷本地是否有數(shù)據(jù)
if(arr==null){
historynum.innerText=`歷史最高:0`
}else{
historynum.innerText=`歷史最高:${arr}`
}
//得分面板
socre.style.display="block";
btn.style.display="none";
//更改背景
section.style.backgroundImage="url(./material/images/background_1.png)";
//實(shí)例化自身飛機(jī)
letmyplane=newMyplane(0,127);
//實(shí)例化敵機(jī)
splane=setInterval(
function(){
letsmallenemys=newSmallenemys(random(0,286),"material/images/enemy1_fly_1.png",-24,1);
},1000)
mplane=setInterval(
function(){
letmidenemys=newMidenemys(random(0,274),"material/images/enemy3_fly_1.png",-60,3);
},6000)
bplane=setInterval(
function(){
letbigenemys=newBigenemys(random(0,210),"material/images/enemy2_fly_1.png",-164,10);
},10000)
boss=setInterval(
function(){
letboss=newBossenemys(random(0,210),"material/images/boss.png",-118,20);
bossrun="./material/audio/bossrun.mp3";
bossrun=newAudio(bossrun);
bossrun.play();//播放mp3這個(gè)音頻對(duì)象
//延遲器
setTimeout(()={
bossrun.pause();
},3000)
},50000)
//己方飛機(jī)
classMyplane{
constructor(firstbot,firstleft){
this.node=document.createElement("img");
//console.log(this.node);
this.firstbot=firstbot;
this.firstleft=firstleft;
this.init();
}
init(){
this.create();
this.render();
this.action();
this.crash();
shoot=setInterval(()={
letbullet=newBullet(this.firstbot+80,this.firstleft+31);
num.innerText=`當(dāng)前分?jǐn)?shù)為:${number}`
},230)
}
render(){
Object.assign(this.node.style,{
position:`absolute`,
bottom:`${this.firstbot}px`,
left:`${this.firstleft}px`,
})
}
create(){
this.node.setAttribute('src','material/images/myPlane.gif');
this.node.setAttribute('id','myplane')
section.appendChild(this.node);
}
action(){
//鍵盤(pán)按下
document.addEventListener("keydown",(event)={
if(this.move){
this.move(event);
}
});
//鍵盤(pán)抬起
document.addEventListener("keyup",function(event){
switch(event.key){
case"w":
flag1=false;
break;
case"a":
flag2=false;
break;
case"s":
flag3=false;
break;
case"d":
flag4=false;
break;
}
})
}
//移動(dòng)
move(event){
switch(event.key){
case"w":
flag1=true;
break;
case"a":
flag2=true;
break;
case"s":
flag3=true;
break;
case"d":
flag4=true;
break;
}
if(move1){
clearInterval(move1)
}
move1=setInterval(()={
//左側(cè)邊框
if(flag2){
if(parseInt(getComputedStyle(this.node).left)=0){
this.firstleft=0;
}
this.firstleft-=2;
this.render()
}
//上側(cè)邊框
elseif(flag1){
this.firstbot+=2;
if(parseInt(getComputedStyle(this.node).bottom)=490){
this.firstbot=490;
}
this.render()
}
//右側(cè)邊框
elseif(flag4){
if(parseInt(getComputedStyle(this.node).left)=255){
this.firstleft=255;
}
this.firstleft+=2;
this.render()
}
//下側(cè)邊框
elseif(flag3){
if(parseInt(getComputedStyle(this.node).bottom)=0){
this.firstbot=0;
}
this.firstbot-=2;
this.render()
}
this.render()
},10)
}
crash(){
lettime=setInterval(()={
letbottom=parseInt(getComputedStyle(this.node).bottom);
letleft=parseInt(getComputedStyle(this.node).left);
for(letitemofenemy){
//碰撞判斷
if(bottom=parseInt(getComputedStyle(item).bottom)+parseInt(getComputedStyle(item).height)
bottom=parseInt(getComputedStyle(item).bottom)-parseInt(getComputedStyle(this.node).height)
left=parseInt(getComputedStyle(item).left)-parseInt(getComputedStyle(this.node).width)
left=parseInt(getComputedStyle(item).left)+parseInt(getComputedStyle(item).width)){
this.node.setAttribute('src','material/images/本方飛機(jī)爆炸.gif');
this.move=null;
//游戲結(jié)束時(shí)清除除自身外飛機(jī)
for(letitem1ofenemy){
item1.style.display='none';
}
clearInterval(bossshoot);
clearInterval(time);
clearInterval(splane);
clearInterval(mplane);
clearInterval(bplane);
clearInterval(shoot);
clearInterval(boss);
mp3.pause();
gameover="./material/audio/gameover.mp3";
gameover=newAudio(gameover);
gameover.play();//播放mp3這個(gè)音頻對(duì)象
if(arrnumber){
localStorage.setItem('scort',JSON.stringify(number));
historynum.innerText=`歷史最高:${number}`;
}
btn.style.display="block";
//alert("游戲結(jié)束");
//location.reload(true);
}
}
},10)
}
//子彈類(lèi)
classBullet{
constructor(firstbot,firstleft){
this.node=document.createElement("img");
this.firstbot=firstbot;
this.firstleft=firstleft;
this.init();
//console.log(this.firstbot);
}
init(){
this.create();
this.render();
this.move();
this.crash();
}
create(){
this.node.setAttribute('src','material/images/bullet1.png');
section.appendChild(this.node);
}
render(){
Object.assign(this.node.style,{
position:`absolute`,
bottom:`${this.firstbot}px`,
left:`${this.firstleft}px`,
})
}
move(){
lettime=setInterval(()={
this.crash();
this.firstbot+=2;
if(this.firstbot=550||getComputedStyle(this.node).display=='none'){
section.removeChild(this.node);
clearInterval(time);
}
this.render();
},10);
}
//碰撞
crash(){
//獲取所有敵機(jī)
constenemy=document.getElementsByClassName("enemys");
//console.log(enemy);
letbottom=parseInt(getComputedStyle(this.node).bottom);
letleft=parseInt(getComputedStyle(this.node).left);
for(letitemofenemy){
//子彈撞擊敵方飛機(jī)
if(bottom=parseInt(getComputedStyle(item).bottom)+parseInt(getComputedStyle(item).height)
bottom=parseInt(getComputedStyle(item).bottom)-parseInt(getComputedStyle(this.node).height)
left=parseInt(getComputedStyle(item).left)-parseInt(getComputedStyle(this.node).width)
left=parseInt(getComputedStyle(item).left)+parseInt(getComputedStyle(item).width)){
//停止子彈碰撞計(jì)時(shí)器
this.node.style.display="none";
item.dataset.id--;
if(item.dataset.id0){
item.dataset.id=0;
}
if(parseInt(getComputedStyle(item).width)==34){
if(item.dataset.id==0){
//圖片替換
item.setAttribute('src','material/images/小飛機(jī)爆炸.gif');
//延遲器
setTimeout(()={
item.style.display="none";
},300)
number+=1;
}
}
if(parseInt(getComputedStyle(item).width)==46){
if(item.dataset.id==0){
item.setAttribute('src','material/images/中飛機(jī)爆炸.gif');
setTimeout(()={
item.style.display="none";
},300)
number+=5;
}else{
item.setAttribute('src','material/images/中飛機(jī)挨打.png');
}
}
if(parseInt(getComputedStyle(item).width)==110){
if(item.dataset.id==0){
item.setAttribute('src','material/images/大飛機(jī)爆炸.gif');
//大飛機(jī)爆炸
letbigboom="./material/audio/bigboom.mp3";
bigboom=newAudio(bigboom);
bigboom.play();//播放mp3這個(gè)音頻對(duì)象
setTimeout(()={
item.style.display="none";
bigboom.pause();
},300)
number+=30;
}else{
item.setAttribute('src','material/images/大飛機(jī)挨打.png');
}
}
//boss爆炸
if(parseInt(getComputedStyle(item).width)==160){
if(item.dataset.id==0){
item.setAttribute('src','material/images/boomx.png');
clearInterval(bossshoot);
letbossover="./material/audio/bossover.mp3";
bossover=newAudio(bossover);
bossover.play();//播放mp3這個(gè)音頻對(duì)象
setTimeout(()={
item.style.display="none";
bossover.pause();
mp3.play();
},300)
number+=200;
}
}
}
}
}
classEnemys{
constructor(x,url,height){
this.node=document.createElement("img");
this.x=x;
this.y=546;
this.url=url;
this.height=height;
this.init();
}
init(){
this.create();
this.render();
this.move();
}
create(){
this.node.setAttribute('src',this.url);
this.node.setAttribute('class',"enemys");
section.appendChild(this.node);
}
render(){
Object.assign(this.node.style,{
position:`absolute`,
bottom:`${this.y}px`,
left:`${this.x}px`,
})
}
move(){
letenemytime=setInterval(()={
this.y-=1;
if(this.y=this.height||getComputedStyle(this.node).display=='none'){
section.removeChild(this.node);
clearInterval(enemytime)
}else{
this.render();
}
},10);
}
//小飛機(jī)
classSmallenemysextendsEnemys{
constructor(x,url,height,hp){
super(x,url,height);
this.hp=hp;
this.node.dataset.id=hp;
}
//中飛機(jī)
classMidenemysextendsEnemys{
constructor(x,url,height,hp){
super(x,url,height)
this.hp=hp;
this.node.dataset.id=hp;
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 電力安全生產(chǎn)知識(shí)培訓(xùn)
- 車(chē)委托改裝協(xié)議書(shū)模板
- 綠色建筑的職業(yè)規(guī)劃之路計(jì)劃
- 道路協(xié)議書(shū)模板
- 個(gè)人信用擔(dān)保貸款服務(wù)協(xié)議
- 沙特合作協(xié)議書(shū)
- 超市經(jīng)營(yíng)外包合同協(xié)議
- 教師全員培訓(xùn)體系構(gòu)建
- 電子商務(wù)平臺(tái)入駐協(xié)議指南
- 毛豬代購(gòu)協(xié)議書(shū)
- 2025年入團(tuán)考試必考題目試題及答案
- 動(dòng)物生理學(xué)題庫(kù)及答案(附解析)
- 【食品生產(chǎn)加工技術(shù)】古井燒鵝制作技術(shù)概要
- 項(xiàng)目部施工安全風(fēng)險(xiǎn)源識(shí)別清單
- 基于單片機(jī)的智能家居控制系統(tǒng)設(shè)計(jì)畢業(yè)設(shè)計(jì)論文
- (高職)《國(guó)際商務(wù)談判與溝通》完整版教學(xué)課件全套電子教案
- 用En值評(píng)價(jià)人員比對(duì)結(jié)果的范例
- 醫(yī)療信息平臺(tái)資源規(guī)劃及數(shù)據(jù)庫(kù)設(shè)計(jì)方案
- 銀行安全保衛(wèi)知識(shí)培訓(xùn)--ppt課件
- 農(nóng)村小學(xué)音樂(lè)課堂教學(xué)有效性及策略探究
- 支局一點(diǎn)一策PPT通用課件
評(píng)論
0/150
提交評(píng)論