




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第javascript實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲本文實(shí)例為大家分享了javascript實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
文檔結(jié)構(gòu)如下
其中tool文件中只使用了隨機(jī)數(shù),audio中是存放的音樂文件,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部分
主要是通過類方法創(chuàng)建敵機(jī)和我方飛機(jī),再通過類的繼承給予小/中/大/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;
//游戲開始
btn.addEventListener("click",function(){
//console.log(gameover);
if(gameover){
gameover.pause();
}
mp3="./material/audio/bgm.mp3";
mp3=newAudio(mp3);
mp3.play();//播放mp3這個音頻對象
//計算分?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這個音頻對象
//延遲器
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(){
//鍵盤按下
document.addEventListener("keydown",(event)={
if(this.move){
this.move(event);
}
});
//鍵盤抬起
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;
}
})
}
//移動
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é)束時清除除自身外飛機(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這個音頻對象
if(arrnumber){
localStorage.setItem('scort',JSON.stringify(number));
historynum.innerText=`歷史最高:${number}`;
}
btn.style.display="block";
//alert("游戲結(jié)束");
//location.reload(true);
}
}
},10)
}
//子彈類
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)){
//停止子彈碰撞計時器
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這個音頻對象
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這個音頻對象
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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 通信設(shè)備性能測試分析服務(wù)協(xié)議
- 工業(yè)生產(chǎn)安全風(fēng)險分析及安全措施
- 遼寧民族師范高等??茖W(xué)?!恶R克思主義經(jīng)典原著選讀》2023-2024學(xué)年第一學(xué)期期末試卷
- 企事業(yè)單位園林綠化行政職責(zé)
- 醫(yī)院建設(shè)工期承諾及保證措施他
- 餐飲行業(yè)節(jié)能減排下的采購配送應(yīng)急保障措施
- 2025年系列自動遙測氣象站項目規(guī)劃申請報告
- 房屋征收補(bǔ)償合同協(xié)議書范文樣本
- 老年護(hù)理機(jī)構(gòu)防疫措施
- 2025年業(yè)務(wù)拓展個人述職報告范文
- 甘肅蘭州2025年公開招聘農(nóng)村(村務(wù))工作者筆試題帶答案分析
- 青少年去極端化安全教育宣傳
- 大健康項目商業(yè)計劃書
- AI賦能與素養(yǎng)導(dǎo)向:初中英語智慧課堂的融合實(shí)踐研究
- 《2025年危險化學(xué)品企業(yè)安全生產(chǎn)執(zhí)法檢查重點(diǎn)事項指導(dǎo)目錄》解讀與培訓(xùn)
- 2025全國交通運(yùn)輸行業(yè)職業(yè)技能競賽學(xué)生組技術(shù)方案
- 常用儀表使用方法鐵道信號綜合實(shí)訓(xùn)課件
- 2025云南黃金礦業(yè)集團(tuán)股份限公司招聘114人易考易錯模擬試題(共500題)試卷后附參考答案
- 2025年03月四川成都農(nóng)業(yè)科技中心公開招聘筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 預(yù)防職務(wù)犯罪交流課件
- 《2025年CSCO非小細(xì)胞癌診療指南》解讀
評論
0/150
提交評論