I am trying to make a simple console application game in which you own a village. It's basically a Tribal Wars rip off , in case you have played it. Basically , you have mines which bring you resources and you need resources in order to build the buildings.
What I did was make an infinite while loop in which all your buildings' levels are displayed and you have to press a button ( decision=_getch()) in order to choose the building to interract with. Also , at the beginning of the while loop I set up a rate at which the resources grow (wood++ every 2 seconds and so on). The problem is that the resources do not increase every 2 seconds because the program is waiting for the user to press the key in order to interract with the building. I get one iteration , then it stops. How can I make the the std::thread function to work separately from the while loop so that the resources can continue increasing even if I haven't pressed the decision key ?
In case you did not understand , the program looks something like this :
int main ()
{
int wood=0;
char decision;
while (1)
{
wood++;
cout<<1.Building1<<endl;
cout<<2.Building2<<endl;
decision=_getch();
std::this_thread::sleep_for (std::chrono::seconds(2));
}
}
There's nothing "simple" about console applications when you want to run the program at the same time as accepting input. You will have a much easier time if you abandon the console altogether: http://www.cplusplus.com/articles/G13hAqkS/
If you really want to use the console though, you should use a dedicated library such as ncurses/pdcurses
> How can I make the the std::thread function to work separately from the while loop
> so that the resources can continue increasing even if I haven't pressed the decision key ?
Execute the periodic increment of resources asynchronously (in a separate thread).
Oh my. It took me years to get this format right, and I'm handing it out free. This is the core of all my text game coding. You can check me out on you tube under "Hungry Wolf Studios"
// Created June 4, 2017
// Author: Feral Crafter
// With: Hungry Wolf Studios
// Purpose: A starting point for any one who wants to make a text game
// Notes:
// A lot of effort goes into any game. I know I had something playable
// by 100 hours, but a good game by 600 hours.
// If you publish a game or hand it out, all I ask is a "Special Thanks to Hungry Wolf Studios"
#include<iostream>
#include<conio.h> // for kbhit();
usingnamespace std;
// declaring globals ... which is bad form!
int g_quit=0; // quit if this value = 1
// basic tools
void pause(void) {
cout << "\nPRESS ANY KEY TO CONTINUE ... ";
do { } while(!kbhit());
_getch();
cout << endl;
}
// processing key stroke here
void KeyStroke(char keyPress) {
switch(keyPress) {
case'q': { // we quit the game
cout << "You pressed: " << keyPress << endl;
g_quit = 1;
break;
}
default:
cout<< keyPress << " is not a valid keystroke" << endl;
break;
}
}
int main(void) {
cout << "Welcome to my game: \n";
pause();
char choice;
int counter = 0;
do {
cout << "Press a key: (\"Q\" to quit)\n";
do{ // this is where the magic happens
if(counter>9999) { counter=0; cout << "\r \r"; } // make sure we don't have a run away number
cout << "\r" << counter;
counter++;
} while(!kbhit()); // This keeps the system running while you wait on the player
cout << endl;
choice = _getch();
KeyStroke(choice);
} while(g_quit==0);
cout << "\nTHANK YOU FOR PLAYING" << endl;
pause();
return 0;
}