JS貪喫蛇小遊戲:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
.map {
margin: 100px auto;
width: 600px;
height: 400px;
background-color: #CCC;
position: relative;
overflow: hidden;
}
</style>
</head>
<body>
<div class="map" id="map"></div>
<script>
//創建food
(function () {
//Food對象
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
var elements = [];//儲存food
Food.prototype.init = function (map) {
this.removeFood();
var fdiv = document.createElement("div");
map.appendChild(fdiv);
fdiv.style.position = "absolute";
//隨機位置
this.x = parseInt(Math.random() * (map.offsetWidth / this.width));
this.y = parseInt(Math.random() * (map.offsetHeight / this.height));
fdiv.style.left = this.x * this.width + "px";
fdiv.style.top = this.y * this.height + "px";
fdiv.style.width = this.width + "px";
fdiv.style.height = this.height + "px";
fdiv.style.backgroundColor = this.color;
elements.push(fdiv);
};
//移除已經喫過的Food
Food.prototype.removeFood = function () {
while (elements.length) {
elements[0].parentNode.removeChild(elements[0]);
elements.shift();
}
};
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},
{x: 2, y: 2},
{x: 1, y: 2}
];
this.direction = direction || 2;
}
//顯示小蛇
Snake.prototype.init = function (map) {
this.removeSnake();
for (var i = 0; i < this.body.length; i++) {
var snakeBody = this.body[i];
var sObj = document.createElement("div");
map.appendChild(sObj);
if (i == 0) {
sObj.style.backgroundColor = "red";
} else {
sObj.style.backgroundColor = "yellow";
}
sObj.style.width = this.width + "px";
sObj.style.height = this.height + "px";
sObj.style.position = "absolute";
sObj.style.left = snakeBody.x * this.width + "px";
sObj.style.top = snakeBody.y * this.height + "px";
elements.push(sObj);//儲存進數組
}
};
//刪除顯示
Snake.prototype.removeSnake = function () {
while (elements.length) {
elements[0].parentNode.removeChild(elements[0]);
elements.shift();
}
};
//小蛇移動
Snake.prototype.move = function (map, food) {
if (food.x == this.body[0].x && food.y == this.body[0].y) {
this.eat(map, food);
}
var newHead = JSON.parse(JSON.stringify(this.body[0]));//深度copy
switch (this.direction) {
case 0:
newHead.x -= 1;
break;
case 1:
newHead.y -= 1;
break;
case 2:
newHead.x += 1;
break;
case 3:
newHead.y += 1;
break;
}
this.body.unshift(newHead);
this.body.pop();
newHead = null;
this.init(map);
};
//喫食物
Snake.prototype.eat = function (map, food) {
var newEnd = JSON.parse(JSON.stringify(this.body[this.body.length - 1]));
this.body.push(newEnd);
newEnd = null;
food.init(map);
};
window.Snake = Snake;
}());
//遊戲
(function () {
function Game(map) {
this.food = new Food(0, 0, 20, 20, "green");
this.snake = new Snake(20, 20, 2);
this.map = map;
that = this;
}
Game.prototype.init = function () {
this.food.init(this.map);
this.snake.init(this.map);
this.runSnake();
this.bindKey();
};
Game.prototype.runSnake = function () {
var timeId = setInterval(function () {
if (!this.gameOver()) {
clearInterval(timeId);
alert("GAME OVER");
return false;
}
this.snake.move(this.map, this.food);
}.bind(that), 150);
};
//判斷遊戲是否結束
Game.prototype.gameOver = function () {
if (!(this.snake.body[0].x >= 0 && this.snake.body[0].x < this.map.offsetWidth / this.snake.width && this.snake.body[0].y >= 0 && this.snake.body[0].y < this.map.offsetHeight / this.snake.height)) {
return false;
}
for (var i = 1; i < this.snake.body.length; i++) {
console.log(this.snake.body[0] + "=====" + this.snake.body[i]);
if (this.snake.body[0].x == this.snake.body[i].x && this.snake.body[0].y == this.snake.body[i].y) {
return false;
}
}
return true;
};
//綁定按鍵
Game.prototype.bindKey = function () {
document.addEventListener("keydown", function (e) {
if (e.keyCode >= 37 && e.keyCode <= 40 && (this.snake.direction + 2) % 4 != e.keyCode - 37) {
this.snake.direction = e.keyCode - 37;
}
}.bind(that), false)
};
window.Game = Game;
}());
//初始化遊戲
var game = new Game(document.getElementById("map"));
game.init();
</script>
</body>
</html>