#贪吃蛇思路
首先由四大步
1.首先有个地图
2.一个食物对象
3.一个蛇对象
4.游戏
#1.地图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 地图的样式 */
.map {
position: relative;
width: 800px;
height: 600px;
background: #cccccc
}
</style>
</head>
<body>
<!-- 地图 -->
<div class="map"></div>
</body>
</html>
2.食物
// 获取地图
var map = document.querySelector(".map");
//自定义构造函数
(function () {
//声明一个空数组 用来存储食物
var element = [];
// 写一个食物构造函数 参数有 x,y,width,height,color
function Food(x, y, width, height, color) {
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) {
this.remove();
// 动态创建一个div
var cib = document.createElement("div");
cib.style.position = "absolute";
cib.style.width = this.width + "px";
cib.style.height = this.height + "px";
cib.style.backgroundColor = this.color;
this.x = Math.floor(Math.random() * (map.offsetWidth / this.width)) * this.width;
this.y = Math.floor(Math.random() * (map.offsetHeight / this.height)) * this.height;
cib.style.left = this.x + "px";
cib.style.top = this.y + "px";
map.appendChild(cib);
element.push(cib);
}
Food.prototype.remove = function () {
for (var i = 0; i < element.length; i++) {
var ele=element[i];
ele.parentNode.removeChild(ele);
element.splice(i,1)
}
};
// 暴露
window.Food=Food;
})();
var food=new Food();
food.init(map);
3.小蛇
(function () {
var element = [];
function Snake(width, height, dirsction) {
this.width = width || 20;
this.height = height || 20;
this.dirsction = dirsction || "right";
this.body = [{
x: 3,
y: 2,
color: "red"
},
{
x: 2,
y: 2,
color: "orange"
},
{
x: 1,
y: 2,
color: "orange"
}
];
};
Snake.prototype.init = function () {
this.remove();
for (var i = 0; i < this.body.length; 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 = this.body[i].color;
div.style.left = this.body[i].x * this.width + "px";
div.style.top = this.body[i].y * this.height + "px";
map.appendChild(div);
element.push(div)
}
};
Snake.prototype.move = function (food, map) {
for (var i = this.body.length - 1; i > 0; i--) {
this.body[i].x = this.body[i - 1].x;
this.body[i].y = this.body[i - 1].y;
}
switch (this.dirsction) {
case "top":
this.body[0].y -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "bottom":
this.body[0].y += 1;
break;
case "left":
this.body[0].x -= 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)
}
};
Snake.prototype.remove = function () {
for (var i = element.length - 1; i >= 0; i--) {
var ele = element[i];
ele.parentNode.removeChild(ele);
element.splice(i, 1);
}
};
window.Snake = Snake;
})();
var snake = new Snake();
snake.init(map)
4.游戏
(function () {
var element = [];
function Snake(width, height, dirsction) {
this.width = width || 20;
this.height = height || 20;
this.dirsction = dirsction || "right";
this.body = [{
x: 3,
y: 2,
color: "red"
},
{
x: 2,
y: 2,
color: "orange"
},
{
x: 1,
y: 2,
color: "orange"
}
];
};
Snake.prototype.init = function () {
this.remove();
for (var i = 0; i < this.body.length; 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 = this.body[i].color;
div.style.left = this.body[i].x * this.width + "px";
div.style.top = this.body[i].y * this.height + "px";
map.appendChild(div);
element.push(div)
}
};
Snake.prototype.move = function (food, map) {
for (var i = this.body.length - 1; i > 0; i--) {
this.body[i].x = this.body[i - 1].x;
this.body[i].y = this.body[i - 1].y;
}
switch (this.dirsction) {
case "top":
this.body[0].y -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "bottom":
this.body[0].y += 1;
break;
case "left":
this.body[0].x -= 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);
}
};
Snake.prototype.remove = function () {
for (var i = element.length - 1; i >= 0; i--) {
var ele = element[i];
ele.parentNode.removeChild(ele);
element.splice(i, 1);
}
};
window.Snake = Snake;
})();
// var snake = new Snake();
// snake.init(map)
(function () {
var that = null;
function Game(map) {
this.snake = new Snake();
this.food = new Food();
this.map = map;
that = this;
};
Game.prototype.init = function () {
this.snake.init(this.map);
this.food.init(this.map);
this.runSnake(this.food, this.map);
this.bindKey();
};
Game.prototype.runSnake = function (food, map) {
var timerId = 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(timerId);
alert("游戏结束")
}
if (headY < 0 || headY >= maxY) {
clearInterval(timerId);
alert("游戏结束")
}
}.bind(that), 300)
};
Game.prototype.bindKey = function () {
document.addEventListener("keydown", function (e) {
switch (e.keyCode) {
case 37:
this.snake.dirsction = "left";
break;
case 38:
this.snake.dirsction = "top";
break;
case 39:
this.snake.dirsction = "right";
break;
case 40:
this.snake.dirsction = "bottom";
break;
}
}.bind(that), false)
};
window.Game = Game;
})();
var game = new Game(map);
game.init();