Hi, I have a game that at the moment is a little like mario- a jump and run. I want to add a timer to my main funtion so that after 60 seconds or so the game ends and goes to a highscore table where you can enter your name if your score qualifies. I don't know how to acheive either of these things so any help would be good :)

Thanks

Recommended Answers

All 7 Replies

This is entirely too generic of a question. Please give it a try and let us know if you have specific questions. If you've already written a game, surely is won't be hard for you! If you're using a library like SDL or something already, they will surely have a timer:

http://www.libsdl.org/intro.en/usingtimers.html

Some other recommended reading:

http://daviddoria.blogspot.com/2010/05/problem-abstraction-helping-others-help.html

Good luck,

Dave

i have been following a tutorial on http://jnrdev.72dpiarmy.com/
I am using SDL in Visual C++.
i want the timer to start at 60 and count down to 0 when it reaches 0 the program then goes to a high score table which allows you to enter your name if your score is high enough.- how is that too general?

You can use sdl's timer or clock, or if your on windows, Queryperformance counter.
For example, here is one that uses clock().

#include <iostream>
#include <ctime>

using namespace std;

class Timer{
public:
	typedef clock_t TimeType;
private:
	TimeType _clockStart; //holds the time of reference
public:
	Timer(): _clockStart(clock()){
	}
	TimeType getTicks()const{ 
		return clock() - _clockStart;  
	}	
	float getSecondsPassed()const{
		return getTicks()* 1.0f/CLOCKS_PER_SEC;
	}
	void startTime(){
		_clockStart = clock();
	}
	void resetTime(){
		_clockStart = clock();
	}

};
int main(){	

	Timer tm;

	tm.startTime();
	int i = 0;
	while(i != 5){
		if(tm.getTicks() > 1000){ //every 1 seconds
			cout << ++i << " seconds" << endl;
			tm.resetTime();
		}
	}

	cout <<"BOOM!!!!!"<<endl;

	return 0;
}

Thats helpful, i have different classes- would i put this code in the main function or make a new .cpp file for it?

Put it in different header and .cpp file.

thanks, im having some difficulties as i can't figure out what part goes in the header and what part goes in the .cpp.

Header should contain the prototype, if possible. So put the Timer class inside
the header, but strip the definition from it. Then implement the definition, in the
.cpp file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.