How to: Make a Game Time Out

Demonstrates how to make a game time out after a period of inactivity.

To make a game time out

  1. Create a class that derives from Game.

  2. Determine the desired time out limit in milliseconds.

    // Time out limit in ms.
    static private double TimeOutLimit = 8000;
    
  3. Add a variable for tracking elapsed time since user activity took place.

    // Amount of time that has passed.
    private double timeoutCount = 0;
    
  4. When user input is checked, set a flag indicating whether any user activity has taken place.

    bool HandleUserInput()
    {
        bool userActivity = false;
        KeyboardState keyboardState = Keyboard.GetState();
    
        if (keyboardState.IsKeyDown( Keys.Left ))
        {
            userActivity = true;
        }
        ...
        return userActivity;
    }
    
  5. In Update, if no user activity has taken place, increment the tracking variable by the elapsed time since the last call to Update. If user activity has taken place, set the tracking variable to zero.

    if (HandleUserInput() == false) timeoutCount += elapsedTime;
    else timeoutCount = 0;
    
  6. Next, check whether the value of the tracking variable is greater than the time out limit. If the variable is greater than the limit, perform some time out logic such as playing an idle animation or, in this case, exiting.

    if (timeoutCount > TimeOutLimit) Exit();
    
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;

    // Time out limit in ms.
    static private double TimeOutLimit = 8000;

    // Amount of time that has passed.
    private double timeoutCount = 0;


    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );
    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here.

        base.Initialize();
    }


    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {
            // TODO: Load any ResourceManagementMode.Automatic content.
        }

        // TODO: Load any ResourceManagementMode.Manual content.
    }


    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // Time elapsed since the last call to update.
        double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;

        if (HandleUserInput() == false) timeoutCount += elapsedTime;
        else timeoutCount = 0;

        if (timeoutCount > TimeOutLimit) Exit();

        base.Update( gameTime );
    }

    bool HandleUserInput()
    {
        bool userActivity = false;
        KeyboardState keyboardState = Keyboard.GetState();

        if (keyboardState.IsKeyDown( Keys.Left ))
        {
            userActivity = true;
        }
        if (keyboardState.IsKeyDown( Keys.Right ))
        {
            userActivity = true;
        }
        if (keyboardState.IsKeyDown( Keys.Up ))
        {
            userActivity = true;
        }
        if (keyboardState.IsKeyDown( Keys.Down ))
        {
            userActivity = true;
        }
        return userActivity;
    }

    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        // TODO: Add your drawing code here.

        base.Draw( gameTime );
    }
}