<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wjz</title>
<style>
*{
margin:0;
padding:0;
}
.map{
width:800px;
height:600px;
background-color: #ccc;
position: relative;
}
</style>
</head>
<body>
<div class="map"></div>
<script src="js/common.js"></script>
<script >
//自调用函数
(function(){
var elements=[];//用来保存每个小方块实物的
////实物就是一个对象,有宽和高和颜色还有横纵坐标,先定义构造函数
function Food(x,y,height,width,color){
this.x=x||0;
this.y=y||0;
this.width=width||20;
this.height=height||20;
this.color=color||"pink";
}
//为原型添加初始化的方法。作用页面显示实物
//因为实物要在地图显示有map参数
Food.prototype.init=function(map){
//先删除这个小食物
remove();
//创建div
var div=document.createElement("div");
//吧div加到map中
map.appendChild(div);
//设置div的样式
div.style.width=this.width+"px";
div.style.height=this.height+"px";
div.style.backgroundColor=this.color;
//先脱离文档流
div.style.position="absolute";
//随机横纵坐标
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";
//把div键入到数组elements中
elements.push(div);
};
//私有的函数--删除实物
function remove(){
//elements数组中有这个食物
for(var i=0;i<elements.length;i++){
var ele=elements[i];
//找到这个元素的父辈元素然后删除这个元素
ele.parentNode.removeChild(ele);
//再次把elelments中的这个元素也要删除
elements.splice(i,1);
}
}
//把Food暴露给window,外部可以使用
window.Food=Food;
}());
//自调用函数--食物
(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();
//循环遍历创建div
for(var i=0; i<this.body.length;i++){
//数组中的每个数组元素都是一个对象
var obj=this.body[i];
var div=document.createElement("div");
//把div加入在map中
map.appendChild(div);
div.style.position="absolute";
div.style.width=this.width+"px";
div.style.height=this.height+"px";
//横纵坐标
div.style.left=obj.x*this.width+"px";
div.style.top=obj.y*this.height+"px";
//背景颜色
div.style.backgroundColor=obj.color;
//方向暂时不定
//把div加入到elements数组中--目的是为了删除
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){
//获取小蛇的最后的尾巴
var last=this.body[this.body.length-1];
//最后的蛇尾复制一份加入到身体当中
this.body.push({
x:last.x,
y:last.y,
color:last.color
});
//把食物删除,重新初始化食物
food.init(map);
}
};
//删除小蛇的私有函数
function remove(){
//获取数组
var i=elements.length-1;
for(;i>=0;i--){
//先从当前的子元素找到父辈元素,然后删除子元素
var ele=elements[i];
//从map中删除这个子元素
ele.parentNode.removeChild(ele);
elements.splice(i,1);
}
}
//把snake暴露给weindow,外部可以访问
window.Snake=Snake;
}());
//自调用函数--游戏对象
(function(){
var that=null;
//游戏的构造函数
function Game(map){
this.food=new Food();//食物的对象
this.snake=new Snake();//小蛇的对象
this.map=map;//地图
that=this;
}
Game.prototype.init=function(){
//初始化游戏
//食物初始化
this.food.init(this.map);
//小蛇初始化
this.snake.init(this.map);
//条用自动移动小蛇的方法
this.runSnake(this.food,this.map);
//调用按键的方法
this.bindKey();//========================
};
//添加原型方法--设置小蛇可以自动跑起来
Game.prototype.runSnake=function(food,map){
//自由的移动
var timeId=setInterval(function(){
//此时的this是window
//移动小蛇
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("难受呀马飞");
}
//纵坐标判断
if(headY<0||headY>=maxY){
//撞墙了
clearInterval(timeId);
alert("难受呀马飞");
}
console.log(maxX+"===="+this.snake.body[0].x);
console.log(this.snake.body[0].x);
console.log(maxY);
console.log(this.snake.body[0].y);
}.bind(that),150)
};
//添加原型方法--设置用户按键,改变小蛇的方向
Game.prototype.bindKey=function(){
//获取用户的按键,改变小蛇的移动方向
document.addEventListener("keydown",function(e){
//这里的this是document,应该改为触发事件keyDown的事件的对象
//获取键盘的按键值
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 gm=new Game(document.querySelector(".map"));
//初始化游戏--开始游戏
gm.init();
//外部测试代码
// var fd=new Food();
// var snake=new Snake();
// snake.init(document.querySelector(".map"));//先看到小蛇
// setInterval(function(){
// snake.move(fd,document.querySelector(".map"));//走一步
// snake.init(document.querySelector(".map"));//初始化重新画一条蛇
// },150);
// console.log(fd.x+"====>"+fd.y);
// console.log(fd.width);
</script>
</body>
</html>
|