如何解决为什么我的 SetInterval 开始无限循环
因此,此代码用于制作蛇游戏,我正在使用 setInterval 使用画布制作一些动画。现在,当我尝试这样做时,它会出现无限循环的错误。我不知道为什么或如何解决这个问题。
代码如下:
let
canvas = document.getElementById("canvas"),ctx = canvas.getContext("2d"),width = canvas.width,height = canvas.height,blockSize = 10,widthInBlocks = width / blockSize,heightInBlocks = height / blockSize,score = 0;
// Code for the border -- or would we use `.strokeRect`?
ctx.fillStyle = "Gray"
ctx.fillRect(0,blockSize,height);
ctx.fillRect(0,width,blockSize);
ctx.fillRect(0,height - blockSize,blockSize);
ctx.fillRect(width - blockSize,height);
// Code for tracking score
ctx.font = "20px Courier";
ctx.fillStyle = "Black"
ctx.textAlign = "left"
ctx.textBaseline = "top"
ctx.fillText("score: " + score,blockSize)
var gameOver = function() {
clearInterval();
ctx.font = "60px Courier";
ctx.fillStyle = "Black"
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Game Over",width / 2,height / 2);
}
var gameOver = function() {
clearInterval(intervalId);
ctx.font = "60px Courier";
ctx.textAlign = "center";
ctx.textBaseline = "middle"
ctx.fillText("Game over",height / 2);
}
var circle = function(x,y,radius,fillCircle) {
ctx.beginPath();
ctx.arc(x,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(color) {
var x = this.col * blockSize;
var y = this.row * blockSize;
ctx.fillStyle = color;
ctx.fillRect(x,blockSize);
}
Block.prototype.drawCircle = function(color) {
var centerX = this.col * blockSize + blockSize / 2;
var centerY = this.row * blockSize + blockSize / 2;
ctx.fillStyle = color;
circle(centerX,centerY,blockSize / 2,true);
}
var Snake = function() {
this.segments = [
new Block(7,5),new Block(6,new Block(5,5)
];
this.direction = "right"
this.nextDirection = "right"
}
Snake.prototype.draw = function() {
for (var i = 0; i < this.segments.length; i++)
this.segments[i].drawSquare("Blue")
}
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 wallCollision = leftCollision || topCollision ||
rightCollision || bottomCollision;
var selfCollision = false
for (var i = 0; i < this.segments.legnth; i++) {
if (head.equal(this.segments.length[i])) {
selfCollision = true;
}
}
return wallCollision || selfCollision;
}
Snake.prototype.setDirection = function(newDirection) {
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("Red");
}
Apple.prototype.move = function () {
var randomCol = Math.floor(Math.random() * (widthInBlocks - 2)) + 1
var randomrow = Math.floor(Math.random() * (heightInBlocks - 2)) + 1
this.position = new Block(randomCol,randomrow);
}
var snake = new Snake();
snake.draw()
var apple = new Apple();
apple.draw()
var intervalId = setInterval(function () {
ctx.clearRect(0,height);
drawscore();
snake.move();
snake.draw();
apple.draw();
drawBorder();
},100)
var directions = {
37: "left",38: "up",39: "right",40: "down"
}
$("body").keydown(function (event) {
var newDirection = directions[event.keyCode];
if (newDirection !== undefined) {
snake.setDirection(newDirection);
}
})
有什么问题吗? 第 170 行出错。 我不知道该怎么做,也不知道错误意味着什么。请有人帮助我
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。