Keyboard input for a simple game
Posted on 2003-11-28
Hello. For a class project I am doing a simple 3D game using C++ and OpenGl.
So far, I am happy with the progress. It basically works the way I expect. As of now, I can use the "K","L" keys to move a paddle across the across the screen. The problem I am having is when the game is actually started, the user pushes "S" to start the game, it goes into a while loop and doesn't exit until the ball goes out of play.
This works fine, but while the loop is executing, the keyboard function doesn't work, therefore it doesn't last long as I cannot move the paddle.
It seems like after the loop exits, it stored all of the keyboard inputs, because then the paddle is drawn in the correct position. Its just not working while the loop is active.
Is there a way to temporarily break from the loop so as to check the keyboard function? If not, I need to find a way to force the system to check for keystrokes every run through the loop.
Here is some code if this helps.
//<<<<<<<<<<<<<<<<<<<<<<<< myKeyboard >>>>>>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int x, int y)
case 'l': movePlayer( 0.3); break; //slide paddle right
case 'k': movePlayer(-0.3); break; // slide paddle left
case 's': startGame(); break; // start the game
case 'r': resetGame(); break; // reset the game
glutPostRedisplay(); // draw it again
while (game_state == PLAYING && player.lives > 0)
//I think i need to call Move Paddle here, but i am not sure how.
// Check to see if ball is falling off bottom of table
//Check Paddle-Ball Collision
//Check Brick-Ball Collision