Solved

Keyboard input for a simple game

Posted on 2003-11-28
1
2,617 Views
Last Modified: 2013-11-12
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)
{
  switch(key)
  {
  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
}

//<<<<<<<<<<<<<<<<<<<<<Game Loop>>>>>>>>>>>>>>>>>>
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
     BallOut();
     
     //Check Paddle-Ball Collision
     Check_Paddle_Ball_Collision();
     
     //Check Brick-Ball Collision
....
0
Comment
Question by:bbaisley
1 Comment
 

Accepted Solution

by:
DeathCheese earned 50 total points
ID: 9853611
It sounds like you are starving the window's message queue with your while(PLAYING) loop.  What's probably happening is glut isn't able to check the message queue until the ball goes out of play and you leave the function containing the while(PLAYING) loop.  Then it  checks the message queue, getting all the keyboard input and calling your keyboard function.

If you aren't already, register the idle callback function and put your game logic (no rendering) in it.  The idle function is automatically called by glut every frame, I believe.  Then you can have code that might look like this:

void IdleFunc(...)
{
    switch(game_state)
    {
        ...
        // any other game states
        ...
        case PLAYING:
        {
            // BallOut, Collision, etc
        }
        ....
    }
}

That way, your code will be run just once per frame, and then glut can continue and do what it needs to do, checking the message queue and gathering keyboard input among other things.

Hope that helped!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: ErwindeVries
Released on the 7th of September this real time strategy game sets a new bar for any real time strategy game. R.U.S.E. is a game made by Eugen Systems and published by Ubisoft and released for the PC, Playstation 3 and the Xbox 360. The setting o…
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now