I'm trying to make a shooting video game in conole mode but the game pauses to recieve an input from the key board. Ho can I get around this? How do game engines work? DFor exaple in a game like pac-man where the ghosts and other elements in the game work in real-time independently of the player's input, how can I achiev this in console mode?

Recommended Answers

All 12 Replies

>Ho can I get around this?
You'll have to learn about working with multiple threads or multiple processes. That's a rather broad and complicated topic for a forum post though, try google.

commented: Very helpful, he gave the perfect solution! +1

You don't need threads, just a means of reading all your input device(s) without blocking.
For example, this.

You need to get familiar with event-driven programming.

SDL is a good place to start.

Running two functions at once is possible by using multi-threading as was pointed out but it is generally used for far more complicated things..

What is SDL exactly? (If I come across like a Newbie I am I just started C++, and don't know very much.)

SDL = http://www.libsdl.org/

But before you get bogged down in specific APIs, I would suggest you get familiar with the core language.

>Ho can I get around this?
You'll have to learn about working with multiple threads or multiple processes. That's a rather broad and complicated topic for a forum post though, try google.

So if I don't need to use multithreading how can I get around having to wait for an input from the user? Any help will be greatly appreciated. (^ - ^)

>So if I don't need to use multithreading how can I get
>around having to wait for an input from the user?
If you need to do something that would be accomplished by multithreading, why do you think you don't need multithreading? You could spawn multiple processes, but I get the feeling you don't "need" that either. This leaves you with polling for input at regular intervals (ala. kbhit) and actually pulling the input with a non-blocking read (ala. getch). But that's some seriously convoluted logic in all but the simplest of cases, and in the end you're essentially simulating threads.

> how can I get around having to wait for an input from the user?
By using a function which tests to see if input is available, without actually blocking.

Pseudo-code. A rather abstract (and simplified) view of the main game loop. If you want to change while ( playerStillAlive ) to while ( nLives > 0 ) , that's easily done.

do {
  if ( keyboardHasInput() ) {
    key = readKeyboard();
    updatePlayer(key,player);
  }
  playerStillAlive = updateMonsters(player,monsters);
  if ( playerStillAlive ) {
    drawScreen(player,monsters);
  }
} while ( playerStillAlive );
drawDeathScreen();

Sorry for the really late reply, but I looked at your suggestion and I was wondering, how would I check to see if an input is available? Please post code. One more thing, is there a way to avail an input to the user for a limited amount of time? One last thing are you familiar with threads?

Hey Narue, thanks for the advice on threads, it help a whole lot!

what about a simple nested while loop?

what about a simple nested while loop?

Yes I thought about that, but I need to be able to have an enemy attacking the player without the console waiting for an input from the player.

Right now my code is:

//For the monster//

while (not hit) 
{
Move left or right;
Fire if you feel like it;
}

//For the player//
Read the player's input;
Execute command;

And I wanted the monster's activity to be independent of the player's input.
By the way do you know how to allot a certain amount of time for the player's input to be accepted?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.