C++/c++ input check
I am a beginner programmer and I've been trying to code a small game (something similar to PONG). In the game loop, I've done something like this:
if(direction=='character') //several conditions and events
//Output here...just basic C++ graphics based on the events above.
My problem is that my game keeps waiting for a keystroke once it gets to that if(kbhit). I would like the output to be performed independent of input. (example: if I don't move the paddle, the ball should still move).
Do you have any solutions I might use? Please keep in mind the fact that I am a beginner.
If you need any more explanations of what I am trying to do, please ask and I could even pass the CPP file to you for a more detailed inspection (if you consider the problem to be coming from something other than the if(kbihit) ).
Thank you in advance for your time!
Alex, the problem is that
does not call the kbhit function, it simply returns the address of the function, which is not zero, and therefore true. You need to add the parenthesis.
Here is the program. I did it on windows, so I use the windows specific
function to slow it down. If you're using linux, you want the unix
Please read my comments in the program.
#include <Windows.h> // For the Sleep function
int done = 0; // or bool done = false
while(!done) // not exit, exit is a function
printf("Waiting for kbhit\n");
if(kbhit()) // not if(kbhit), you need the parentheses to make the function call
if(direction=='x') // Only 1 character can be in single quotes.