I am trying to create a snake game in C++ Microsoft Visual Studio. I have created it so that you can control where the snake moves using letters, but I would prefer it use the arrow keys. Here's the code I have for the movement of the snake so far.
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = LEFT;
break;
case 'b':
dir = RIGHT;
break;
case 'c':
dir = UP;
break;
case 'd':
dir = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
the arrow keys may actually produce more than 1 character; I have forgotten exactly how they work. My advice is to write a dummy program that prints what you read back out (in hex or as integers, not chars) when you press your arrow keys, and then code around that. There may be defined constants in the win headers for the arrow keys, I don't know --- it defines a number of odd keys in there. That means you may need multiple getch calls to process the input, in a micro state machine type setup (case first letter, switch/case second letter inside type setup maybe). Youll see when you start messing with it, but treat it as if an arrow gives multiple bytes.