Every time you call getch
, you're waiting for a character of input from the user. If you want to move left, you'd actually have to press the key four times.
The fix is simple - only call getch()
once:
if (kbhit()) {
char keystroke = getch();
if (keystroke == 'w'){PlayerX = PlayerX - 1;}
else if (keystroke == 's'){PlayerX = PlayerX +1;}
else if (keystroke == 'd'){PlayerY = PlayerY +1;}
else if (keystroke == 'a'){PlayerY = PlayerY - 1;}
}
Incidentally, you had an extra pair of parenthesis around each getch() call. All you need is getch()
, not (getch())
.