您好,登錄后才能下訂單哦!
這篇文章主要講解了javascript實(shí)現(xiàn)貪吃蛇經(jīng)典小游戲的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{ margin:0px; padding:0px; } #board{ width:300px; height:500px; border:5px solid #000; margin:50px auto 0px; position: relative; background: #cccccc91; } li:first-child{ position: absolute; width: 10px; height:10px; left: 100px; top: 200px; background:cornflowerblue; z-index: 1; } li{ position: absolute; width: 10px; height:10px; background: pink; list-style: none; } li:nth-child(2){ left: 100px; top: 210px; } li:nth-child(3){ left: 100px; top: 220px; } p{ width: 10px; height: 10px; position: absolute; box-shadow: 0px 0px 2px 2px #000; border-radius: 50%; } #menu{ width:300px; height:30px; border:5px solid #000; border-top:none; margin:0px auto; position: relative; } #menu>button{ border: 0px; width: 75px; display: block; float: left; height: 30px; cursor: pointer; outline: none; } button:nth-child(1){ background: pink; } button:nth-child(2){ background: skyblue; } button:nth-child(3){ background: orange; } button:nth-child(4){ background: rgb(25, 230, 6); } button:hover{ font-size: 18px; color: white; font-weight: bold; } dl{ width: 75px; position: absolute; right:0; bottom:30px; cursor:pointer; display: none; height: 90px; } dl>dd{ display: block; cursor:pointer; text-align: center; line-height: 30px; } dl>dd:nth-child(1){ height: 30px; background: rgba(212, 118, 9, 0.527); } dl>dd:nth-child(2){ height: 30px; background: rgb(142, 6, 253); } dl>dd:nth-child(3){ height: 30px; background: rgb(205, 9, 231); } </style> </head> <body> <div id="board"> <li></li> <li></li> <li></li> </div> <div id="menu"> <button>開始游戲</button> <button>暫停游戲</button> <button onclick="afresh()">重新開始</button> <button>游戲難度</button> <dl> <dd>菜鳥級</dd> <dd>大師級</dd> <dd>魔鬼級</dd> </dl> </div> <script> var ele = document.querySelectorAll("li") var board = document.getElementById("board") var menu = document.querySelectorAll("button") var ddDom = document.querySelectorAll("dd") var locLogLeft = [] var locLogTop = [] var dir = "up" var upLock = true; //讓定時(shí)器不能連開 var downLock = true; var leftLock = true; var rightLock = true; var level = 200; var count = 0; menu[0].onclick = function(){var result = game(dir,level);} //開始游戲 menu[1].onclick = function(){clearInterval(timer)} //暫停游戲 menu[3].onclick = function(){ //設(shè)置難度的函數(shù) count++; if(count%2!=0){ document.getElementsByTagName("dl")[0].style.display="block"} else{ document.getElementsByTagName("dl")[0].style.display="none" } ddDom[0].onclick =function() { count++ level = 200; document.getElementsByTagName("dl")[0].style.display="none" } ddDom[1].onclick =function() { count++ level = 100; document.getElementsByTagName("dl")[0].style.display="none" } ddDom[2].onclick =function() { count++ level = 50; document.getElementsByTagName("dl")[0].style.display="none" } } function afresh(){ //重新開始函數(shù) ele[0].style.left = "100px" ele[0].style.top = "200px" ele[1].style.left = "100px" ele[1].style.top = "210px" ele[2].style.left = "100px" ele[2].style.top = "220px" ele = document.querySelectorAll("li") for(var i = 3;i<ele.length;i++){ board.removeChild(ele[i]) } clearInterval(timer) locLogLeft = [] locLogTop = [] } var timer; foodFun() //游戲開始先生成一食物 document.onkeydown = function(e){ //方向控制函數(shù) var e = e||window.event; var foodDom = document.getElementsByTagName("p")[0] var ele = document.querySelectorAll("li") if(e.keyCode == 87 && ele[0].offsetTop<=ele[1].offsetTop ){ rightLock = true; leftLock = true; if(!upLock == true){ return; } upLock = false; // clearInterval(timer) game("up",level) } if(e.keyCode == 83 && ele[0].offsetTop>=ele[1].offsetTop){ rightLock = true; leftLock = true if(!downLock == true){ return; } downLock = false; // clearInterval(timer) game("down",level) } if(e.keyCode == 68 && ele[0].offsetLeft>=ele[1].offsetLeft){ upLock = true downLock = true; if(!rightLock == true){ return; } rightLock = false; // clearInterval(timer) game("right",level) } if(e.keyCode == 65 && ele[0].offsetLeft<=ele[1].offsetLeft){ upLock = true; downLock = true; if(!leftLock == true){ return; } leftLock = false; // clearInterval(timer) game("left",level) } } function game(direction,speed){ //控制蛇身轉(zhuǎn)彎函數(shù) clearInterval(timer) timer = setInterval(function(e){ var e = e||window.event; var foodDom = document.getElementsByTagName("p")[0] var ele = document.querySelectorAll("li") if(direction == "down"){ dir = "down" locLogLeft = [] locLogTop = [] for(var i=0;i<ele.length;i++){ locLogLeft[i] = ele[i].offsetLeft; locLogTop[i] = ele[i].offsetTop; } ele[0].style.top = 10+ele[0].offsetTop + "px"; for(var i=1;i<ele.length;i++){ ele[i].style.left = locLogLeft[i-1]+"px"; ele[i].style.top = locLogTop[i-1]+"px"; } } if(direction == "up"){ dir = "up" locLogLeft = [] locLogTop = [] for(var i=0;i<ele.length;i++){ locLogLeft[i] = ele[i].offsetLeft; locLogTop[i] = ele[i].offsetTop; } ele[0].style.top = ele[0].offsetTop - 10 + "px"; for(var i=1;i<ele.length;i++){ ele[i].style.left = locLogLeft[i-1]+"px"; ele[i].style.top = locLogTop[i-1]+"px"; } } if(direction == "left"){ dir = "left" locLogLeft = [] locLogTop = [] for(var i=0;i<ele.length;i++){ locLogLeft[i] = ele[i].offsetLeft; locLogTop[i] = ele[i].offsetTop; } ele[0].style.left = ele[0].offsetLeft - 10 + "px"; for(var i=1;i<ele.length;i++){ ele[i].style.left = locLogLeft[i-1]+"px"; ele[i].style.top = locLogTop[i-1]+"px"; } } if(direction=="right"){ dir = "right" locLogLeft = [] locLogTop = [] for(var i=0;i<ele.length;i++){ locLogLeft[i] = ele[i].offsetLeft; locLogTop[i] = ele[i].offsetTop; } ele[0].style.left = 10+ele[0].offsetLeft + "px"; for(var i=1;i<ele.length;i++){ ele[i].style.left = locLogLeft[i-1]+"px"; ele[i].style.top = locLogTop[i-1]+"px"; } } isLost() if(parseInt(foodDom.style.left) == ele[0].offsetLeft && parseInt(foodDom.style.top) == ele[0].offsetTop ){ board.removeChild(foodDom) foodFun() var snakeBody = document.createElement("li") board.appendChild(snakeBody) ele = document.querySelectorAll("li") // console.log(ele,ele.length) ele[ele.length-1].style.left = ele[ele.length-2].offsetLeft+"px"; ele[ele.length-1].style.top = ele[ele.length-2].offsetTop+"px"; } },speed) } function foodFun(e){ //生成食物函數(shù) var e = e||window.Element; var foodColorR = Math.floor((Math.random()*256)) var foodColorG = Math.floor((Math.random()*256)) var foodColorB = Math.floor((Math.random()*256)) var randomFoodX = Math.floor(Math.random()*(board.clientWidth/10)-1) var randomFoodY = Math.floor(Math.random()*(board.clientHeight/10)-1); var food = document.createElement("p") board.appendChild(food) var foodDom = document.getElementsByTagName("p")[0] foodDom.style.left=(randomFoodX+1)*10 +"px" foodDom.style.top=(randomFoodY+1)*10 +"px"; foodDom.style.backgroundColor = "rgb("+foodColorR+","+foodColorG+","+foodColorB+")" if(parseInt(foodDom.style.left) == ele[0].offsetLeft && parseInt(foodDom.style.top) == ele[0].offsetTop ){ foodFun() } } function isLost(){ //判定勝負(fù) ele = document.querySelectorAll("li") if(ele[0].offsetLeft<0 || ele[0].offsetLeft>= board.clientWidth || ele[0].offsetTop<0||ele[0].offsetTop+10>board.clientHeight){ bump = false; alert("你失敗了!") afresh() } for(var i=1;i<ele.length;i++){ if(ele[0].offsetLeft == ele[i].offsetLeft && ele[0].offsetTop == ele[i].offsetTop){ alert("你失敗了!") afresh() } } } </script> </body> </html>
看完上述內(nèi)容,是不是對javascript實(shí)現(xiàn)貪吃蛇經(jīng)典小游戲的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。