food.js
//自调用函数--食物的
(function () {
var elements = [];//保存每个小方块食物
//食物就是一个对象,有宽、高、颜色、横纵坐标
function Food(width, height, color, x, y) {
this.x = x || 0;
this.y = y || 0;
this.width = width || 20;
this.height = height || 20;
this.color = color || "green";
}
//为原型添加初始化方法
//在页面上显示这个食物--要多次调用
//因为食物要在地图上显示,所以,需要地图的这个参数
Food.prototype.init = function (map) {
//先删除这个小食物--外部无法访问这个函数
remove();
//创建food
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);
//再从elements中删除这个子元素
elements.splice(i, 1);
}
}
//局部-->全局
window.Food = Food;
}());
//外部测试
/* var fd=new Food();
fd.init(document.querySelector(".map"));
console.log(fd.x+"===>"+fd.y);
console.log(fd.x);*/
snake.js
//自调用函数--小蛇的
(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];
//创建div
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;//2
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];
//把最后的蛇尾复制一个,重新的加入到小蛇的body中
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];
ele.parentNode.removeChild(ele);
elements.splice(i, 1);
}
}
//局部-->全局
window.Snake = Snake;
}());
game.js
//自调用函数--游戏对象
(function () {
var that;
//游戏的构造函数
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) {
console.log(headX);
//撞墙了,停止定时器
clearInterval(timeId);
alert("游戏结束");
}
//纵坐标
if (headY < 0 || headY >= maxY) {
//撞墙了,停止定时器
clearInterval(timeId);
alert("游戏结束");
}
//console.log(headX+"====>"+maxX);
}.bind(that), 200);
/*bind(that) 将函数里的this指向that*/
};
//添加原型方法--设置用户按键,改变小蛇移动的方向
Game.prototype.bindKey = function () {
document.addEventListener("keydown", function (e) {
//这里的this应该是触发keydown的事件的对象---document,
//所以,这里的this就是document
//获取按键的值
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;
}());
贪吃蛇.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>贪吃蛇</title>
<style>
.map {
width: 800px;
height: 400px;
background-color: gray;
position: relative;
}
</style>
</head>
<body>
<!--画出地图,设置样式-->
<div class="map"></div>
<script src="food.js"></script>
<script src="snake.js"></script>
<script src="game.js"></script>
<script>
//初始化游戏对象
var game = new Game(document.querySelector(".map"));
//开始游戏
game.init();
//外部测试
/* var fd=new Food();
fd.init(document.querySelector(".map"));
var snake=new Snake();
snake.init(document.querySelector(".map"));//先在地图上看到小蛇
setInterval(function () {
snake.move(fd,document.querySelector(".map"));//走一步
snake.init(document.querySelector(".map"));//初始化--重新画蛇(先删除原来的,再显示现在的)
},200);*/
</script>
</body>
</html>