<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.map {
width: 800px;
height: 600px;
background-color: gainsboro;
position: relative;
}
</style>
</head>
<body>
<div class="map"></div>
<script>
//獲取map這個對象
var map=document.querySelector(".map");
//由於要在外部調用函數,所以使用自調用使局部變為全局
//自調用函數——食物的構造函數
(function (){
var elements=[];//用於存儲小方塊
//自定義構造函數,創建food這個對象——
function Food(width,height,color){
this.width=width||20;
this.height=height||20;
this.color=color;
this.x=0;
this.y=0;
}
window.Food=Food;
//為原型對象添加方法——初始化食物的位置及顯示效果
Food.prototype.init=function (map){
//先清除小方塊
remove();
//創建並添加小方塊對象
var div=document.createElement("div");
div.style.position="absolute";
div.style.width=this.width+"px";
div.style.height=this.height+"px";
div.style.backgroundColor=this.color;
//x y為隨機數
this.x=parseInt(Math.random()*(map.offsetWidth/this.width))*this.width;
this.y=parseInt(Math.random()*(map.offsetHeight/this.height))*this.height;
div.style.left=this.x+"px";
div.style.top=this.y+"px";
//把這個小方塊添加到map中
map.appendChild(div);
elements.push(div);
};
//私有函數——寫一個用於刪除小方塊的函數——既刪除map中的也刪除數組中的
function remove(){
for(var i=0;i<elements.length;i++){
//一個小方塊對象
var ele=elements[i];
//根據小方塊對象找到map,並刪除map中的小方塊
ele.parentNode.removeChild(ele);
//再刪除數組中的小方塊對象
elements.splice(i,1);
}
}
})();
//自調用函數——蛇的構造函數
(function (){
//數組用於存儲蛇的信息
var elements=[];
function Snake(width,height,direction){
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();
//循環遍歷數組
for(var i=0;i<this.body.length;i++){
var obj=this.body[i];
//創建一個蛇的部分的對象
var div=document.createElement("div");
div.style.position="absolute";
div.style.width=this.width+"px";
div.style.height=this.height+"px";
div.style.backgroundColor=obj.color;
div.style.left=obj.x*this.width+"px";
div.style.top=obj.y*this.height+"px";
map.appendChild(div);
//把這個對象加入到數組中——為了后面用於刪除
elements.push(div);
}
};
//為原型添加方法——小蛇動起來
Snake.prototype.move=function (food,map){
//倒循環——一次從頭開始,把頭的坐標給它后面的一個
//改變蛇身體的坐標
var i=this.body.length-1;
for(;i>0;i--){
this.body[i].x=this.body[i-1].x;
this.body[i].y=this.body[i-1].y;
}
//判斷方向,改變蛇頭的位置
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;
}
//判斷小蛇的頭部坐標是否與食物一樣
var headX=this.body[0].x*this.width;
var headY=this.body[0].y*this.height;
if(headX==food.x&&headY==food.y){
//第一步——食物消失,重新出現一個新的食物
food.init(map);
//第二步——小蛇的尾巴加長——即復制了一份尾巴加到小蛇上
var snakeLast=this.body[this.body.length-1];
this.body.push({
x:snakeLast.x,
y:snakeLast.y,
color:snakeLast.color,
})
}
};
function remove(){
//獲取數組
var i=elements.length-1;
for(;i>=0;i--){
var ele=elements[i];
ele.parentNode.removeChild(ele);
elements.splice(i,1);
}
}
window.Snake=Snake;
})();
//自調用函數——游戲對象的構造函數
(function (){
var that=null;
//游戲對象的構造函數
function Game(map){
this.food=new Food(20,20,"red");
this.snake=new Snake();
this.map=map;
that=this;
}
//原型添加方法
//游戲初始化
Game.prototype.init=function (){
//食物初始化
this.food.init(this.map);
//小蛇初始化
this.snake.init(this.map);
//調用小蛇移動的方法
this.gameRun(this.food,this.map);
this.bindKey();
};
//添加原型方法——設置小蛇可以跑起來
Game.prototype.gameRun=function (food,map){
var timeId=setInterval(function (){
//小蛇移動
this.snake.move(food,map);
//小蛇初始化
this.snake.init(map);
//最大橫縱坐標
var maxX=map.offsetWidth/this.snake.width;
var maxY=map.offsetHeight/this.snake.height;
//小蛇頭的坐標
var headX=this.snake.body[0].x;
var headY=this.snake.body[0].y;
if(headX<0||headX>=maxX){
clearInterval(timeId);
alert("game over");
}
if(headY<0||headY>=maxY){
clearInterval(timeId);
alert("game over");
}
}.bind(that),150)
};
//添加原型方法——注冊按鍵點擊事件
Game.prototype.bindKey=function (){
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);
};
window.Game=Game;
}());
var game=new Game(map);
game.init();
</script>
</body>
</html>
實現思路:
1、先有一個map區域,即效果顯示的范圍;
2、用js加入一個food到map中,這個food能隨機出現在map的任意位置;
3、用js加入一個snake到map中,這是一條可以在map這個范圍內上下左右移動的蛇,當snake與food的坐標相同時,食物消失並出現一個新的食物,並且蛇的尾部會加長一個;
