Need help with snake JS game

Hey Guy’s I’m trying to write a function to check if the snake has hit itself. This is the second time I’ve made this and I’m closely following the first version I made. In that version I used a for loop to check every part of the snake array and compare it to the head position. Here’s the first version http://jsfiddle.net/zd8v7xch/ I thought I would just copy the same function from the first one and use it in this one. However when I put it in my new version the snake doesn’t move. It looks like this is happening because the head position is the same as the first element in the snake array, so it thinks the head collided with head. This confuses me because I don’t have this problem in the first version?(first question) Can someone explain why that is? Anyway I set the for loop to skip over the first element so i could get the snake moving again, only to have it stop as soon as the snake hits a food item?(second question) second version-> http://jsfiddle.net/zd8v7xch/1/

1 Like

Mostly this has to do with how you’re invoking the game as a whole. Think about what happens when you first load the page, and what your variables are at that time.

If you’re stuck, consider this: What would you do if you wanted to add a title screen that displayed before the game started?

In fact, you can skip checking the first three elements. A challenge to you: figure out the logic of why I can make that statement.

Consider how you’re making the snake longer, and why that doesn’t work with your collision code. Consider how you can change your snake-gets-longer code so that it WILL work.

I find out like one of the previous problems I had that you helped me with; It had to do with the order of my program.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.