Solved

Keyboard input for a simple game

Posted on 2003-11-28
1
2,623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you searching for a fun but also medium paced MMORPG game I would highly suggest Ragnarok Online. This MMORPG is a bit older than World of Warcraft and in a way is extremely different. Compared to WoW's 3D environment and massive wor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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