<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#map {
width: 800px;
height: 600px;
background-color: lightgreen;
margin: 50px auto;
position: relative;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
/*
分析:食物是一个对象,小蛇也是一个对象,最后组成游戏对象
*/
// 一、食物对象
((function() {
// 1.自定义食物的构造函数
function Food(width, height, color, x, y) {
this.width = width || 20;
this.height = height || 20;
this.color = color || "pink";
this.x = x || 0;
this.y = y || 0;
this.element = document.createElement("div");
}
// 2.在原型对象上初始化食物
Food.prototype.init = function(map) {
// 设置小方块的样式
var div = this.element;
div.style.width = this.width + "px";
div.style.height = this.height + "px";
div.style.backgroundColor = this.color;
div.style.position = "absolute";
// 设置随机坐标
this.x = Math.floor(Math.random() * (map.offsetWidth / this.width)) * this.width;
this.y = Math.floor(Math.random() * (map.offsetHeight / this.height)) * this.height;
// 设置小方块的left和top值
this.element.style.left = this.x + "px";
this.element.style.top = this.y + "px";
// ② 把小方块添加到map中
map.appendChild(div);
}
// 3.Food暴露给window
window.Food = Food;
})());
// 二、小蛇对象
((function() {
var elements = [];// 存放蛇的部位
// 1.自定义小蛇的构造函数
function Snake(width, height, direction) {
// 小蛇每一个部分的宽高
this.width = width || 20;
this.height = height || 20;
// 方向
this.direction = direction || "right";
// 小蛇的身体
this.body = [
{x: 3, y: 1, color: "red"},// 小蛇的头
{x: 2, y: 1, color: "orange"},// 小蛇的身体
{x: 1, y: 1, color: "orange"}// 小蛇的身体
]
}
// 2.在原型对象上初始化小蛇
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.style.width = this.width + "px";
div.style.height = this.height + "px";
div.style.backgroundColor = obj.color;
div.style.position = "absolute";
// 设置坐标
div.style.left = obj.x * this.width + "px";
div.style.top = obj.y * this.height + "px";
map.appendChild(div);
// 把蛇添加到数组里--->目的:为了删除
elements.push(div);
}
}
// 3.移动小蛇的方法
Snake.prototype.move = function(food, map) {
// 改变小蛇身体的坐标,小蛇的头部去判断方向
for(var i = this.body.length - 1; i > 0; i--) {
// 当i = 2时,让第三块坐标x = 第二块坐标x
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++;
break;
case "left":
this.body[0].x--;
break;
case "top":
this.body[0].y--;
break;
case "bottom":
this.body[0].y++;
break;
}
// 判断小蛇是否吃到食物--->即判断头部坐标和食物的坐标一致
// 头部的坐标
var headX = this.body[0].x * this.width;
var headY = this.body[0].y * this.height;
// 食物的坐标
var foodX = food.x;
var foodY = food.y;
if(headX == foodX && headY == foodY) {
// 追加一个蛇的身体到body最后
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() {
// 先删尾巴
for(var i = elements.length - 1; i >= 0; i--) {
// 数组中的每一个部位
var ele = elements[i];
// 在map中删除div
ele.parentNode.removeChild(ele);
// 删数组的div
elements.splice(i, 1);
}
}
window.Snake = Snake;
})());
// 三、游戏对象
((function() {
var that;
// 1.自定义游戏的构造函数
function Game(map) {
this.food = new Food();// 食物对象
this.snake = new Snake();// 小蛇对象
this.map = map;
that = this;
}
// 2.初始化游戏
Game.prototype.init = function() {
this.food.init(this.map);
this.snake.init(this.map);
// 调用
this.runSnake(this.map);
// 调用按键方法
this.bindKey();
}
// 3.添加方法使小蛇自动跑起来
Game.prototype.runSnake = function(map) {
var timeId = setInterval(function() {
that.snake.move(that.food, that.map);
that.snake.init(map);
// 判断横纵坐标的最大最小值
var maxX = map.offsetWidth / that.snake.width;
var maxY = map.offsetHeight / that.snake.height;
// 蛇头的坐标
var headX = that.snake.body[0].x;
var headY = that.snake.body[0].y;
if(headX < 0 || headX >= maxX || headY < 0 || headY >= maxY) {
// 清除定时器
clearInterval(timeId);
alert("你可真是菜死了!");
}
}, 200)
}
// 4.设置用户的按键,来改变蛇移动的方向
Game.prototype.bindKey = function() {
document.addEventListener("keydown", function(e) {
// console.log(e.keyCode);
switch(e.keyCode) {
case 37:
that.snake.direction = "left";
break;
case 38:
that.snake.direction = "top";
break;
case 39:
that.snake.direction = "right";
break;
case 40:
that.snake.direction = "bottom";
break;
}
})
}
window.Game = Game;
})());
var map = document.getElementById("map");
var game = new Game(map);
game.init();
</script>
</body>
</html>