Snake or nib or tron

This is really starting to bother me...

How would I move a character across the screen with the arrow keys and continously keep moving in that direction until another character is pressed. I can't figure it out. I would rather not get a link to a page with a snake game code on it.

Here is some code from Game Tutorials that uses a Windows Console. Obviously this can be used in windows.

Code:

BOOL MovePlayer(CHAR_INFO screenBuffer[], COORD *playerPos)
{
INPUT_RECORD InputRecord; // Here is our structure to hold the information on the input buffer (what the user does)
COORD oldPosition = *playerPos; // This stores the old player positive so we can erase it when we move.
DWORD Events=0; // We create DWORD to be compatible with ReadConsoleInput(). (Holds how many input events took place)
HANDLE hInput; // This will be used to query the keyboard
BOOL bPlayerMoved = FALSE; // This holds if the player move or not
int bKeyDown = 0; // This tells us if we pressed DOWN a key, not UP too
hInput = GetStdHandle(STD_INPUT_HANDLE); // Here we initialize our input handle "hInput" A HANDLE could be many things so we specify
// Read in the input from the user, storing the information into the InputRecord structure.
ReadConsoleInput(hInput, &InputRecord, 1, &Events);
// Now we check if there was a keyboard event.
// For Windows 95 and 98, we don't need this, but for NT based systems, it handles
// the console functions different. In Windows 95 and 98, it only registers a key
// when the key is pressed down, where NT OS's register the key up and key down.
// This makes the player go 2 moves every time, so we need to make sure we just
// move the player when the key is DOWN. We use the bKeyDown variable stored in the
// input record, then just make sure it equals "true" when a key is hit.
// Notice the "&& bKeyDown" in the following if statement. That says that if the key
// was pressed, we only want to enter that if statement if it was a key down press.
bKeyDown = InputRecord.Event.KeyEvent.bKeyDown;
if(InputRecord.EventType == KEY_EVENT && bKeyDown)
{ // If the user hit the keyboard:
// Check to see if the user hit the RIGHT arrow key
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
if(playerPos->X < (SCREEN_WIDTH - 1) ) // Check to make sure our new position doesn't go outside of our buffer
{
playerPos->X++; // Increase the player's X value because we are going RIGHT.
bPlayerMoved = TRUE; // Set the flag to TRUE that the player moved
}
}
// Check to see if we hit the LEFT arrow key
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
if(playerPos->X > 0) // Check to make sure our new position doesn't go outside of our buffer
{
playerPos->X--; // Increase the player's X value because we are going LEFT.
bPlayerMoved = TRUE; // Set the flag to TRUE that the player moved
}
}
// Lets check if we hit the UP arrow:
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
{
if(playerPos->Y > 0) // Check to make sure our new position doesn't go outside of our buffer
{
playerPos->Y--; // Increase the player's X value because we are going UP.
bPlayerMoved = TRUE; // Set the flag to TRUE that the player moved
}
}
// Let's check if we hit the DOWN
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
{
if(playerPos->Y < (SCREEN_HEIGHT - 1) ) // Check to make sure our new position doesn't go outside of our buffer
{
playerPos->Y++; // Increase the player's X value because we are going DOWN.
bPlayerMoved = TRUE; // Set the flag to TRUE that the player moved
}
} // If we hit ESCAPE
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
{
exit(0); // Quit the program
}
}
FlushConsoleInputBuffer(hInput); // This clears the input buffer each time so it doesn't echo.
// If the player moved
if(bPlayerMoved)
{
// Here we check for collision with a wall. If our new positive is the same X and Y
// as a wall, we want to set the characters position back to it's old position.
// Otherwise, if it isn't a wall, then we want to erase the old position with a space
// If we didn't hit a wall, erase the old position
if(screenBuffer[playerPos->X + playerPos->Y * SCREEN_WIDTH].Char.AsciiChar != WALL)
{
// Erase the old position of the player with a space
screenBuffer[oldPosition.X + oldPosition.Y * SCREEN_WIDTH].Char.AsciiChar = ' ';
screenBuffer[oldPosition.X + oldPosition.Y * SCREEN_WIDTH].Attributes = 0;
}
else // We hit a wall
{
// Set the players position back to it's old position (Hence, we collided)
*playerPos = oldPosition;
}
return TRUE; // Return true to say we need to redraw the screen
}
return TRUE; // We didn't move so return false
}

It is pretty well documented, but it basically gets the current key and then determines which arrow was hit or if escape was hit and then follows the appropiate command.

- Sean

If cities were built like software is built, the first woodpecker to come along would level civilization.Black Frog Studios