js實(shí)現(xiàn)貪吃蛇小游戲(容易理解)
話不多說(shuō),請(qǐng)看代碼:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>貪吃蛇</title> <link rel="stylesheet" href="style.css"> <script src="style.js" ></script> </head> <body> <div id="container"></div> </body> </html>
第一步:初始化地圖,創(chuàng)建蛇圈。
第二步:創(chuàng)建蛇,隨機(jī)生產(chǎn)食物。
第三步:讓蛇移動(dòng)起來(lái)。
第四步:通過(guò)js綁定鍵盤(pán)事件,控制蛇移動(dòng)方向。
var box={width:50,height:50};//每一個(gè)方塊的高度
var snake=[];//保存蛇每一節(jié)身體對(duì)應(yīng)的span
var DIR={
DIR_RIGHT:1,
DIR_LEFT:2,
DIR_TOP:3,
DIR_BOTTOM:4
};
var dir=DIR.DIR_BOTTOM;
var food=null; //始終記錄當(dāng)前的食物
window.onload=function(){
//1.初始化地圖
initMap();
//2.創(chuàng)建蛇
//2.5隨機(jī)顯示食物
showFood();
createSnake();
//3.讓蛇動(dòng)起來(lái)
setInterval(snakeMove,100);
//4.控制蛇移動(dòng)
document.onkeyup=function(e){
switch(e.keyCode){
case 37:dir=DIR.DIR_LEFT;break;
case 38:dir=DIR.DIR_TOP;break;
case 39:dir=DIR.DIR_RIGHT;break;
case 40:dir=DIR.DIR_BOTTOM;break;
}
}
};
function isInSnakeBody(left,top){
for(var i=0;i<snake.length;i++){
if(snake[i].offsetTop==top&&snake[i].offsetLeft==left){
return true;
}
}
}
//這種隨機(jī)生成食物的方法效率低---隨著蛇身體的增長(zhǎng),隨機(jī)生成食物的時(shí)間會(huì)變慢。
function showFood(){
var con=document.getElementById("container");
food=document.createElement("span");
food.className="food";
food.style.width=box.width+"px";
food.style.height=box.height+"px";
var left,top;
do{
left=Math.floor((con.offsetWidth-2)/box.width*Math.random())*box.width;
top=Math.floor((con.offsetHeight-2)/box.height*Math.random())*box.height;
}while(isInSnakeBody(left,top));
food.style.left=left+"px";
food.style.top=top+"px";
con.appendChild(food);
}
function initMap(){
var con=document.getElementById("container");
var row=Math.floor(con.offsetWidth/box.width);
var rol=Math.floor(con.offsetHeight/box.height);
var num=row*rol;
var newSpan=null;
for(var i=1;i<=num;i++){
newSpan=document.createElement("span");
newSpan.style.width=box.width+"px";
newSpan.style.height=box.height+"px";
con.appendChild(newSpan);
}
}
function createSnake(){
var newBody=null;
var con=document.getElementById("container");
for(var i=1;i<=5;i++){
newBody=document.createElement("span");
newBody.style.width=box.width+"px";
newBody.style.height=box.height+"px";
newBody.style.left=(i-1)*box.width+"px";
newBody.style.top="0px";
newBody.className="snake";
con.appendChild(newBody);
snake.push(newBody);
}
}
function snakeMove(){
var con=document.getElementById("container");
//蛇頭移動(dòng)
var head=snake[snake.length-1];
var newTop=head.offsetTop,newLeft=head.offsetLeft;
switch(dir){
case DIR.DIR_LEFT:newLeft-=box.width; break;
case DIR.DIR_RIGHT:newLeft+=box.width; break;
case DIR.DIR_TOP:newTop-=box.height; break;
case DIR.DIR_BOTTOM:newTop+=box.height; break;
default:break;
}
//如果超出邊界,計(jì)算蛇頭下一個(gè)位置的坐標(biāo)
if(newLeft>con.offsetWidth-2-1){newLeft=0;}
if(newLeft<0){newLeft=con.offsetWidth-2-box.width;}
if(newTop<0){newTop=con.offsetHeight-2-box.height;}
if(newTop>con.offsetHeight-2-1){newTop=0;}
//判斷新蛇頭的位置是不是在蛇身體里面
//for(var i=0;i<snake.length-1;i++){
// if(snake[i].offsetLeft==newLeft&&snake[i].offsetTop==newTop){
// alert("Game over!!");
// window.location.href=window.location.href;
// }
//}
//1.如果吃到食物
if(newLeft==food.offsetLeft&&newTop==food.offsetTop){
food.className="snake";
snake.push(food);
showFood();
return;
}
//2.如果沒(méi)吃到
//除蛇頭外身體移動(dòng)
for(var i=0;i<snake.length-1;i++){
snake[i].style.top=snake[i+1].offsetTop+"px";
snake[i].style.left=snake[i+1].offsetLeft+"px";
}
head.style.left=newLeft+"px";
head.style.top=newTop=newTop+"px";
}
*{
padding:0;
margin:0;
}
html,body{
width:100%;
height:100%;
}
body{
position:relative;
}
div#container{
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
width:800px;
height:500px;
border:1px solid black;
font-size:0px;
}
span{
display:inline-block;
border:1px solid black;
box-sizing:border-box;
}
span.snake{
position:absolute;
background-color:red;
}
span.food{
position:absolute;
background-color:blue;
}
最后的效果圖如下:


以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
從setTimeout看js函數(shù)執(zhí)行過(guò)程
這篇文章主要介紹了從setTimeout看js函數(shù)執(zhí)行過(guò)程,需要的朋友可以參考下2017-12-12
uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)
隨著UniApp的流行,越來(lái)越多的開(kāi)發(fā)者選擇使用它來(lái)構(gòu)建跨平臺(tái)應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)的相關(guān)資料,需要的朋友可以參考下2023-06-06
JavaScript實(shí)現(xiàn)封裝一個(gè)快速生成目錄樹(shù)的全局腳本
目錄樹(shù)可以很好的介紹項(xiàng)目中各文件目錄的用途,幫助讀者了解整個(gè)項(xiàng)目結(jié)構(gòu)。本文就來(lái)用JavaScript封裝一個(gè)快速生成目錄樹(shù)的全局腳本,希望對(duì)大家有所幫助2023-03-03
js使用正則實(shí)現(xiàn)ReplaceAll全部替換的方法
JS?沒(méi)有提供replaceAll這樣的方法。使用正則表可以達(dá)成Replace?的效果,感興趣的朋友看看下面的示例2014-08-08
認(rèn)識(shí)Knockout及如何使用Knockout綁定上下文
Knockout簡(jiǎn)稱(chēng)ko,是一個(gè)輕量級(jí)的javascript類(lèi)庫(kù),采用MVVM設(shè)計(jì)模式(即Model、view、viewModel),簡(jiǎn)單優(yōu)雅的實(shí)現(xiàn)了雙向綁定,實(shí)時(shí)更新,幫助您使用干凈的數(shù)據(jù)模型來(lái)創(chuàng)建豐富的、響應(yīng)式的用戶(hù)界面2015-12-12
JSON.stringify轉(zhuǎn)換JSON時(shí)日期時(shí)間不準(zhǔn)確的解決方法
這篇文章主要介紹了JSON.stringify轉(zhuǎn)換JSON時(shí)日期時(shí)間不準(zhǔn)確的解決方法,即JSON數(shù)據(jù)中包含日期對(duì)象時(shí),在轉(zhuǎn)換時(shí)會(huì)轉(zhuǎn)換成國(guó)際時(shí)間,而不是中國(guó)的時(shí)區(qū),需要的朋友可以參考下2014-08-08
jquery form表單獲取內(nèi)容以及綁定數(shù)據(jù)
這篇文章主要介紹了jquery form表單獲取內(nèi)容以及form表單綁定數(shù)據(jù),獲取表單的數(shù)據(jù)保存到數(shù)據(jù)庫(kù),或者將數(shù)據(jù)綁定到form表單,感興趣的小伙伴們可以參考一下2016-02-02

