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 << "\n\t\t\t\t\t SCORE: " << sum << " pts"
<< "\n ******************************\n";
cout << " * " << arr[i] << " *\n";
cout << " ******************************\n\n";
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 << "\tEnter 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);
}
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:
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 thestop
: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
andstop
variables should bevolatile
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.