Сразу при начале игры она завершается, просидевши час, я ничего не добился.
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height= canvas.height;
var blockSize = 30;
var widthInBlocks = width / blockSize;
var heightInblocks = height / blockSize;
var score = 0;
var colors = ["red","orange","green","darkblue","darkred","LimeGreen"];
var color1 = colors[Math.floor(Math.random() * colors.length)];
var color2 = colors[Math.floor(Math.random() * colors.length)];
var intervalId = setInterval(function(){
ctx.clearRect(0,0,width,height);
drawScreen();
drawScore();
snake.move();
snake.draw();
apple.draw();
drawBorder();
},100);
var drawScreen = function(){
ctx.fillStyle = "black";
ctx.fillRect(0,0,width,height);
};
var drawBorder = function(){
ctx.strokeStyle = "orange";
ctx.lineWidth = 30;
ctx.strokeRect(5,5,width-10,height-10);
}
var drawScore = function(){
ctx.fillStyle = "red";
ctx.font = "40px Comic Sans MS";
ctx.fillText("Счёт: " + score,25,55);
};
var gameOver = function() {
clearInterval(intervalId);
ctx.fillStyle = "red";
ctx.font = "150px Comic Sans MS";
ctx.fillText("GAME OVER",180,300);
};
var circle = function(x,y,radius,fillCircle){
ctx.beginPath();
ctx.arc(x,y,radius,0,Math.Pi * 2,false);
if (fillCircle) {
ctx.fill();
} else {
ctx.stroke();
}
};
var Block = function(col,row) {
this.col = col;
this.row = row;
};
Block.prototype.drawSquare = function(){
var x = this.col * blockSize;
var y = this.row * blockSize;
ctx.fillStyle = color1;
ctx.fillRect(x, y, blockSize, blockSize);
};
Block.prototype.drawCircle = function(){
var CenterX = this.col * blockSize + blockSize / 2;
var CenterY = this.row * blockSize + blockSize / 2;
ctx.fillStyle = color2;
circle(CenterX,CenterY, blockSize/2, true);
};
Block.prototype.equal = function(otherBlock){ //ПРОВЕРКА НА СТОЛКНОВЕНИЕ
return this.col === otherBlock.col && this.row === otherBlock.row;
};
var Snake = function() {
this.segments = [
new Block(7,5), //Первый сегмент(голова) - Head
new Block(8,5),
new Block(9,5)
];
this.direction = "right";
this.nextDirection = "right";
};
Snake.prototype.draw = function(){
for (var i = 0; i < this.segments.length; i++) {
this.segments[i].drawSquare();
}
};
Snake.prototype.move = function(){
var head = this.segments[0];
var newHead;
this.direction = this.nextDirection;
if (this.direction === "right"){
newHead = new Block(head.col + 1, head.row);
} else if (this.direction === "down"){
newHead = new Block(head.col, head.row + 1);
} else if (this.direction === "left"){
newHead = new Block(head.col - 1,head.row);
} else if (this.direction === "up"){
newHead = new Block(head.col, head.row -1);
}
if (this.checkCollision(newHead)){
gameOver();
return;
}
this.segments.unshift(newHead); //Добавляю в начало массива новую голову
if (newHead.equal(apple.position)) {
score++;
Apple.move()
} else {
this.segments.pop();
}
};
Snake.prototype.checkCollision = function(head){
var leftCollision = (head.col === 0);
var topCollision = (head.row === 0);
var rightCollision = (head.col === widthInBlocks - 1);
var bottomCollision = (head.row === heightInblocks -1);
var wallCollosion = leftCollision || topCollision || rightCollision || bottomCollision;
var selfCollision = false;
for (var i = 0; i < this.segments.length; i++) {
if (head.equal(this.segments[i])){
selfCollision = true;
}
}
return wallCollosion || selfCollision;
};
var direction = {
37: "left",
38: "up",
39: "right",
40: "down"
}
$("body").keydown(function(event){
var newDirection = direction[event.keycode];
if (newDirection !== undefined){
Snake.setDirection(newDirection);
}
});
Snake.prototype.setDirection = function(newDirection){
if (this.direction === "up" && newDirection === "down"){
return;
} else if(this.direction === "right" && newDirection === "left"){
return;
} else if(this.direction === "down" && newDirection === "up"){
return;
} else if(this.direction === "left" && newDirection === "right"){
return;
}
this.nextDirection = newDirection;
};
var Apple = function(){
this.position = new Block(10,10);
};
Apple.prototype.draw = function() {
this.position.drawCircle();
};
Apple.prototype.move = function(){
var randomCol = Math.floor(Math.random() * (widthInBlocks -2)) +1;
var randomRaw = Math.floor(Math.random() * (heightInblocks -2)) +1;
};
var snake = new Snake();
var apple = new Apple();