• C

How to implement a TIMER in a GAME

I'm programming a game. I've designed it so that after the game starts, it's in a continuous loop that can only be broken by the user choosing to quit or by time running out.

My question is how do I set a timer to run while the program is running and how do I make it interrupt my loop when time is up?

For clarification a real high level design follows:

display_board
{
  displays 5X5 matrix of random letters
  calls play_game()
}

play_game
{
  prompts user for word
  reads in word
  calls is_word_legal()
}

is_word_legal
{
  checks to see if word is on the board
  calls display_board()
}


//the user should have 60 seconds to try to find all the words he/she can. After that the loop should be broken and another function called, game_over:

game_over
{
  Ask if user wants to play again
  if yes call display_board
}

shinaenayAsked:
Who is Participating?
 
cookreConnect With a Mentor Commented:
Gasp!  You mean you're not (assuming MS & DOS) using:

if (_kbhit()) ...

to check the keyboard buffer first?

Or, Int 16h, Func 01h?

Or, to get down to looking at the keyboard buffer itself, if the offset to the start of the buffer(word @ 0040:001a) is the same as the offset to the end (word @ 0040:001c), the buffer is empty.

Now you'll never HAVE to wait for user input again...
0
 
bmuitaCommented:
You could try hooking onto the timer interrupt, and have a count within the handler( lets call it count) than when count / 18.2 = 60 then it should call the exit game procedure.

~Brian Muita
0
 
shinaenayAuthor Commented:
Adjusted points to 21
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
shinaenayAuthor Commented:
That sounds like a feasible solution, however, I'm way too inexperienced to know how to implement your theory. At a minimum I need instructions on how to hook onto the timer interrupt.
0
 
shinaenayAuthor Commented:
Adjusted points to 50
0
 
cookreCommented:
If you're writing for DOS, use the 'clock' call in your loop to count elapsed time:

clock_t StartTics;
....
StartTics=clock();
...
top of loop
if ( (clock()/CLOCKS_PER_SEC)>60 ) break;




If you're writing for Windows, use a timer.  When you want to start timing, issue a SetTimer to specify the interval you want.  When the interval has elapsed, you'll recveive a WM_TIMER message.  


For either approach, you may want to consider setting your detection interval to something like one second so you can display an irritating and distracting message as the clock ticks down.
0
 
sunsetyangCommented:
Cookre 's suggestion is feasible and easier to implement than to change the interrupt vector.
  If you want to change the interrupt vector,you should do like this:
 write a interrupt type function like:
 interrupt serv(...)
 {
  old_vector();
  //do your own calculation or counting
 }
 in your function,you can just use the couting result the serv(..) keeps to find out whether 60 seconds have elapsed.If you keep a gloabal variable like count,and every time the vector runs,just increase the count.Then when the count cames to 18.2*60,the time is up.This method is too complex and low efficiency.Before you use this,you should get the old vector and store it,then installs your vector;before you end your application,you should restore the old vector.
 Initialization Code like:
 getvector(0x1c,old_vector);
 setvector(0x1c,serv);
 ....
 ....
 Uninstallation Code lie:
 setvector(0x1c,old_vector);
 //interrupt 8 also des this.
 You can also achieve your intension by getting the bios time counter located at
0040h:0072(?)h,I'm not sure of.But should be near there.It's a dword ,recorded the ticks since the 00:00,also you should div 18.2 to get the right seconds.You can use a far pointer to
get the value.Just like:
 long far *pvalue=(long far*)MK_FP(0x40,0x72);
 (*pvalue ) is the value of the tick count.
  The clock function metioned by Cookre access the location and returns the right value.  
0
 
WynCommented:
DOS?
0
 
Lab_RatCommented:
Better than Windows...
0
 
BudVVeezerCommented:
Lab_rat, why do you say that?  And why is everyone making it so complicated??  It sounds like you need something that tells you when a certain amount of seconds have passed..  in your main game loop(I am assuming you know how to create a simple game, but if you don't, just ask) have this:

int timer = 0;
int start_time = 0;

start_time = time();  //initial time
while(!done) //start of game loop
{
  get_input();
  do_game_stuff();
  timer = time();
  if(timer - start_time >= Seconds_till_quit)
  done = 1;
}

~Aaron
0
 
BudVVeezerCommented:
if I remember correctly(don't have my complier handy so can't check for sure) time() simply returns the time it is in seconds...  so get a start time when you want to start the timer, and each iteration through your game loop, check the current time.  If the current time minus the start time is greater than or equal to how long the user can play, then simply break the loop and end the game.  It's a fairly standard practice assuming you don't need your time to be absolutely PERFECT.  It's possbible, ig you have a lot of processing in your game that more than 1 second will pass before you check the time again..in which case you get to figure out where you can place your timer, or how you can reduce the processing.  In WINDOWS(sorry lab_rat) you generally just start another thread and use the multimedia timers...

~Aaron
0
 
shinaenayAuthor Commented:
Most of these are good suggestions guys and I really appreciate them. However, I need to be able to INTERRUPT the program - you see,time is probably going to run out while the computer is waiting for input from the user. The user is searching for a word on the board and the last command in the program at that point is: read(userword)

So the program is just waiting for the user and during that time, time will run out and I need to interrupt there.
0
 
Lab_RatCommented:
cookre, what compiler are you using?
'_kbhit()' ?

BudVVeezer, I should finish my scentence: ...if you want to do all the hard work yourself.

:)
0
 
cookreCommented:
_kbhit() has been in the various MS C & VC compilers from v1.5 (that's as far back as I go) up to, and including, MSVC v6.  Interestingly enough, it's also in the Novell Netware API library.

Here's an MS help file entry:



_kbhit

#include <conio.h>       Required only for function declarations

Syntax       int _kbhit( void );


The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer. The program can then call _getch or _getche to get the keystroke.

Return Value

The _kbhit function returns a nonzero value if a key has been pressed. Otherwise, it returns 0.
0
 
shinaenayAuthor Commented:
cookre that may be what I'm looking for. I'll let you know.
0
 
BudVVeezerCommented:
well, if you want ANYTHING done the right way, you usually end up having to do it the hard way.  ESPECIALLY with M$. ;-)  But what can I say, people use the OS, gotta make games for it!  Have you seen BeOS yet???  Man, I can't wait to get my copy, that could take some KILLER games..  ;-)

~Aaron
0
 
shinaenayAuthor Commented:
Thanks for everyone's input. I finally got it working!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.