




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第用JS實(shí)現(xiàn)貪吃蛇游戲本文實(shí)例為大家分享了JS實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
完整代碼如下:
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="index.css"
scriptsrc="index.js"/script
/head
body
div
divbutton/button/div
divbutton/button/div
divid="snakeWrap"
!--div/div
div/div
div/div--
/div
/div
/body
/html
css:
.content{
position:relative;
width:640px;
height:640px;
margin:100pxauto;
.btn{
width:100%;
height:100%;
position:absolute;
top:0;
left:0;
background-color:black;
opacity:.1;
z-index:2;
.btnbutton{
background:none;
border:none;
background-size:100%100%;
cursor:pointer;
outline:none;
position:absolute;
left:50%;
top:50%;
.startBtnbutton{
width:200px;
height:80px;
background-image:url(../貪吃蛇/img/btn.gif);
margin-left:-100px;
margin-top:-40px;
.pauseBtn{
display:none;
.pauseBtnbutton{
width:70px;
height:70px;
background-image:url(../貪吃蛇/img/btn4.png);
margin-left:-35px;
margin-top:-35px;
#snakeWrap{
width:600px;
height:600px;
background-color:pink;
border:20pxsolidpurple;
position:relative;
/*#snakeWrapdiv{
width:20px;
height:20px;
.snakeHead{
background-image:url(../貪吃蛇/img/snake.png);
background-size:cover;
.snakeBody{
background-color:rgb(85,146,126);
border-radius:50%;
.food{
background-image:url(../貪吃蛇/img/草莓.png);
background-size:cover;
}
js:
window.addEventListener('load',function(){
//聲明方塊的寬高,行數(shù)和列數(shù)
varsw=20,
sh=20,
tr=30,
td=30;
varsnake=null,//蛇的實(shí)例
food=null,//食物的實(shí)例
game=null;//游戲的實(shí)例
functionSquare(x,y,classname){
this.x=sw*x;
this.y=sh*y;
this.class=classname;
this.viewContent=document.createElement('div');//方塊對(duì)應(yīng)的DOM元素
this.viewContent.className=this.class;
this.parent=document.getElementById('snakeWrap');//方塊的父級(jí)
}
//創(chuàng)建方塊DOM,并添加到頁面里
Stotype.create=function(){
this.viewContent.style.position='absolute';
this.viewContent.style.width=sw+'px';
this.viewContent.style.height=sh+'px';
this.viewContent.style.left=this.x+'px';
this.viewContent.style.top=this.y+'px';
this.parent.appendChild(this.viewContent);
};
Stotype.remove=function(){
this.parent.removeChild(this.viewContent);
};
//蛇
functionSnake(){
this.head=null//存蛇頭的信息
this.tail=null//存蛇尾的信息
this.pos=[];//存蛇身上每個(gè)方塊的位置
//存儲(chǔ)蛇走的方向,用一個(gè)對(duì)象來表示
this.directionNum={
left:{
x:-1,
y:0,
rotate:180
},
right:{
x:1,
y:0,
rotate:0
},
up:{
x:0,
y:-1,
rotate:-90
},
down:{
x:0,
y:1,
rotate:90
}
};
}
//初始化
Stotype.init=function(){
//創(chuàng)建蛇頭
varsnakeHead=newSquare(2,0,'snakeHead');
snakeHead.create();
this.head=snakeHead;//存儲(chǔ)蛇頭信息
this.pos.push([2,0]);//把蛇頭的位置存起來
//創(chuàng)建蛇身體1
varsnakeBody1=newSquare(1,0,'snakeBody');
snakeBody1.create();
this.pos.push([1,0]);//把蛇身1的坐標(biāo)存起來
//創(chuàng)建蛇身體2
varsnakeBody2=newSquare(0,0,'snakeBody');
snakeBody2.create();
this.tail=snakeBody2;//把蛇尾的信息存起來
this.pos.push([0,0]);//把蛇身1的坐標(biāo)存起來
//形成鏈表關(guān)系
snakeHead.last=null;
snakeHead.next=snakeBody1;
snakeBody1.last=snakeHead;
snakeBody1.next=snakeBody2;
snakeBody2.last=snakeBody1;
snakeBody2.next=null;
//給蛇添加一條屬性,用來表示蛇的走向
this.direction=this.directionNum.right;//默認(rèn)讓蛇往右走
};
//該方法用來獲取蛇頭的下一個(gè)位置對(duì)應(yīng)的元素,要根據(jù)元素做不同的事情
Stotype.getNextPos=function(){
varnextPos=[//蛇頭要走的下一個(gè)點(diǎn)的坐標(biāo)
this.head.x/sw+this.direction.x,
this.head.y/sh+this.direction.y
];
//console.log(nextPos[0]);
//下個(gè)點(diǎn)是自己,游戲結(jié)束
varselfCollied=false;//是否撞到自己,默認(rèn)是否
//value表示數(shù)組中的某一項(xiàng)
this.pos.forEach(function(value){
//console.log(nextPos[0],value[0]);
if(value[0]==nextPos[0]value[1]==nextPos[1]){
selfCollied=true;
//console.log(2);
}
});
if(selfCollied){
console.log('撞到自己了!');
this.strategies.die.call(this);
return;
}
//下個(gè)點(diǎn)是墻,游戲結(jié)束
if(nextPos[0]0||nextPos[1]0||nextPos[0]td-1||nextPos[1]tr-1){
console.log('撞墻!');
this.strategies.die.call(this);
return;
}
//下個(gè)點(diǎn)是食物,吃
if(foodfood.pos[0]==nextPos[0]food.pos[1]==nextPos[1]){
//如這條件成立,說明蛇頭走的下一點(diǎn)是食物的點(diǎn)
console.log('撞到食物了');
this.strategies.eat.call(this);
return;
}
//下個(gè)點(diǎn)什么都不是,繼續(xù)走
this.strategies.move.call(this);
};
//處理碰撞后要做的事
Stotype.strategies={
move:function(format){//format這個(gè)參數(shù)用于決定要不要?jiǎng)h除最后一個(gè)方塊(蛇尾),當(dāng)傳了這個(gè)參數(shù)就表示要做的事情是吃
//創(chuàng)建新身體(在舊蛇頭的位置)
varnewBody=newSquare(this.head.x/sw,this.head.y/sh,'snakeBody');
//更新鏈表的關(guān)系
newBody.next=this.head.next;
newBody.next.last=newBody;
newBody.last=null;
//把舊蛇頭從原來的位置刪除
this.head.remove();
newBody.create();
//創(chuàng)建新的蛇頭(蛇頭下一個(gè)要走到的點(diǎn)nextPos)
varnewHead=newSquare(this.head.x/sw+this.direction.x,this.head.y/sh+this.direction.y,'snakeHead');
//更新鏈表關(guān)系
newHead.next=newBody;
newHead.last=null;
newBody.last=newHead;
newHead.viewContent.style.transform='rotate('+this.direction.rotate+'deg)';
newHead.create();
//蛇身上的每一個(gè)方塊的坐標(biāo)也要更新
this.pos.splice(0,0,[this.head.x/sw+this.direction.x,this.head.y/sh+this.direction.y])
this.head=newHead;//還要把this.head的信息更新一下
if(!format){//如果format的值為false,表示需要?jiǎng)h除(除了吃之外的操作)
this.tail.remove();
this.tail=this.tail.last;
this.pos.pop();
}
},
eat:function(){
this.strategies.move.call(this,true);
createFood();
game.score++;
},
die:function(){
//console.log('die');
game.over();
}
}
snake=newSnake();
//創(chuàng)建食物
functioncreateFood(){
//食物小方塊的隨機(jī)坐標(biāo)
varx=null;
vary=null;
varinclude=true;//循環(huán)跳出的條件,true表示食物坐標(biāo)在蛇身上。(需要繼續(xù)循環(huán))false表示食物的坐標(biāo)不再蛇身上(不用繼續(xù)循環(huán))
while(include){
x=Math.round(Math.random()*(td-1));
y=Math.round(Math.random()*(tr-1));
snake.pos.forEach(function(value){
if(x!=value[0]y!=value[1]){
//這個(gè)條件成立說明現(xiàn)在隨機(jī)出來的這個(gè)坐標(biāo),在蛇身上沒有找到。
include=false;
}
});
}
//生成食物
food=newSquare(x,y,'food');
//存儲(chǔ)生成食物的坐標(biāo),用于跟蛇頭要走的下一坐標(biāo)對(duì)比
food.pos=[x,y];
varfoodDom=document.querySelector('.food');
if(foodDom){
foodDom.style.left=x*sw+'px';
foodDom.style.top=y*sh+'px';
}else{
food.create();
}
}
//創(chuàng)建游戲邏輯
functionGame(){
this.timer=null;
this.score=0;
}
Gtotype.init=function(){
snake.init();
//snake.getNextPos();
createFood();
varflag=true;
document.onkeydown=function(ev){
//當(dāng)蛇在往右走,不能按左鍵
if(ev.which==37snake.direction!=snake.directionNum.right){
if(flag){
flag=false;
setTimeout(function(){
snake.direction=snake.directionNum.left;
flag=true;
},150)
}
}elseif(ev.which==38snake.direction!=snake.directionNum.down){
if(flag){
flag=false;
setTimeout(function(){
snake.direction=snake.directionNum.up;
flag=true;
},150)
}
}elseif(ev.which==39snake.direction!=snake.directionNum.left){
if(flag){
flag=false;
setTimeout(function(){
snake.direction=snake.directionNum.right;
flag=true;
},150)
}
}elseif(ev.which==40snake.direction!=snake.directionNum.up){
if(flag){
flag=false;
setTimeout(function(){
snake.direction=snake.directionNum.down;
flag=true;
},150)
}
}
}
this.start();
};
//開始游戲
Gtotype.start=function(){
this.timer=setInterval(function(){
snake.getNextPos();
},150);
};
//暫停游戲
Gtotype.pause=function(){
clearInterval(this.timer);
};
//游戲結(jié)束
Gt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- QC-T 375-1999 一端固定式單管夾片
- 2025年中考數(shù)學(xué)沖刺復(fù)習(xí)《幾何圖形》含答案解析
- 2026年高考?xì)v史一輪復(fù)習(xí)講義:統(tǒng)編版選擇性必修2經(jīng)濟(jì)與社會(huì)生活
- 2025年憲法知識(shí)競(jìng)賽試題庫及答案(共88題)
- 2025年升降機(jī)司機(jī)考試題題庫
- 2025年外研版高中英語選擇性必修第二冊(cè)Unit 6綜合檢測(cè)試卷及答案
- 2025年山西(行測(cè))考試模擬試題(含答案)
- 2025上海市八年級(jí)升九年級(jí)數(shù)學(xué)暑假提升講義:三角形一邊的平行線(第1課時(shí))(十大題型)解析版
- 2026高中語文必須要關(guān)注的七種高頻作文題型-2026年高考語文議論文寫作技巧
- 2025統(tǒng)編版初升高語文專項(xiàng)提升:文言句式(解析版)
- 硅PU球場(chǎng)施工方案模板
- 職高英語詞匯表優(yōu)質(zhì)資料
- YY/T 0752-2009電動(dòng)骨組織手術(shù)設(shè)備
- 用人單位職業(yè)衛(wèi)生檔案(加油站)
- GB/T 40080-2021鋼管無損檢測(cè)用于確認(rèn)無縫和焊接鋼管(埋弧焊除外)水壓密實(shí)性的自動(dòng)電磁檢測(cè)方法
- GB/T 2-2001緊固件外螺紋零件的末端
- 插花藝術(shù)全部講課稿課件
- 標(biāo)準(zhǔn)DBS54 2002-2017 食品安全地方標(biāo)準(zhǔn) 糌粑制作規(guī)范
- 教育評(píng)價(jià)學(xué)全套ppt課件完整版教學(xué)教程
- 油氣藏類型、典型的相圖特征和識(shí)別實(shí)例
- 未來教育家治校方略
評(píng)論
0/150
提交評(píng)論