




版權(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)簡(jiǎn)單飛機(jī)大戰(zhàn)小游戲本文實(shí)例為大家分享了javascript實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
html文件
!DOCTYPEhtml
htmllang='zh'
head
metacharset='UTF-8'
titlemm/title
linkrel="stylesheet"href="./css/index.css"
style
*{
margin:0;
padding:0;
list-style:none;
user-select:none;
}
/style
/head
body
div
!--游戲等級(jí)--
divid="level"
h1飛機(jī)大戰(zhàn)Lv1/h1
p簡(jiǎn)答模式/p
p一般模式/p
p困難模式/p
p地獄模式/p
/div
!--游戲界面--
divid="map"
!--我軍飛機(jī)--
!--敵軍飛機(jī)1--
!--敵軍飛機(jī)2--
div
!--我軍子彈--
/div
/div
!--游戲得分--
divid="score"0/div
!--結(jié)束游戲的界面--
divid="settlement"
div最終得分:span10/span/div
div獲得評(píng)價(jià):span青銅/span/div
button重新開(kāi)始/button
/div
/div
scriptsrc="./js/index.js"/script
/body
/html
css文件
.wrap{
position:relative;
width:400px;
height:600px;
margin:10pxauto;
overflow:hidden;
/*游戲等級(jí)*/
#level{
position:absolute;
top:0;
left:0;
z-index:1;
width:100%;
height:100%;
font-family:"楷體";
background:url(../img/bg_1.jpg)repeat0/cover;
#levelh1{
width:100%;
color:#fff;
text-align:center;
padding:100px0;
#levelp{
text-align:center;
padding:10px20px;
letter-spacing:5px;
font-weight:bold;
background-color:#fff;
margin:30px120px;
border-radius:5px;
box-shadow:1px1px10px1px#aaa;
#levelp:hover{
cursor:pointer;
background-color:#4D6BA2;
color:#fff;
/*
游戲界面
*/
#map{
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background:url(../img/bg_2.jpg)repeat0/cover;
#map.plane,/*我軍飛機(jī)*/
.enemy,/*敵軍飛機(jī)*/
.boom,/*敵軍飛機(jī)爆炸*/
.boom2,/*我軍飛機(jī)爆炸*/
.fire{/*我軍飛機(jī)子彈*/
position:absolute;
#map.plane,.enemy{
z-index:5;
#map.boom{
z-index:6;
animation:enemyDisappear0.8s1;
animation-fill-mode:forwards;/*顯示最后一個(gè)關(guān)鍵幀*/
#map.boom2{
z-index:6;
animation:planeDisappear2s1;
animation-fill-mode:forwards;
#map.fire{
z-index:7;
/*敵軍消失的動(dòng)畫(huà)*/
@keyframesenemyDisappear{
from{opacity:1;}
to{opacity:0;}
/*我軍消失的動(dòng)畫(huà)*/
@keyframesplaneDisappear{
0%{opacity:1;}
20%{opacity:0;}
40%{opacity:1;}
60%{opacity:0;}
80%{opacity:1;}
100%{opacity:0;}
/*
游戲得分
*/
#score{
display:none;
position:absolute;
top:0;
left:0;
z-index:10;
padding:10px20px;
color:#fff;
/*結(jié)束游戲的界面*/
#settlement{
display:none;
position:absolute;
top:30%;
left:0;
z-index:100;
width:100%;
height:35%;
font-family:"楷體";
#settlementdiv{
margin:20px0;
text-align:center;
font-size:20px;
color:#fff;
#settlementdivspan{
font-size:24px;
#settlementdiv.totalscore{
color:red;
#settlementdiv.appraise{
color:green;
#settlementbutton{
margin:30px34%0;
padding:10px20px;
background-color:rgba(255,255,255,.8);
letter-spacing:2px;
border:none;
font-family:"楷體";
font-size:20px;
font-weight:bold;
border-radius:5px;
#settlementbutton:hover{
cursor:pointer;
background-color:rgba(255,255,255,1);
}
js文件
letoWrap=document.querySelector(".wrap"),
oLevel=document.getElementById("level"),//游戲等級(jí)盒子
oLevelList=document.querySelectorAll("#levelp"),//游戲等級(jí)
oMap=document.getElementById("map"),//游戲界面
oAllFire=document.querySelector(".allFire"),//子彈盒子
oScore=document.getElementById("score"),//游戲得分
oSettlement=document.getElementById("settlement"),//結(jié)算界面
oButton=
oSettlement.querySelector("button");//重新開(kāi)始
for(leti=0;ioLevelList.length;i++){
oLevelList[i].onclick=function(e){
startGame(i,
{
x:e.clientX-oWrap.offsetLeft,//獲取鼠標(biāo)到游戲界面的位置
y:e.clientY-oWrap.offsetTop
}
)
};
//重新開(kāi)始
oButton.onclick=functionaginGame(){
//背景圖停止移動(dòng)
cancelAnimationFrame(oWrap.timer);
//隱藏得分界面
oSettlement.style.display="none";
//顯示等級(jí)
oLevel.style.display="block";
//分?jǐn)?shù)清0
oScore.innerHTML=0;
//
oMap.innerHTML=`divid="allFire"/div
oAllFire=document.getElementById("allFire");
//開(kāi)始游戲
functionstartGame(level,pos){
hiddenShow();
bgImgMove(level);
letmyPlane=createPlane(level,pos);
createEnemy(level,myPlane);
oWrap.score=0;
//點(diǎn)擊游戲等級(jí)的消失與隱藏
functionhiddenShow(){
oLevel.style.display="none";
oMap.style.display="block";
oScore.style.display="block";
//背景圖移動(dòng)
functionbgImgMove(level){
oMap.style.backgroundImage=`url(./img/bg_${level+1}.jpg)`;
letbgMove=0;
(functionbgMoveTimer(){
bgMove++;
oMap.style.backgroundPositionY=bgMove+"px";
oWrap.timer=requestAnimationFrame(bgMoveTimer);
})();
//我軍飛機(jī)飛機(jī)
functioncreatePlane(level,pos){
//創(chuàng)建我軍飛機(jī)
letoImgPlane=newImage();
oImgPlane.src="./img/plane_0.png";
oImgPlane.width=70;
oImgPlane.height=70;
oImgPlane.className="plane";
oImgPlane.style.left=pos.x-oImgPlane.width/2
+"px";
oImgPlane.style.top=pos.y-oImgPlane.height/2+"px";
oMap.appendChild(oImgPlane);
//鼠標(biāo)移動(dòng)是飛機(jī)不斷獲取鼠標(biāo)位置
letminLeft=-oImgPlane.width/2,
minTop=0,
maxLeft=oMap.clientWidth-oImgPlane.width/2,
maxTop=oMap.clientHeight-oImgPlane.height/2;
document.onmousemove=function(e){
letleft=e.clientX-oWrap.offsetLeft-oImgPlane.width/2,
top=e.clientY-oWrap.offsetTop-oImgPlane.height/2;
left=Math.min(left,maxLeft);
left=Math.max(left,minLeft);
top=Math.min(top,maxTop);
top=Math.max(top,minTop);
oImgPlane.style.left=left+"px";
oImgPlane.style.top=top+"px";
}
fire(level,oImgPlane)
returnoImgPlane;
functionfire(level,oImgPlane){
//創(chuàng)建子彈
functioncreateFires(isDouble,n){
letcreateFire=newImage();
createFire.src="./img/fire.png";
createFire.width=30;
createFire.height=30;
createFire.className="fire"
letleft=oImgPlane.offsetLeft+oImgPlane.width/2-createFire.width/2,
top=oImgPlane.offsetTop-oImgPlane.height/2;
if(isDouble){
left=left+oImgPlane.width/3*n;
}
createFire.style.left=left+"px";
createFire.style.top=top+"px";
oAllFire.appendChild(createFire);
//子彈運(yùn)動(dòng)
functionfireMove(){
if(createFire.parentNode){
top-=10;
if(top-createFire.height){
oAllFire.removeChild(createFire);
}else{
createFire.style.top=top+"px";
requestAnimationFrame(fireMove);
}
}
}
setTimeout(()={
requestAnimationFrame(fireMove);
},20);
}
//定時(shí)生成子彈的頻率
oWrap.fireTimer=setInterval(()={
if(oWrap.score=100){
createFires(true,-1);
createFires(true,+1);
}else{
createFires();
}
},[200,150,100,50][level])
//創(chuàng)建敵軍
letenemyNum=1;
functioncreateEnemy(level,myPlane){
letspeed=[4,6,8,10][level];//游戲模式相對(duì)應(yīng)的的等級(jí)飛機(jī)下落的速度
MapW=oMap.clientWidth,
MapH=oMap.clientHeight;
oWrap.enemyTimer=setInterval(()={
letcreateEnemy=newImage();
createEnemy.index=enemyNum%201:0;
createEnemy.src=`./img/enemy_${["big","small"][createEnemy.index]}.png`;
createEnemy.width=[100,50][createEnemy.index];
createEnemy.height=[100,50][createEnemy.index];
//敵軍的血量
createEnemy.hp=[4,1][createEnemy.index];
createEnemy.className="enemy";
//敵軍首次出現(xiàn)的位置
letenemyTop=-createEnemy.height;
createEnemy.style.top=enemyTop+"px";
createEnemy.style.left=Math.random()*MapW-createEnemy.width/2+"px";
oMap.appendChild(createEnemy);
enemyNum++;
console.log(createEnemy.offsetLeft,createEnemy.offsetTop)
//判斷敵軍的下落運(yùn)動(dòng)
functionenemyMove(){
if(createEnemy.parentNode){
enemyTop+=speed;
if(enemyTop=MapH){
oMap.removeChild(createEnemy);
}else{
createEnemy.style.top=enemyTop+"px";
//子彈與敵軍發(fā)生碰撞
letarrAllFire=oAllFire.children;
for(leti=arrAllFire.length-1;ii--){
letnewFire=arrAllFire[i];
if(isCollide(newFire,createEnemy)){
//撞上的移出子彈
oAllFire.removeChild(newFire)
//血量減一
createEnemy.hp--;
if(createEnemy.hp===0){
oWrap.score+=[5,1][createEnemy.index];
oScore.innerText=oWrap.score;
//敵軍發(fā)生爆炸
boom({
x:createEnemy.offsetLeft,
y:createEnemy.offsetTop,
w:createEnemy.width,
h:createEnemy.height,
i:createEnemy.index
});
oMap.removeChild(createEnemy);
return;
}
}
}
//我軍與敵軍發(fā)生碰撞
if(myPlane.parentNodeisCollide(createEnemy,myPlane)){
//敵軍產(chǎn)生爆炸
boom({
x:createEnemy.offsetLeft,
y:createEnemy.offsetTop,
w:createEnemy.width,
h:createEnemy.height,
i:createEnemy.index
});
//我軍發(fā)生爆炸
boom({
x:myPlane.offsetLeft,
y:myPlane.offsetTop,
w:myPlane.width,
h:myPlane.height,
i:2
});
oMap.removeChild(createEnemy);
oMap.removeChild(myPlane)
//游戲結(jié)束
gameOver();
return;
}
requestAnimationFrame(enemyMove);
}
}
}
requestAnimationFrame(enemyMove);
},[400,350,300,200][level])
//是否發(fā)生碰撞
functionisCollide(newFirePlane,createEnemy){
//子彈/我軍飛機(jī)的位置
letfireTop=newFirePlane.offsetTop,
fireLeft=newFirePlane.offsetLeft,
fireBottom=fireTop+newFirePlane.clientHeight,
fireRight=fireLeft+newFirePlane.clientWidth;
//飛機(jī)的位置
letcreateEnemyTop=createEnemy.offsetTop,
createEnemyLeft=createEnemy.offsetLeft,
createEnemyRight=createEnemyLeft+createEnemy.clientWidth,
createEnemyBotoom=createEnemyTop+createEnemy.clientHeight;
//沒(méi)碰上的四種結(jié)果
return!(fireTopcreateEnemyBotoom||
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公司銀行本票管理制度
- 售票崗位風(fēng)險(xiǎn)管理制度
- 別墅現(xiàn)場(chǎng)裝修管理制度
- 開(kāi)發(fā)與測(cè)試的協(xié)同工作模式探討試題及答案
- 醫(yī)院推行電腦管理制度
- 廠區(qū)裝修現(xiàn)場(chǎng)管理制度
- 學(xué)校教室規(guī)章管理制度
- 醫(yī)院職工假期管理制度
- 公司禮品贈(zèng)送管理制度
- 沖床加工車(chē)間管理制度
- 爆炸賠償協(xié)議書(shū)
- 數(shù)據(jù)備份與恢復(fù)技巧試題及答案
- 高級(jí)審計(jì)師考試關(guān)注熱點(diǎn)試題及答案
- 2024年建筑《主體結(jié)構(gòu)及裝飾裝修》考試習(xí)題庫(kù)(濃縮500題)
- 致2025屆高考生高二到高三如何順利轉(zhuǎn)型
- 慈善專(zhuān)項(xiàng)捐贈(zèng)協(xié)議書(shū)
- 2025年高考數(shù)學(xué)二輪熱點(diǎn)題型歸納與演練(上海專(zhuān)用)專(zhuān)題06數(shù)列(九大題型)(原卷版+解析)
- 2025中國(guó)鐵路南寧局集團(tuán)有限公司招聘高校畢業(yè)生32人四(本科及以上學(xué)歷)筆試參考題庫(kù)附帶答案詳解
- 國(guó)開(kāi)政治經(jīng)濟(jì)學(xué)形考任務(wù)1-4試題及答案
- 第1章 整式的乘法(單元測(cè)試)(原卷)2024-2025學(xué)年湘教版七年級(jí)數(shù)學(xué)下冊(cè)
- 《高中數(shù)學(xué)知識(shí)競(jìng)賽》課件
評(píng)論
0/150
提交評(píng)論