




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第用JS實現貪吃蛇游戲本文實例為大家分享了JS實現貪吃蛇游戲的具體代碼,供大家參考,具體內容如下
效果圖:
完整代碼如下:
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(){
//聲明方塊的寬高,行數和列數
varsw=20,
sh=20,
tr=30,
td=30;
varsnake=null,//蛇的實例
food=null,//食物的實例
game=null;//游戲的實例
functionSquare(x,y,classname){
this.x=sw*x;
this.y=sh*y;
this.class=classname;
this.viewContent=document.createElement('div');//方塊對應的DOM元素
this.viewContent.className=this.class;
this.parent=document.getElementById('snakeWrap');//方塊的父級
}
//創建方塊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=[];//存蛇身上每個方塊的位置
//存儲蛇走的方向,用一個對象來表示
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(){
//創建蛇頭
varsnakeHead=newSquare(2,0,'snakeHead');
snakeHead.create();
this.head=snakeHead;//存儲蛇頭信息
this.pos.push([2,0]);//把蛇頭的位置存起來
//創建蛇身體1
varsnakeBody1=newSquare(1,0,'snakeBody');
snakeBody1.create();
this.pos.push([1,0]);//把蛇身1的坐標存起來
//創建蛇身體2
varsnakeBody2=newSquare(0,0,'snakeBody');
snakeBody2.create();
this.tail=snakeBody2;//把蛇尾的信息存起來
this.pos.push([0,0]);//把蛇身1的坐標存起來
//形成鏈表關系
snakeHead.last=null;
snakeHead.next=snakeBody1;
snakeBody1.last=snakeHead;
snakeBody1.next=snakeBody2;
snakeBody2.last=snakeBody1;
snakeBody2.next=null;
//給蛇添加一條屬性,用來表示蛇的走向
this.direction=this.directionNum.right;//默認讓蛇往右走
};
//該方法用來獲取蛇頭的下一個位置對應的元素,要根據元素做不同的事情
Stotype.getNextPos=function(){
varnextPos=[//蛇頭要走的下一個點的坐標
this.head.x/sw+this.direction.x,
this.head.y/sh+this.direction.y
];
//console.log(nextPos[0]);
//下個點是自己,游戲結束
varselfCollied=false;//是否撞到自己,默認是否
//value表示數組中的某一項
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;
}
//下個點是墻,游戲結束
if(nextPos[0]0||nextPos[1]0||nextPos[0]td-1||nextPos[1]tr-1){
console.log('撞墻!');
this.strategies.die.call(this);
return;
}
//下個點是食物,吃
if(foodfood.pos[0]==nextPos[0]food.pos[1]==nextPos[1]){
//如這條件成立,說明蛇頭走的下一點是食物的點
console.log('撞到食物了');
this.strategies.eat.call(this);
return;
}
//下個點什么都不是,繼續走
this.strategies.move.call(this);
};
//處理碰撞后要做的事
Stotype.strategies={
move:function(format){//format這個參數用于決定要不要刪除最后一個方塊(蛇尾),當傳了這個參數就表示要做的事情是吃
//創建新身體(在舊蛇頭的位置)
varnewBody=newSquare(this.head.x/sw,this.head.y/sh,'snakeBody');
//更新鏈表的關系
newBody.next=this.head.next;
newBody.next.last=newBody;
newBody.last=null;
//把舊蛇頭從原來的位置刪除
this.head.remove();
newBody.create();
//創建新的蛇頭(蛇頭下一個要走到的點nextPos)
varnewHead=newSquare(this.head.x/sw+this.direction.x,this.head.y/sh+this.direction.y,'snakeHead');
//更新鏈表關系
newHead.next=newBody;
newHead.last=null;
newBody.last=newHead;
newHead.viewContent.style.transform='rotate('+this.direction.rotate+'deg)';
newHead.create();
//蛇身上的每一個方塊的坐標也要更新
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,表示需要刪除(除了吃之外的操作)
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();
//創建食物
functioncreateFood(){
//食物小方塊的隨機坐標
varx=null;
vary=null;
varinclude=true;//循環跳出的條件,true表示食物坐標在蛇身上。(需要繼續循環)false表示食物的坐標不再蛇身上(不用繼續循環)
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]){
//這個條件成立說明現在隨機出來的這個坐標,在蛇身上沒有找到。
include=false;
}
});
}
//生成食物
food=newSquare(x,y,'food');
//存儲生成食物的坐標,用于跟蛇頭要走的下一坐標對比
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();
}
}
//創建游戲邏輯
functionGame(){
this.timer=null;
this.score=0;
}
Gtotype.init=function(){
snake.init();
//snake.getNextPos();
createFood();
varflag=true;
document.onkeydown=function(ev){
//當蛇在往右走,不能按左鍵
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);
};
//游戲結束
Gt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 航空運輸業2025年疫情復蘇策略與綠色出行趨勢研究報告001
- 游戲化教學在兒童自主學習能力培養中的應用策略與成效
- 2024年南京市雨花臺區所屬學校招聘教師考試真題
- 未來五年全球流媒體平臺競爭態勢與內容創意發展研究報告
- Phenylbiguanide-Standard-N-Phenylbiguanide-Standard-生命科學試劑-MCE
- BLINK11-生命科學試劑-MCE
- 2025至2030有機絕熱材料市場前景分析及發展趨勢分析與未來投資戰略咨詢研究報告
- 2024-2025學年度浙江省金磚聯盟高一第二學期期中考試歷史試題(含答案)
- 2025至2030寵物衣服市場行業發展趨勢分析與未來投資戰略咨詢研究報告
- 環境監測物聯網技術在工業廢棄物處理中的應用報告
- GB/T 15452-2009工業循環冷卻水中鈣、鎂離子的測定EDTA滴定法
- 一二三四級應急響應流程圖參考模板范本
- 《等離子弧焊》教學課件
- 電動車棚施工方案及工藝方法
- 前臺交接班記錄表
- 油氣藏類型、典型的相圖特征和識別實例
- COMSOL 4.4 模擬螺線管線圈產生的磁場分布
- 氣相催化加氫法生產1.5萬噸年苯胺車間工藝設計
- 三位數顯示計時器定時器數電課程設計報告書
- 凹凸棒石(千土之王、萬用之土)
- 大氣污染控制工程課程設計_某工廠布袋除塵器的設計
評論
0/150
提交評論