Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

c++ - adding timer to game

I am making a game program in turbo c++ for my project and I need help on how to add a game timer, I've seen videos on how to create timer using while loop but I don't know how to implement it to my game. My plan for my game is to have it show 6 initialized letters(ex. "N A E B T S") and within 30 secs input as many words as possible which has corresponding points(6=10pts, 5=8pts, 4=6pts, 3=4pts). The correct words are written in a txt file with their corresponding points. Also the whole thing is in loop with clrscr();

Here is some parts of the game code:

void start()
{
    char arr[10][50] = {" B A N S E T ",
                        " L E A Z D Z ",
                        " M B L U E J ",
                        " P R G N I S ",
                        " A C Q U K Y ",
                        " S A H L E S ",
                        " R E D G A E ",
                        " Z E D Z U B "};

    int i = 0;
    int sum = 0;
    int x = 0;
    do
    {
        clrscr();
        cout << "
 SCORE: " << sum << " pts"
             << "
                  ******************************
";
        cout << "                  *       " << arr[i] << "        *
";
        cout << "                  ******************************

";
        char a[50], b[50];
        int  c;
        if (arr[0])
        {
            ifstream fin;
            fin.open("lvl1.txt");
            if (fin.fail())
            {
                cout << "File doesn't exist!";
                exit(1);
            }
            cout << "Enter word: ";
            cin >> a;
            do
            {
                fin >> b >> c;
                if (fin.eof() == 1)
                {
                    cout << "Incorrect! Try Again!";
                    delay(1500);
                    exit(1);
                }
            } while (strcmp(a, b) != 0);
            fin.close();
            if (strcmp(a, b) == 0)
            {
                sum += c;
            }
        }
    } while(s != 0); 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use PIT as a timer I used it in here:

its a mines game in old Turbo C++ and MS-DOS. For more info about PIT see:

there are links to PIT reference and examples I recommend you to see the PCGPE.

Now back to your question. You should register PIT ISR routine doing your timing/timeouting in the background ... Here example I just busted in DOSBOX:

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

int stop=0;
int timeout_cnt=0;

const int int_PIT=0x08;
void interrupt (*isr_PIT0)(...)=NULL; // original ISR handler
void interrupt isr_PIT(...) // new ISR handler
    {
    isr_PIT0(); // call original handler
    // here do your stuff
    if (timeout_cnt) timeout_cnt--;
    else stop=1;
    }

void main()
    {
    clrscr();
    isr_PIT0=getvect(int_PIT);  // store original ISR
    setvect(int_PIT,isr_PIT);   // set new ISR
    cout << "start counting" << endl;
    stop=0;
    timeout_cnt=(3*182)/10;     // init timeout 18.2Hz -> 3 sec
    for (;!stop;)
        {
        // here do your stuff
        }
    cout << "timeouted" << endl;
    setvect(int_PIT,isr_PIT0);  // restore original ISR
    getch(); // this is duplicated just to avoid DOSBOX glitches
    getch();
    getch();
    }

You basically need just dos.h all the other stuff is just for printing and handling keyboard.

So I created ISR that hooks up to PIT which is called with 18.2 Hz frequency. The timeout is initiated by setting the timeout_cnt to timeout time value and reseting the stop:

stop = 0;
timeout_cnt = time[sec] * 18.2;

ported to integer... once counter underflows it sets the stop value to true. I also call the original ISR handler as MS-DOS relays on it. Do not forget to restore original ISR before apps exit.

btw the timeout_cnt and stop variables should be volatile but IIRC it does not matter in old Turbo C++ as there are no optimizations that could optimize them out to speak of.

In case you change the PIT frequency you should call the original handler with 18.2 Hz and restore original PIT frequency before apps exit.

This can be also used as a sort of multitasking as you can do stuff in the ISR handler too (regardless of the main code) but you need to be careful as the main code can be paused at any time like in middle of writing string to screen and if your background stuff is printing too you can have distorted output etc ... so similar rules like in multi-threading applies.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...