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

Dani AI

Generated

Good plan and doable in small steps. already has SDL and VC++ set up, and the replies from and point in the right direction. The simplest robust approach is: keep a small timer object as part of the game state, measure real elapsed time (not frame count), subtract frame delta each update, and when remaining time hits zero switch to a "highscore entry" state that loads/saves a small score file and accepts text input.

Keep timing monotonic (steady) and update from your main loop rather than blocking sleeps. Example interface (minimal) you can drop into a header and implement in a .cpp file:

/* Timer.h */
#pragma once
class GameTimer {
public:
  GameTimer(double seconds = 60.0);
  void start();
  void update(double dt);    // dt = seconds since last frame
  double remaining() const;
  bool expired() const;
private:
  double total_seconds_;
  double left_seconds_;
};

Usage sketch in the game loop:

GameTimer timer(60.0);
timer.start();
while (running) {
  double dt = computeFrameDelta(); // use steady clock
  timer.update(dt);
  if (timer.expired()) changeToHighscoreState();
  renderTimerText(timer.remaining());
}

For highscores: load a small top-N list at startup (CSV or simple text), compare the final score to the lowest saved score, and if it qualifies enable a name-entry overlay. Use SDL text-input events (start text input, handle SDL_TEXTINPUT) to gather the name, then insert/sort and write the file back. See SDL text input docs for details (SDL_StartTextInput).

Notes and troubleshooting: prefer a monotonic clock (std::chrono::steady_clock) to avoid system-time jumps — reference: std::chrono::steady_clock. Put the class declaration in the header, method definitions and heavy includes (chrono, iostream, SDL) in the .cpp to reduce rebuilds. Do not block the loop while waiting for name input; handle input through your event loop so rendering/animations remain responsive.

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:

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.