Question

else if(po==true){
        snake_array.reverse();
        var i=0;
        var c=snake_array[i];
        //paints head
        paint_head(c.x,c.y);
        for(i=1;i<snake_array.length;i++){
            //paints body
            var c=snake_array[i];
            paint_body(c.x,c.y);
            if(snake_array[i].x<snake_array[i-1].x){
                d="right";
            }
            else if(snake_array[i].x>snake_array[i-1].x){
                d="left";
            }
            else if(snake_array[i].y>snake_array[i-1].y){
                d="up";
            }
            else if(snake_array[i].y<snake_array[i-1].y){
                d="down";
            }
        }
        //poison status false
        po=false;
    }

po is the variable for poison status.. whenever the snake eats this food. it should be reversed, going back to its previous trail.. yes, the snake reverts, but the problem is the snake did not go back to its previous trail., but opposite to the previous direction before it ate the poison...

      x  <-food
      ^  <-head            v  <-tail                v <-tail
      |                    |                        | 
      |   >-becomes->      |    >-instead of->      |
>.....|             .......|                  <.....|
^                   |                         ^ 
|_tail              V  <-head                 |_head  
Was it helpful?

Solution

You are basing your new direction on the tail rather than the head. d is set on every iteration of the loop, which means the only one that matters is the last. So since your tail is facing down in the example, d is set to down.

You either need to examine the tail before the reverse, and then reverse d or examine the head instead of the tail.

Assuming the snake is at least length 2, do the following, outside of the loop:

if(snake_array[0].x<snake_array[1].x){
    d="left";  // left or right depending on how your coordinate system works
} ...

This will just look at the facing of the head. You would probably want a check to see if the snake is only length 1, and then just reverse your current direction.

OTHER TIPS

The problem here is, how are you determining the direction of the head? And where is the tail drawn? Depending on your answers, this might be due to different problems. It seems to me that to know the proper direction (d) for the tail, you need to store not only all the coordinates of the present segments of the snake, but the last position occupied and now freed as well. Do you keep it in the array as well? If that is the case, then the .reverse() function will give you a wrong result, because it will put the head not in place of the tail, but in place of the point BEFORE the tail, that is the point the tail occupied in the previous turn.

Another thing that could be failing here is your updating mechanism. Maybe after the reversal, there's an update so that the head automatically advances by one space (but does so in the wrong direction)? Again, we need to know more of your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top