What i am trying to do is first store a number of values in a 2D array with a constant of only 3 columns. The first column represents x values, the second column y values, and he third is the symbol to be printed. I then print this array on the screen, which results in a snake like character. Now i want the snake to move right when i press the right arrow key, so using a switch statement first an then a for loop, i print a space in the last cell of the array representing the tail of the snake, shift all the cells down through another for loop, change the coordinates of the first position of the array, and print a new character at that position. This should increment each time, but instead it only moves one segment of the snake and doesn't even print the space in the right position, here is my code, this is just a segment of a larger function, but right now im just focusing on the right key press:
bool move( int &xPos, int &yPos, int xVect, int yVect )
{
// initialize function/variables
bool moved = false;;
int arr[25][10];
int index = 0, index2 = 0;
int loc, userInput;
int length = 4;
//clears out array
for( index =0; index <= 25; index++ )
{
for( index2 =0; index2 <= 10; index2++ )
{
arr[index][index2] = 0;
}
}
// prints original snake on screen and stores array values
for( index = length; index >= 0; index-- )
{
for( loc = SCRN_MAX_Y/2+length; loc >= SCRN_MAX_Y/2; loc-- )
{
arr[index][0] = SCRN_MAX_X/2;
arr[index][1] = loc;
arr[index][2] = MARKER_CHAR;
printCharAt( arr[index][0], arr[index][1], arr[index][2] );
}
}
do
{
userInput = waitForInput( FIXED_WAIT );
switch( userInput )
{
case KB_RIGHT_ARROW:
{
for( index = length; index >= 0; index-- )
{
waitForInput(1);
// prints a space in tail position
arr[length][2] = SPACE;
printCharAt( arr[0][0], arr[length][1], arr[length][2] );
// shifts array down
for( index=0; index <= length; index++ )
{
for( index2 = 0; index2 <= 2; index2++ )
{
arr[index + 1][index2] = arr[index][index2];
}
}
// prints a charater at head position
arr[0][0] += xVect;
arr[0][2] = MARKER_CHAR;
printCharAt( arr[0][0], arr[0][1], arr[0][2] );
}
moved = true;
}
break;
Am i not storing the arrays correctly? is it not remembering the original array values when i try to move the snake? any help would be greatly appreciated. Thankyou.