# For loop is not being called

I am working through a snake game program and have run into a really odd problem. Everything works except that the snake does not crash into itself. When the snake runs into itself the screen should turn red temporarily. The specific problem is on line 55. There is a for loop that for some reason is not running. I am really confused as I do not see the problem. Can anyone please help?

``````let numOfBlocks = 20;
let blockSize = 20;
let speedX = 0;
let speedY = 0;
let tailLength = 0;
let tailBlocks = [];
let appleX = 0;
let appleY = 0;
let game = true;

function setup() {
createCanvas(400, 400);
frameRate(10);
//start snake at center
//starting location of the apple
appleX = floor(random(0, numOfBlocks));
appleY = floor(random(0, numOfBlocks));
}

function draw() {
if (game == true) {
background("black");
// the snake
fill("white");
//the apple
fill("red");
rect(appleX * blockSize, appleY * blockSize, blockSize, blockSize);
// snake touches apple
appleX = floor(random(0, numOfBlocks));
appleY = floor(random(0, numOfBlocks));
tailLength = tailLength + 1;
}
// the tail
tailBlocks.push({
})
for (let i = 0; i < tailBlocks.length; i++) {
fill("white");
rect(tailBlocks[i].x * blockSize, tailBlocks[i].y * blockSize, blockSize, blockSize);
}
// tail cutter
while (tailBlocks.length > tailLength) {
tailBlocks.shift();
}
for (let z = 0; z < tailLength.length; z++) {
if (headX + speedX == tailBlocks[z].x && headY + speedY == tailBlocks[z].y) {
background("red");
game = false;
}

}

// the walls
}
if (headX > numOfBlocks - 1) {
}
}
if (headY > numOfBlocks - 1) {
}

}
// what happens when the game ends
if (game == false) {
background("red");

textSize(50);
text("Game Over", 70, 200);
textSize(30);
text("Try Again?", 130, 275);
if (mouseIsPressed) {
game = true;
}
}

}

function keyPressed() {
// the controls
if (key == "w" || keyIsDown(UP_ARROW)) {
speedX = 0;
speedY = -1;
}
if (key == "a" || keyIsDown(LEFT_ARROW)) {
speedX = -1;
speedY = 0;
}
if (key == "s" || keyIsDown(DOWN_ARROW)) {
speedX = 0;
speedY = 1;
}
if (key == "d" || keyIsDown(RIGHT_ARROW)) {
speedX = 1;
speedY = 0;
}

}
``````
``````for (let z = 0; z < tailLength.length; z++) {
``````

I don’t know if variable has property `.length` or not, but that’s the problem why loop doesn’t work

1 Like

It’s strange because I have done this exact code before and have ha no issues with it.

Take a closer look.

``````for (let i = 0; i < tailBlocks.length; i++) {
``````

tailBlocks is an array that has lenght property

``````for (let z = 0; z < tailLength.length; z++)
``````

tailLenght is a numeral so it hasn’t length property

What you are saying makes perfect sense. The issue that I am having is that this code is based on a tutorial; a tutorial that I completed months ago. I am trying to do it again and now it is not working. But when I compare this code to the code that I did originally, the code is the same and my old code works fine and this new attempt does not work. So I am really confused.

Sorry, sorry, sorry. I finally see it. I was using the wrong variable, which is the error that you were pointing out. I am really sorry for my error.

1 Like

It’s sometimes hard to see errors. You have an idea what you were doing and you just cannot see that code is not what you meant it to be. With practise and time you’ll learn. And there’s no reason to be sorry, when you learning 