Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 –  Jaakko Lipsanen Dec 20 '12 at 17:00
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.