一、body部分
<style>
#box{
width: 800px;
height: 600px;
background-color: #999;
position: relative;
}
</style>
</head>
<script src="./贪吃蛇js/食物.js"></script>
<script src="./贪吃蛇js/蛇.js"></script>
<script src="./贪吃蛇js/游戏.js"></script>
<body>
<!-- 新建一个盒子用来做背景 -->
<div id="box"></div>
</body>
<script>
var map =document.getElementById('box')
var game = new Game(new Food(20, 20, 'blue', 'absolute', 0, 0, []),
new Snake(20, 20, 'absolute', 'right', []),
map
)
game.renderGame()
二、食物部分
function Food(width, height, bgColor, position, left, top, foodArr) {
this.width = width;
this.height = height;
this.bgColor = bgColor;
this.position = position;
this.left = left;
this.top = top;
this.foodArr =foodArr;
}
Food.prototype.renderFood = function () {
var foodBox = map.appendChild(document.createElement("div"));
foodBox.style.width = this.width + "px";
foodBox.style.height = this.height + "px";
foodBox.style.backgroundColor = this.bgColor;
foodBox.style.position = this.position;
this.left =
parseInt(Math.random() * (map.clientWidth / this.width)) * this.width;
this.top =
parseInt(Math.random() * (map.clientHeight / this.height)) * this.height;
foodBox.style.left = this.left + "px";
foodBox.style.top = this.top + "px";
this.foodArr.push(foodBox);
};
Food.prototype.deleteFood = function () {
for (var i = this.foodArr.length - 1; i >= 0; i--) {
map.removeChild(this.foodArr[i]);
this.foodArr.splice(i, 1);
}
};
三、蛇部分
function Snake(width, height, position, direction, snakeArr) {
this.width = width;
this.height = height;
this.position = position;
this.direction = direction;
this.body = [
{
x: 2,
y: 1,
color: 'yellow'
},
{
x: 1,
y: 1,
color: 'red'
},
{
x: 0,
y: 1,
color: 'red'
}
]
this.snakeArr = snakeArr
}
Snake.prototype.renderSnake = function () {
for (var i = 0; i < this.body.length; i++) {
var snakeBox = map.appendChild(document.createElement('div'))
snakeBox.style.width = this.width + 'px';
snakeBox.style.height = this.height + "px";
snakeBox.style.position = this.position;
snakeBox.style.left = this.body[i].x * this.width + 'px';
snakeBox.style.top = this.body[i].y * this.height + 'px';
snakeBox.style.backgroundColor = this.body[i].color;
this.snakeArr.push(snakeBox)
}
console.log(this.snakeArr);
}
Snake.prototype.moveSnake = function (food) {
console.log('蛇移动了');
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.direction) {
case 'left':
this.body[0].x--
break;
case 'right':
this.body[0].x++
break;
case 'top':
this.body[0].y--
break;
case 'bottom':
this.body[0].y++
break;
default:
break;
}
var snakeLeft = this.body[0].x * this.width
var snakeTop = this.body[0].y * this.height
console.log(food.left);
if (food.left == snakeLeft && food.top == snakeTop) {
console.log('重合了');
this.body.push({
x: this.body[this.body.length - 1].x,
y: this.body[this.body.length - 1].y,
color: this.body[this.body.length - 1].color
})
this.renderSnake()
food.deleteFood()
food.renderFood()
}
}
Snake.prototype.deleteSnake = function () {
for (var i = this.snakeArr.length - 1; i >= 0; i--) {
map.removeChild(this.snakeArr[i])
this.snakeArr.splice(i, 1)
}
console.log(this.snakeArr);
}
四、游戏的主体部分
function Game(food, snake, map) {
this.food = food;
this.snake = snake;
this.map = map;
}
Game.prototype.renderGame = function () {
this.food.renderFood()
this.snake.renderSnake()
this.startGame()
this.directionSnake()
}
Game.prototype.startGame = function () {
console.log('游戏开始');
var timer = setInterval(function () {
console.log('定时器');
var maxX = map.clientWidth / this.snake.width - 1
var maxY = map.clientHeight / this.snake.height - 1
var x = this.snake.body[0].x
var y = this.snake.body[0].y
if (x > 0 && x < maxX && y > 0 && y < maxY) {
this.snake.moveSnake(this.food)
this.snake.renderSnake()
} else {
clearInterval(timer)
alert('Game over')
}
}.bind(this), 150)
}
Game.prototype.directionSnake = function () {
var that = this;
console.log(that);
document.onkeyup = function () {
var event = event || window.event
console.log(event.keyCode);
switch (event.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;
default:
break;
}
}
}