js實(shí)現(xiàn)網(wǎng)頁版貪吃蛇游戲
使用原生 js 實(shí)現(xiàn)貪吃蛇小游戲,首先這個(gè) 小游戲的目錄結(jié)構(gòu)如下: 有 貪吃蛇 , 食物 ,地圖 ,還有 游戲
當(dāng)我們?cè)跒g覽器打開 index.html 的時(shí)候,會(huì)出現(xiàn) 移動(dòng)的小蛇 ,隨機(jī)生成的食物(這里只有一個(gè),當(dāng)前食物被吃掉,才會(huì)初始化下一個(gè)),用戶通過鍵盤上的方向鍵控制小蛇移動(dòng)的方向
當(dāng)小蛇觸碰到了墻,即畫布邊緣的時(shí)候,游戲結(jié)束!
接下來就是代碼實(shí)現(xiàn)啦 ~
食物模塊
//食物的自調(diào)用函數(shù) (function(){ //創(chuàng)建一個(gè)數(shù)組 來存放元素 var elements=[]; //食物就是一個(gè)對(duì)象 有寬 有高 有顏色 有橫縱坐標(biāo) 先有構(gòu)造函數(shù) 然后創(chuàng)建對(duì)象 function Food(width,height,color,x,y){ //元素的寬和高 默認(rèn)20 this.width=width||20; this.height=height||20; //元素的顏色 默認(rèn)綠色 this.color=color||"green"; //元素的橫縱坐標(biāo) 默認(rèn)為0 this.x=x||0; this.y=y||0; } //為元素添加初始化的方法 最好放原型里 公用 在頁面上顯示 所以需要map Food.prototype.init=function(map) { //先刪除食物 //外部無法訪問的函數(shù) remove(); // 創(chuàng)建元素 var div=document.createElement("div"); // 把元素追加到map中 map.appendChild(div); // 設(shè)置元素的樣式 寬 高 顏色 div.style.width=this.width+"px"; div.style.height=this.height+"px"; div.style.backgroundColor=this.color; //先脫離文檔流 div.style.position="absolute"; //橫縱坐標(biāo)隨機(jī)產(chǎn)生的 this.x=parseInt(Math.random()*(map.offsetWidth/this.width))*this.width; this.y=parseInt(Math.random()*(map.offsetHeight/this.height))*this.height; //元素的橫縱坐標(biāo) div.style.left=this.x+"px"; div.style.top=this.y+"px"; //把div元素追加到elements數(shù)組中 elements.push(div); }; //私有的函數(shù) 刪除食物的 function remove(){ //elements數(shù)組中有這個(gè)食物 for(var i=0;i<elements.length;i++){ var ele=elements[i]; //找到這個(gè)食物的父級(jí)元素 從地圖上刪除食物 ele.parentNode.removeChild(ele); //刪除數(shù)組的div元素 在i處刪除一項(xiàng) elements.splice(i,1); } } //把Food暴露給window window.Food=Food; }());
小蛇模塊
//小蛇的自調(diào)用函數(shù) (function(){ //定義一個(gè)數(shù)組 存放小蛇 var elements=[]; // 小蛇的構(gòu)造函數(shù) function Snake(width,height,direction){ //每個(gè)部分的寬和高 this.width=width||20; this.height=height||20; //小蛇的身體部分 this.body=[ {x:3,y:2,color:"red"},//小蛇的頭部 {x:2,y:2,color:"orange"},//小蛇的身體 {x:1,y:2,color:"orange"}//小蛇的身體 ]; //方向 this.direction=direction||"right"; } //通過原型添加方法,給小蛇添加初始化方法 Snake.prototype.init=function(map){ remove(); //循環(huán)遍歷 for(var i=0;i<this.body.length;i++){ //每一個(gè)數(shù)組元素都是一個(gè)對(duì)象 var obj=this.body[i]; //創(chuàng)建div var div=document.createElement("div"); //追加到map中 map.appendChild(div); //設(shè)置div的樣式 div.style.position="absolute"; div.style.width=this.width+"px"; div.style.height=this.height+"px"; //橫縱坐標(biāo) div.style.left=obj.x*this.width+"px"; div.style.top=obj.y*this.height+"px"; //背景顏色 div.style.backgroundColor=obj.color; //方向暫定 //把div追加到elements數(shù)組中 目的是為了刪除 elements.push(div); } }; //通過原型添加move方法 Snake.prototype.move=function(food,map) { //小蛇的身體部分 把前一個(gè)的橫縱坐標(biāo)給下一個(gè) var i=this.body.length-1; //逆序循環(huán) for(;i>0;i--){ this.body[i].x=this.body[i-1].x; this.body[i].y=this.body[i-1].y; } // 判斷方向 改變小蛇的頭部的坐標(biāo) switch(this.direction){ case "right": this.body[0].x+=1; break; case "left": this.body[0].x-=1; break; case "top": this.body[0].y-=1; break; case "bottom": this.body[0].y+=1; break; } //判斷有沒有吃到食物 //小蛇的頭的坐標(biāo)和食物的坐標(biāo)一致 var headX=this.body[0].x*this.width; var headY=this.body[0].y*this.height; //判斷小蛇的頭部坐標(biāo)和食物的坐標(biāo)是否相同 if(headX==food.x&&headY==food.y){ //獲取小蛇的最后的尾巴 var last=this.body[this.body.length-1]; //加入到數(shù)組中 以對(duì)象的方式加入 this.body.push({ x:last.x, y:last.y, color:last.color }); //把食物刪除 初始化食物 food.init(map); } }; //添加私有的刪除函數(shù) function remove(){ var i=elements.length-1; //逆序 找到這個(gè)元素的父元素 刪除 for(;i>=0;i--){ var ele=elements[i]; //從地圖上刪除元素 ele.parentNode.removeChild(ele); //從數(shù)組中刪除 elements.splice(i,1); } } //把Snake暴露給window window.Snake=Snake; }());
游戲模塊
//游戲的自調(diào)用函數(shù) (function(){ var that=null; //游戲的構(gòu)造函數(shù) function Game(map){ this.food=new Food();//食物對(duì)象 this.snake=new Snake();//小蛇對(duì)象 this.map=map;//地圖 that=this;//保留當(dāng)前的實(shí)例對(duì)象到that變量中 此時(shí)that 就是this } //游戲初始化 Game.prototype.init=function(){ //食物初始化 this.food.init(this.map); //小蛇初始化 this.snake.init(this.map);//先讓小蛇顯示 //調(diào)用設(shè)置小蛇移動(dòng)的方法 this.runSnake(this.food,this.map); //調(diào)用按鍵的方法 this.bindKey(); }; //添加原型函數(shù) 設(shè)置小蛇可以自由移動(dòng) Game.prototype.runSnake=function(food,map){ //此時(shí)的this是實(shí)例對(duì)象 //setInterval 方法是通過window調(diào)用的 this指向改變了 var timeId=setInterval(function(){ this.snake.move(food,map); this.snake.init(map); //橫坐標(biāo)的最大值 map的屬性在style標(biāo)簽中 var maxX=map.offsetWidth/this.snake.width; //縱坐標(biāo)的最大值 var maxY=map.offsetHeight/this.snake.height; var headX=this.snake.body[0].x; var headY=this.snake.body[0].y; // 橫坐標(biāo)方向的檢測 if(headX<0||headX>=maxX){ //撞墻了 停止定時(shí)器 clearInterval(timeId); alert("游戲結(jié)束"); } //縱坐標(biāo)方向的檢測 if(headY<0||headY>=maxY){ //撞墻了 停止定時(shí)器 clearInterval(timeId); alert("游戲結(jié)束"); } }.bind(that),200);//綁定到that中 即實(shí)例對(duì)象 }; //獲取用戶的按鍵 改變小蛇的方向 Game.prototype.bindKey=function(){ //這里的this 應(yīng)該是觸發(fā)keydown事件的對(duì)象 --document //所以這里的this就是document //獲取用戶的按鍵 document.addEventListener("keydown",function(e){ switch(e.keyCode){ case 37: this.snake.direction="left"; break; case 38: this.snake.direction="top"; break; case 39: this.snake.direction="right"; break; case 40: this.snake.direction="bottom"; break; } }.bind(that),false);//綁定實(shí)例對(duì)象 }; //暴露給window window.Game=Game; }());
主頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>貪吃蛇</title> <style> .map { width: 1800px; height: 800px; background-color: gray; position: relative; margin: 0 auto; } </style> </head> <body> <!-- 畫出地圖 設(shè)置樣式 --> <div class="map"></div> <script src="../js/food.js"></script> <script src="../js/snake.js"></script> <script src="../js/game.js"></script> <script> //初始化游戲?qū)ο? var game=new Game(document.querySelector(".map")); //初始化游戲 game.init(); </script> </body> </html>
小編還為大家準(zhǔn)備了精彩的專題:javascript經(jīng)典小游戲匯總
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript創(chuàng)建數(shù)組的最簡代碼
2008-02-02javascript實(shí)現(xiàn)搶購倒計(jì)時(shí)程序
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)搶購倒計(jì)時(shí)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08document.getElementById的一些細(xì)節(jié)
document.getElementById的一些細(xì)節(jié)...2006-09-09JS圖片定時(shí)翻滾效果實(shí)現(xiàn)方法
這篇文章主要介紹了JS圖片定時(shí)翻滾效果實(shí)現(xiàn)方法,涉及javascript結(jié)合時(shí)間函數(shù)實(shí)現(xiàn)頁面元素動(dòng)態(tài)切換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06JavaScript中for與forEach分別如何跳出循環(huán)
forEach的優(yōu)勢(shì)一個(gè)是它的回調(diào)函數(shù)形成了一個(gè)作用域,它的curItem和i不會(huì)像for循環(huán)一樣污染全局變量,這篇文章主要給大家介紹了關(guān)于JavaScript中for與forEach分別如何跳出循環(huán)的相關(guān)資料,需要的朋友可以參考下2024-01-01Bootstrap編寫一個(gè)同時(shí)適用于PC、平板、手機(jī)的登陸頁面
這篇文章主要為大家詳細(xì)介紹了Bootstrap編寫一個(gè)同時(shí)適用于PC、平板、手機(jī)的登陸頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06javascript jquery對(duì)form元素的常見操作詳解
下面小編就為大家?guī)硪黄猨avascript jquery對(duì)form元素的常見操作詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06