Making a countdown timer

Started by
3 comments, last by moeron 18 years, 9 months ago
Hi, For my newly re-written Pong clone, I'd like to make the matches last a certain period of time. At the top of the screen, there will be the remaining time rendered with SDL_TTF as text. I'd like it so that the timer starts out at, let's say, 02:30, and counts down 02:29, 02:28 etc., every second like a normal stopwatch. I'm using SDL on Windows, how would I implement this? I also want to be able to check when it is 00:00 so that I can end the game there. Any thoughts as to how I might achieve this? Thanks in advance, ukdeveloper.
Advertisement
I made this with C++ real quick to see if maybe it might help lead you in the direction that you wanting to go in. Here are some quick picks of it, and the source code. (Oh I have the time counting from 2:30 to 0:00)

Here are the pictures. = ]







And here is the source. I did this in C++ in Visual C++.NET

#include <iostream>#include <windows.h>#include <time.h>using namespace std;int main(){	long start = GetTickCount();	int minutes		= 2,		tenseconds	= 3,  // Timer Starts at 2:30		oneseconds	= 0;	srand(time(NULL));	system("CLS");	cout << "Timer Starting" << endl;	cout << minutes << ":" << tenseconds << "" << oneseconds << endl;	while(1)	{		if(GetTickCount() - start >= 1000)		{			start = GetTickCount();			if(oneseconds == 0)			{				oneseconds = 9;								if(tenseconds == 0)				{					tenseconds = 5;					if(minutes == 0)					{						minutes = 0;					}					else if(minutes != 0)						minutes--;				}				else					tenseconds--;			}			else				oneseconds--;			system("CLS");			cout << "Timer\n";			cout << minutes << ":" << tenseconds << "" << oneseconds << endl;			if( (minutes == 0) && (tenseconds == 0) && (oneseconds == 0) )				break;		}			}	cout << "This Match is Over!!!\n\n";	return (false);}


I hope this helps you out any on your project. If you have any questions about the code just let reply. = ] Good luck on your pong game.
if(minutes == 0)
{
minutes = 0;
}

The most useless 4 lines of code I have ever seen. :)

Instead of all that stuff, just have an int that counts down every second. So if you wanted to start the game with 3 minutes, set the int to 180, count down every second, and stop the game when it == 0. To display, your minutes would be seconds_left / 60, and your seconds would be seconds_left % 60.
Quote:Original post by Anonymous Poster
if(minutes == 0)
{
minutes = 0;
}

The most useless 4 lines of code I have ever seen. :)


Hahahahahahaha! I did it fast to help out, but it seems like you have a better idea then I do of how to do timers. = ]

I'm going to have to try the 180 seconds thing very soon, but after my 24 hour game project. = ]

Here's a pretty simple LowResTimer that uses GetTickCount. You can modify this to format the time however you please, but the basic principle is the same...
#include <iostream>#include <windows.h>	//	GetTickCountclass LowResTimer{	//	constructor / destruction	public:		LowResTimer() : m_current(0ul), m_stop(0ul)		{		}		virtual ~LowResTimer()		{		}	public:			//	Takes Time in MS  so 3000 will be ~3 seconds		void StartTimerMS(unsigned long ulHowLongToTimeMS)		{			m_current	= GetTickCount();			m_stop		= m_current + ulHowLongToTimeMS;		}		void StartTimerS(unsigned long ulHowLongToTimeSeconds)		{			m_current = GetTickCount();			m_stop	= m_current + (ulHowLongToTimeSeconds * 1000ul);		}		//	has our time elapsed?		bool Ding(void)		{			m_current = GetTickCount();			return(m_current >= m_stop);		}		unsigned long GetElapsedTime(void)const		{			return m_stop - m_current;		}		unsigned long GetElapsedTimeSeconds(void)const		{			return (m_stop - m_current) / 1000ul;		}	private:		unsigned long	m_current;	//	our start time		unsigned long	m_stop;	//	when we should stop		};int main(){	LowResTimer timer;      // Set the timer to count down from 10 and print each second as it goes	for( timer.StartTimerS(10ul); !timer.Ding(); )	{		static unsigned long lastTime = timer.GetElapsedTimeSeconds();		if( timer.GetElapsedTimeSeconds() != lastTime )			std::cout << (lastTime = timer.GetElapsedTimeSeconds() ) << std::endl;			}		return(0);}


Output:
9876543210
moe.ron

This topic is closed to new replies.

Advertisement