$(document).keydown(function(e){
var key=e.which;
//to prevent reversing, add the possible value of d which hinders the snake to go to desired direction
if(key=="37"&&d!="right"){
if(p==true){
clearInterval(game_loop);
game_loop=setInterval(paint,60);
p=false;
d="left";
}
else{
d="left";
}
}
else if(key=="38"&&d!="down"){
if(p==true){
clearInterval(game_loop);
game_loop=setInterval(paint,60);
p=false;
d="up";
}
else{
d="up";
}
}
else if(key=="39"&&d!="left"){
if(p==true){
clearInterval(game_loop);
game_loop=setInterval(paint,60);
p=false;
d="right";
}
else{
d="right";
}
}
else if(key=="40"&&d!="up"){
if(p==true){
clearInterval(game_loop);
game_loop=setInterval(paint,60);
p=false;
d="down";
}
else{
d="down";
}
}
else if(key=="32"){
if(p==false){
clearInterval(game_loop);
p=true;
}
else{
game_loop=setInterval(paint,60);
p=false;
}
}
})
this code defines my keydown for my snake,. the problem is. whenever i pressed 2 keys at the same time or if i press down and left very quickly (perhaps other combinations are also possible) the game is over.,my function that checks the collision to its own body returns true after i press 2 keys quickly like down and left..
how can it be fixeD?