Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Creating a simple survival game.

When the player has been caught I want to display the amount of seconds that they survived for.

How do I go about this?

share|improve this question
You should accept one of the answers if they are what you were looking for – flai Dec 20 '12 at 17:00

When your round starts, save the current time, e.g. copying DateTime.Now.

Once the round is over, compare your old value with the current DateTime.Now and you should end up with the time span representing the length of the game (keep in mind that the precision is limited and different based on your operating system, but it's usually around 10-16 ms).

share|improve this answer

One fairly simple way to do this is to just make a variable called "SecondsSurvived" to either Player class or GameplayScreen/whatever. Then in the update method, if the game is not paused/stopped/ended, increment SecondsSurvived variable by ElapsedTime. Then when the player dies, simply use that SecondsSurvived variable as you like. So something like this:

public class GameplayScreen
     private float _secondsSurvived = 0f;
     private Player _player;


    public void Update(GameTime gameTime)

         if(_player.IsAlive && this.IsActiveScreen)
              _secondsSurvived += (float)gameTime.ElapsedGameTime.TotalSeconds;

Remember to reset _secondsSurvived to zero everytime the game restarts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.