1

I'm making a text-based game. I'm trying to have a little "health bar". When the user hit space, and when the button is pressed it will make health go up by 5, but while this is also happening I need something to be running in the background that is doing the same as if it was a AI, except I want it to subtract 1 every time and for it to only hit it once every second.

I tried getch(); the most but the problem with that is that it stops everything and waits for the user to use a input so nothing can be going on in the background that is subtracting health.

If you could use the _getch(); command in any way to accomplish this, because I already know how to use it well.

Community
  • 1
  • 1
iiZ
  • 59
  • 1
  • 1
  • 4
  • why don't you use time() function from time.h for a 1 second break? – anshabhi Dec 15 '15 at 07:12
  • 2
    There are many ways to handle this, but most of the time it's all handled with platform-specific functions, and as you don't say which platform you are on it's going to be very hard to give you a good answer.. However one thing that might work is using the ["new" C++11 thread functionality](http://en.cppreference.com/w/cpp/thread). – Some programmer dude Dec 15 '15 at 07:14
  • I don't really understand what you're saying, but if you need to check/measure time, `std::chrono` is your friend. – MatthewRock Dec 15 '15 at 07:36
  • Possible duplicate of [C non-blocking keyboard input](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input) – Werner Henze Dec 15 '15 at 09:48
  • The type of code that you're writing is called multithreaded - which is a big step forward from the simple game app that you're describing. If you want to go this route, there is a nice tutorial at http://www.bogotobogo.com/cplusplus/multithreading_win32A.php – bob Dec 16 '15 at 13:29

0 Answers0