用javaScript实现贪吃蛇小游戏
- 首先创建一个贪吃蛇.html文件,代码如下
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
.map {
width: 800px;
height: 600px;
background-color: #CCC;
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 gm = new Game(document.querySelector(".map"));
gm.init();
</script>
</body>
</html>
- 其次创建一个food.js的文件,代码如下
(function () {
var elements = [];
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) {
remove();
var div = document.createElement("div");
map.appendChild(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";
elements.push(div);
};
function remove() {
for (var i = 0; i < elements.length; i++) {
var ele = elements[i];
ele.parentNode.removeChild(ele);
elements.splice(i, 1);
}
}
window.Food = Food;
}());
- 再创建一个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();
for (var i = 0; i < this.body.length; i++) {
var obj = this.body[i];
var div = document.createElement("div");
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;
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];
ele.parentNode.removeChild(ele);
elements.splice(i, 1);
}
}
window.Snake = Snake;
}());
- 最后创建一个Game.js的文件,代码如下
(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.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("游戏结束");
}
}.bind(that), 300);
};
Game.prototype.bindKey=function () {
document.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;
}());
- 最后,运行结果如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c2ca07e418cbb64ab651a1ae94b75399.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/220540f69f28050947a4fbd0a45a784f.png)