Solved

How to implement a TIMER in a GAME

Posted on 2000-03-11
17
218 Views
Last Modified: 2010-04-15
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
}

0
Comment
Question by:shinaenay
  • 6
  • 3
  • 3
  • +4
17 Comments
 

Expert Comment

by:bmuita
Comment Utility
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
 

Author Comment

by:shinaenay
Comment Utility
Adjusted points to 21
0
 

Author Comment

by:shinaenay
Comment Utility
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
 

Author Comment

by:shinaenay
Comment Utility
Adjusted points to 50
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 

Expert Comment

by:sunsetyang
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
DOS?
0
 
LVL 1

Expert Comment

by:Lab_Rat
Comment Utility
Better than Windows...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Expert Comment

by:BudVVeezer
Comment Utility
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
 
LVL 3

Expert Comment

by:BudVVeezer
Comment Utility
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
 

Author Comment

by:shinaenay
Comment Utility
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
 
LVL 22

Accepted Solution

by:
cookre earned 50 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:Lab_Rat
Comment Utility
cookre, what compiler are you using?
'_kbhit()' ?

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

:)
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
_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
 

Author Comment

by:shinaenay
Comment Utility
cookre that may be what I'm looking for. I'll let you know.
0
 
LVL 3

Expert Comment

by:BudVVeezer
Comment Utility
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
 

Author Comment

by:shinaenay
Comment Utility
Thanks for everyone's input. I finally got it working!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with form project 66 313
Adjust Mfcapp 29 154
Read file descriptor 3 in C 14 101
Grammars for C C++ and java 1 108
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now