Sign in to follow this  
Figherstarter

Need help with a game timer?

Recommended Posts

Figherstarter    122
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
Mrduckels    106
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
Unwise owl    158
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this