To determine the elapsed time, create two
DateTime
variables - one called
started
, and called
stopped
.
When the players starts to play, set the
started
datetime with
DateTime.Now
, and when a collision occurs, set the
stopped
datetime.
Since you're only displaying time as it passes, create a
Timer
object and when the game starts, start the timer, and in the
Tick
event handler, set your visual display of elapsed time.
You can use the two datetime objects to calculate the total time played, or you can use the tracked elapsed time that was set by the timer.
EDIT (response to your first comment) ============
If you wrote that code, you'd be the best one to identify the correct place to put the functionality you need. I'm not emotionally invested in your project, so I don't feel compelled to write/test code for you, and your lack of initialitive to learn on your own further diminishes my level of caring. Lastly, you're not going to learn anything if I spoon-feed you, but here are some guidelines:
0) You need to add this to the top of your file:
using System.Timers;
1) You need to define a Timer and two TimeSpan objects in your class:
System.Timers.Timer m_timer = new Timer(1000);
TimeSpan m_elapsed = new TimeSpan();
TimeSpan m_oneSecond = new TimeSpan(0,0,0,1);
2) You need to create an event handler for the Timer.Elapsed event in your constructor:
m_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
3) You need to add this method to your class:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if (m_elapsed != null)
{
m_timeSpan.Add(m_oneSecond);
... write some code to update the on-screen timer.
}
}
You have to do other maintenance crap associated with the timer, but you'll discover that eventually. This is as far as I can (or want to) help you on this.
Asking a question is fine if you're truly stuck, but ALL of what I've given you is easily discoverable by the most miniscule amount of googling.
One last thing - don't forget to re-mark this as the answer.