# Need help with a game timer?

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Writing small game in C I am not sure how to go about getting the game to run without the user pressing a key. Basically its like that old car game on the TI calculators, where you can't hit the sides or you crash. I am stuck at how to get the game to move without me pressing a key. I am using WASD, and well when I press a key the player moves and the background scrolls but, I have no idea how to make the background scroll alone without me pressing a key. I am a pretty decent programmer, but never touched this topic in class, my classes are based on applications...not games :( Anyway, any help with what kind of header file to include and how to use a proper fuction would help. I was thinking of using time.h and some functions in there but I can't find any good examples... Thanks so much in advance. Working on this lil game feels extrememly good and I can't wait to get this part going :)

##### Share on other sites
I had some troubles with timers, couldnt find an example so I made something really simple. Accualy, I am a n00b heheh..

It should be obvious what it does, for me its my global timer (which starts at 300 secs and goes to 0 ;>) and a combo timer which keeps track of the endurance of 2 seconds. Check the comments.

class Timer{	//Function: Creates a Timer object where time can be measure from		public:			Timer();				//Default constructor					void Start();					//Function: Take a copy of the current time					bool Check(int Seconds);			//int Seconds = number of Seconds to compare Start-time with				//Return value: bool = are the Seconds more then the timedifference between now and Start? (False)					//Function: Check a given time for time between starttime end checktime						int Update();				//Return value: int = time between now and starttime					//Function: Returns between now and starttime						private:		time_t m_time_1;		time_t m_time_2;	};Timer::Timer():m_time_1(0), m_time_2(0){time(&m_time_1);}void Timer::Start(){	time(&m_time_1);}bool Timer::Check(int Seconds){	time(&m_time_2);		if (difftime(m_time_2, m_time_1) < Seconds)		return(true);		return(false);}int Timer::Update(){	time(&m_time_2);		return(difftime(m_time_2, m_time_1));}

##### Share on other sites
Read up on "time-independent/frame-independent movement" (I've seen both terms for the same method). I believe there is an article here on GameDev for that; check the game programming section.

As for the actual function which to use I urge you to define what platform you develop on. On Windows GetTickCount is a rather poor function when it comes to time-independent movement, and QueryPerformanceCounter is more to recommend if you decide to go with that instead of fixed time-steps.