Simple game help

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
Last edited on
> 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).

Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <atomic>
#include <future>
#include <thread>
#include <chrono>
#include <cctype>

template < typename RESOURCE > // requires: RESOURCE is a TriviallyCopyable type
void periodic_increment( std::atomic<RESOURCE>& resource, unsigned int millisecs, 
                         std::atomic<bool>& quit )
{
    while( !quit )
    {
        std::this_thread::sleep_for( std::chrono::milliseconds(millisecs) ) ;
        ++resource ;
    }
}

int main()
{
    std::atomic<int> wood{0} ;
    std::atomic<bool> quit{false} ;
    const int interval_msecs = 2000 ; // 2 seconds

    // launch periodic_increment<int> asynchronously (execute it in a separate thread)
    auto future = std::async( std::launch::async, periodic_increment<int>,
                              std::ref(wood), interval_msecs, std::ref(quit) ) ;

    char decision ;
    while( std::cout << "decision (enter q to quit)? " &&
           std::cin >> decision && std::tolower(decision) != 'q' )
    {
        std::cout << "current value of wood == " << wood << '\n' << std::flush ;
        // whatever else
    }

    quit = true ; // indicate that the the program is over
    future.wait() ; // wait for the async operation to complete gracefully
}
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"

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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();

using namespace 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;
}

Last edited on
Topic archived. No new replies allowed.