7
 for (;;)
{
    cout << "You are playing for:" << playtime << "seconds." << endl;
    cout << "You have " << bytes << " bytes." << endl;
    cout << "You are compiling " << bps << " bytes per second." << endl;
    cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
    switch(getch())
    {
        case 'a': bytes = bytes - 10; bps++; break;
    }
    bytes = bytes + bps;
playtime++;
Sleep(1000);
system("cls");
}

Let's say that's my incremental game. I want refresh my game after 1 second. How can I make getch() to wait for input without stopping all other stuff?

Fairlight
  • 79
  • 1
  • 4

3 Answers3

5

Use kbhit() function to detect if a key was pressed :)

something like:

 for (;;)
{
    cout << "You are playing for:" << playtime << "seconds." << endl;
    cout << "You have " << bytes << " bytes." << endl;
    cout << "You are compiling " << bps << " bytes per second." << endl;
    cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
    if(kbhit()){  //is true when a key was pressed
        char c = getch();   //capture the key code and insert into c

        switch(c)
        {
            case 'a': bytes = bytes - 10; bps++; break;
        }
    }
    bytes = bytes + bps;
    playtime++;
    Sleep(1000);
    system("cls");
}
Jason C
  • 38,729
  • 14
  • 126
  • 182
Mario
  • 51
  • 1
  • 2
1

You could use another thread, to get the user input.

The for (;;) is unnecessary, instead you should use while (true).

#include <Windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

DWORD WINAPI SpeedThread(LPVOID lpParam);



int main ()
{
    int playtime = 0,
        bytes = 0,
        bps = 1;

    bool bKeyPressed = false;

    CreateThread( NULL, 0, SpeedThread, &bKeyPressed, 0, NULL);

    while (true)
    {
        cout << "You are playing for:" << playtime << "seconds." << endl;
        cout << "You have " << bytes << " bytes." << endl;
        cout << "You are compiling " << bps << " bytes per second." << endl;
        cout << "Press a to buy assembler monkey (produces 1 byte per second)/(cost 10 bytes)" << endl;
        if (bKeyPressed && bytes >= 10)
        {
            bytes -= 10;    
            bps++; 

            bKeyPressed = false;
        }
        bytes = bytes + bps;
        playtime++;
        Sleep(1000);
        system("cls");
    }

}

DWORD WINAPI SpeedThread (LPVOID lpParam)
{
    bool * bKeyPressed = (bool *) lpParam;

    while (true)
    {
        if (_getch () == 'a')
            *bKeyPressed = true;
    }
}
schacker22
  • 439
  • 2
  • 5
  • 12
  • You tried to help me and I appreciate this but unfortunalty it doesn't work :/ – Fairlight Jul 20 '14 at 13:04
  • @Fairlight I've tested the code now and edited it. On my side it works now, but please tell me the errors you have. You will also have to add an if condition in the bKeyPressed part, so that the bytes will never be unter 0- – schacker22 Jul 20 '14 at 13:38
  • I said that wrong. Your code seems to be good, but I am newbie in C++. I have no idea how to use WinAPI (these DWORD's etc.). I don't know where to put my code. – Fairlight Jul 22 '14 at 14:49
  • There is no difference between for(;;) and while(true). They mean the same same and do the same thing. – Emacs The Viking Jul 09 '22 at 13:04
1

What worked for me is not to use getch() but instead use scanf(). In order to stop scanf from stopping you have to use :

scanf("%c \n",example);

Keep in mind, that example is a pointer (char* example;)

stvar
  • 6,551
  • 2
  • 13
  • 28
Max
  • 11
  • 1