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
Solved

Keyboard input for a simple game

Posted on 2003-11-28
1
2,621 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Article by: Joseph
CONTENTS I.      What is a MOBA?   a. Definition   b. Origin   c. Current Generation II. The New Games   a. League of Legends   b. Heroes of Newerth   c. Dota 2   d. Smite III. Comparison IV. Conclusion ------------------------…
See the related links at the bottom of the tutorial for downloads and supplemental information. Introduction The Nvidia 3D Vision Kit is a [not so] new gaming technology which allows you to play the newest computer games in "Stereo 3D" (like …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

765 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