javascript實現(xiàn)貪吃蛇小游戲思路
javascript小游戲貪吃蛇實現(xiàn)思路講解(完整代碼實現(xiàn)),供大家參考,具體內(nèi)容如下
效果流程
1、首先我們要操作的canvas
<!doctype html> <html> <head> <meta charset="utf-8"> <title>貪吃蛇</title> </head> <body> <canvas id="canvas"></canvas> <!-- 我們要操作的canvas --> <input type="button" value="開始游戲" /><!-- 開始游戲按鈕 --> <script> //獲取元素 var canvas = document.getElementById("canvas"); //找到我們要操作的canvas var context = canvas.getContext("2d"); //規(guī)定在canvas上操作的環(huán)境為2d var but = document.getElementsByTagName("input")[0]; //找到開始按鈕 </script>
2、在初始化
canvas.width = 500; //定義canvas寬度 canvas.height = 500; //定義canvas高度 canvas.style.border = "5px solid #000"; //定義canvas邊框 var times = 100; //默認時間200毫秒 var long = 10; //蛇身相對于步長的個數(shù) var x = y =8; //蛇的初始坐標 var direction = 3; // 1 上 2 右 3 下 0 左 var size = 8; //蛇每次移動的長度(步長) var map = []; //用來記錄蛇每次移動的坐標 var foodx = 0; //食物的初始X軸坐標 var foody = 0; //食物的初始y軸坐標 var onOff = true; var foodT = true; var timer = null;
3、根據(jù)方向控制蛇的坐標變化,判斷蛇的坐標是否超出canvas邊界,判斷蛇有沒有碰到自己
//根據(jù)方向控制蛇的坐標變化 switch(direction){ case 1: y = y - size; break; //上 case 2: x = x + size; break; //右 case 3: y = y + size; break; //下 case 0: x = x - size; break; //左 } //判斷蛇的坐標是否超出canvas邊界,超出則出現(xiàn)碰壁提示,游戲結(jié)束 if(x>500 || x<0 || y>500 || y<0){ // alert("你碰壁掛了!繼續(xù)努力吧……"); window.location.reload(); } //判斷蛇有沒有碰到自己,碰到自己游戲結(jié)束 for(var i=0; i<map.length; i++){ if(parseInt( map[i].x ) == x && parseInt( map[i].y ) == y){ // alert("你碰到自己掛了!繼續(xù)努力吧……"); window.location.reload(); //重新載入 } }
4、然后繪制蛇
//繪制蛇 if(map.length>long){ var cl = map.shift(); context.clearRect(cl['x'],cl['y'],size,size); } map.push({'x':x,'y':y}); context.fillStyle = "#777"; //填充蛇的顏色 context.fillRect(x,y,size,size); //繪制蛇
5、判斷食物坐標等于蛇的坐標時(蛇吃掉食物)
//判斷食物坐標等于蛇的坐標時(蛇吃掉食物) if(foodx*8 == x && foody*8 == y ){ food(); //再次繪制食物 long++; //蛇的長度增加 times = times - 10; //速度加快 clearInterval(timer); onOff = true; setTimeout(goto,times); //開始新的一輪 }
6、確定食物隨機顯示坐標,繪制食物
//確定食物隨機顯示坐標,繪制食物 function food(){ foodx = Math.ceil(Math.random()*40); //食物的X軸隨機坐標 foody = Math.ceil(Math.random()*40); //食物的Y軸隨機坐標 context.fillStyle = "mediumvioletred"; //食物的填充顏色 context.fillRect(foodx*8,foody*8,8,8); //繪制食物 }
7、監(jiān)聽按下方向鍵,獲得蛇前進的方向
//監(jiān)聽按下方向鍵,獲得蛇前進的方向 document.onkeydown = function(ev){ var ev = ev || event; var cod = ev.keyCode - 37; switch(cod){ case 1: direction = 1; break; //向上 case 2: direction = 2; break; //向右 case 3: direction = 3; break; //向下 case 0: direction = 0; break; //向左 } }
完整代碼實現(xiàn)
<!doctype html> <html> <head> <meta charset="utf-8"> <title>貪吃蛇</title> </head> <body> <canvas id="canvas"></canvas> <!-- 我們要操作的canvas --> <input type="button" value="開始游戲" /><!-- 開始游戲按鈕 --> <script> //獲取元素 var canvas = document.getElementById("canvas"); //找到我們要操作的canvas var context = canvas.getContext("2d"); //規(guī)定在canvas上操作的環(huán)境為2d var but = document.getElementsByTagName("input")[0]; //找到開始按鈕 //初始化 canvas.width = 500; //定義canvas寬度 canvas.height = 500; //定義canvas高度 canvas.style.border = "5px solid #000"; //定義canvas邊框 var times = 100; //默認時間200毫秒 var long = 10; //蛇身相對于步長的個數(shù) var x = y =8; //蛇的初始坐標 var direction = 3; // 1 上 2 右 3 下 0 左 var size = 8; //蛇每次移動的長度(步長) var map = []; //用來記錄蛇每次移動的坐標 var foodx = 0; //食物的初始X軸坐標 var foody = 0; //食物的初始y軸坐標 var onOff = true; var foodT = true; var timer = null; function star(){ //根據(jù)方向控制蛇的坐標變化 switch(direction){ case 1: y = y - size; break; //上 case 2: x = x + size; break; //右 case 3: y = y + size; break; //下 case 0: x = x - size; break; //左 } //判斷蛇的坐標是否超出canvas邊界,超出則出現(xiàn)碰壁提示,游戲結(jié)束 if(x>500 || x<0 || y>500 || y<0){ // alert("你碰壁掛了!繼續(xù)努力吧……"); window.location.reload(); } //判斷蛇有沒有碰到自己,碰到自己游戲結(jié)束 for(var i=0; i<map.length; i++){ if(parseInt( map[i].x ) == x && parseInt( map[i].y ) == y){ // alert("你碰到自己掛了!繼續(xù)努力吧……"); window.location.reload(); //重新載入 } } //繪制蛇 if(map.length>long){ var cl = map.shift(); context.clearRect(cl['x'],cl['y'],size,size); } map.push({'x':x,'y':y}); context.fillStyle = "#777"; //填充蛇的顏色 context.fillRect(x,y,size,size); //繪制蛇 //判斷食物坐標等于蛇的坐標時(蛇吃掉食物) if(foodx*8 == x && foody*8 == y ){ food(); //再次繪制食物 long++; //蛇的長度增加 times = times - 10; //速度加快 clearInterval(timer); onOff = true; setTimeout(goto,times); //開始新的一輪 } } //確定食物隨機顯示坐標,繪制食物 function food(){ foodx = Math.ceil(Math.random()*40); //食物的X軸隨機坐標 foody = Math.ceil(Math.random()*40); //食物的Y軸隨機坐標 context.fillStyle = "mediumvioletred"; //食物的填充顏色 context.fillRect(foodx*8,foody*8,8,8); //繪制食物 } //開始游戲 function goto(){ if(onOff){ timer = setInterval(star,times); onOff = false; } if(foodT){ food(); foodT = false; } } //點擊按鈕開始游戲開始 but.onclick = goto;//點擊按鈕,開始游戲 //監(jiān)聽按下方向鍵,獲得蛇前進的方向 document.onkeydown = function(ev){ var ev = ev || event; var cod = ev.keyCode - 37; switch(cod){ case 1: direction = 1; break; //向上 case 2: direction = 2; break; //向右 case 3: direction = 3; break; //向下 case 0: direction = 0; break; //向左 } } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript腳本獲取form和input內(nèi)容的方法(兩種方法)
隨著js的發(fā)展,許多的網(wǎng)頁數(shù)據(jù)處理完全可以由js腳本解決,而不需要發(fā)送到服務(wù)器,這里分享兩種Javascript腳本獲取form和input內(nèi)容的方法,感興趣的朋友跟隨小編一起看看吧2023-05-05動態(tài)加載圖片路徑 保持JavaScript控件的相對獨立性
根據(jù)新界面的要求,需要一部分圖片來增強日期控件的美觀性??紤]到既要實現(xiàn)加載圖表的目標,又要保持控件的獨立性以便將來的移植。2010-09-09非常漂亮的讓背景如此暗淡(一種彈出提示信息時頁面背景色調(diào)改變的方法)
非常漂亮的讓背景如此暗淡(一種彈出提示信息時頁面背景色調(diào)改變的方法)...2007-04-04js 截取或者替換字符串中的數(shù)字實現(xiàn)方法
下面小編就為大家?guī)硪黄猨s 截取或者替換字符串中的數(shù)字實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06