Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.
Comments
:
If all you want to do is to read a single keypress, you can use the getch() function, that's in
: :
:
: If all you want to do is to read a single keypress, you can use the getch() function, that's in
:
:
What you need is a keyboard function lib. It replaces the old key handler and reads keystrokes without waiting for a return key. Most game libs have one. The problem with getch() is that it waits for a key instead of just checking and continuing.
: : :
: :
: : If all you want to do is to read a single keypress, you can use the getch() function, that's in
: :
: :
: What you need is a keyboard function lib. It replaces the old key handler and reads keystrokes without waiting for a return key. Most game libs have one. The problem with getch() is that it waits for a key instead of just checking and continuing.
:
:
I know that. I just wanted to give him a simpler answer, after all, he's doing an RPG, not an action game.
I might know of a way that might help you.
:
:
Have you checked out the keyboard libraries that use direct scanning yet? It's the only way to doit!
if you can't find one I'll send you one.
mail me: etlusk@woodstock.com
BOOL MovePlayer(CHAR_INFO screenBuffer[], COORD *playerPos)
{
INPUT_RECORD InputRecord;
COORD oldPosition = *playerPos;
DWORD Events=0;
HANDLE hInput;
BOOL bPlayerMoved = FALSE;
hInput = GetStdHandle(STD_INPUT_HANDLE);
ReadConsoleInput(hInput, &InputRecord, 1, &Events);
if(InputRecord.EventType == KEY_EVENT)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
if(InputRecord.Event.KeyEvent.bKeyDown)
{
if(playerPos->X < (SCREEN_WIDTH - 1))
{
playerPos->X++;
bPlayerMoved = TRUE;
}
}
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
if(InputRecord.Event.KeyEvent.bKeyDown)
{
if(playerPos->X > 0)
{
playerPos->X--;
bPlayerMoved = TRUE;
}
}
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
{
if(InputRecord.Event.KeyEvent.bKeyDown)
{
if(playerPos->Y > 0)
{
playerPos->Y--;
bPlayerMoved = TRUE;
}
}
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
{
if(InputRecord.Event.KeyEvent.bKeyDown)
{
if(playerPos->Y < (SCREEN_HEIGHT - 1))
{
playerPos->Y++;
bPlayerMoved = TRUE;
}
}
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
{
printf("The end!");
sleep(2000);
exit(0);
}
}
FlushConsoleInputBuffer(hInput);
if(bPlayerMoved)
{
/* Collision Detection goes here */
If(screenBuffer[playerPos->X + playerPos->Y * SCREEN_WIDTH].Char.AsciiChar == WALL)
{
*playerPos = oldPosition;
}
}
return TRUE;
}
Check it out, although it uses console functions most of the Maze tutorial.
:
Just as somewhat of a more mundane and theoretical suggestion, wouldn't it be a little more convenient to use the keypad for these movements? Of course, the numLock should be turned off, but I think that this would be a better utilization of the entire keyboard, especially if the characters in the game have the opportunity to speak. Just a thought.
- InterGeek
We are the music makers, and we are the dreamers of dreams. Willy Wonka