Let's say I've two ways of writing a piece of code:
Way 1:
int TailX[100];
int TailY[100];
int prevX = TailX[0];
int prevY = TailY[0];
TailX[0] = x;
TailY[0] = y;
int prev2X, prev2Y;
for(int i=1; i< nTail; i++){
prev2X = TailX[i];
prev2Y = TailY[i];
TailX[i] = prevX;
TailY[i] = prevY;
prevX = prev2X;
prevY =prev2Y;
}
Way 2:
int TailX[100];
int TailY[100];
TailX[0] = x;
TailY[0] = y;
int prevX = TailX[0];
int prevY = TailY[0];
int prev2X, prev2Y;
for(int i=1; i< nTail; i++){
prev2X = TailX[i];
prev2Y = TailY[i];
TailX[i] = prevX;
TailY[i] = prevY;
prevX = prev2X;
prevY =prev2Y;
}
I am trying to design a snake game and this chunk of the code is to store the coordinates of the previous block so as to follow it. The above code (Way 1) is working properly whereas the latter code (Way 2) is resulting in the block after the head-block to be attached to its right side while the snake is moving vertically up or down.