I would suggest getting the coordinates of the front of the snake’s head every time it moves (perhaps every frame).
Then use snakeHead.style.display = 'none';
temporarily hide the snake’s head (you should see it again before rendering)
Then document.elementFromPoint(x, y) to check which element is directly under the front of the snake’s head. Perhaps add a class called collidable or something to all the elements that cause collision and validation
var class = ' '+document.elementFromPoint(x, y).className+' '; if(class.indexOf(' collidable ') > -1){ alert('Collision!'); }
Then, of course, look at the head of the snake:
snakeHead.style.display = 'whatever is was before probably inline';
Just an idea.
elementFromPoint has good browser compatibility: http://www.quirksmode.org/dom/w3c_cssom.html#documentview
Let me know how this happens :)
source share