While making a asteroid shooter, I came around using _kbhit()
and kbhit()
. I'm no expert, but here is the problem I think I'm having:
int run = 1;
int main() {
while(run){
if(GetUserInput() == 2)
printf("W");
if(GetUserInput() == 1)
printf("S");
Sleep(50);
}
}
int GetUserInput(){
if(kbhit()){
char c = _getch();
if(c == 's')
return 2;
if(c == 'w')
return 1;
}
else
return 0;*
}
So, what I think is happening, it does the first check on GetUserInput()
, and because of the nature of getch()
, the keyboard is read from the buffer and discarded? Anyways, I store the value in c
and should return appropriately. But it only does the first check. Is it because there is no more input on the buffer after the first check (in the main()
function)?