Jump to content
  • Advertisement
Sign in to follow this  
Figherstarter

Need help with a game timer?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!