How do I represent the total time played in seconds in C# for this game application?

How do I represent, in seconds, the total time played for the game represented in the code below.  The code is being run in Visual Studio 2005.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ShootTheGreenLightGame
{
    public partial class Form1 : Form
    {

        private Image redLightImage;
        private Image greenLightImage;
        private Graphics g;

        private Boolean IsGameOver = false;

        private int width;  // Win Mobile Pocket PC screen Width default 230
        private int height; // Win Mobile Pocket PC screen Height default 258

        private int redLightImageW;
        private int redLightImageH;
        private int currentRedLightX;
        private int currentRedLightY;

        private int greenLightImageW;
        private int greenLightImageH;
        private int currentGreenLightX;
        private int currentGreenLightY;

        private int score = 0;

        private const double SPEED = 1000 / 50; // 50 frames per second

        public KeyEventArgs keyState;

       
       
        public Form1()
        {
            width = Width;
            height = Height;

            redLightImage =
                 new Bitmap(System.Reflection.Assembly.GetExecutingAssembly()
                 .GetManifestResourceStream("ShootTheGreenLightGame.RedLightW30H20.gif"));

            redLightImageW = redLightImage.Width;
            redLightImageH = redLightImage.Height;


            greenLightImage =
                new Bitmap(System.Reflection.Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("ShootTheGreenLightGame.GreenLightW30H20.gif"));

            greenLightImageW = greenLightImage.Width;
            greenLightImageH = greenLightImage.Height;

            g = CreateGraphics();
           
            InitializeComponent();
        }

        public void Stop()
        {
            IsGameOver = true;
            MessageBox.Show(String.Format("Game Over\n\nScore = {0}\nTotal Time = {1}", score));
            Application.Exit();
        }


        public void Start()
        {

            score = 0;
            IsGameOver = false;

            currentRedLightX = 0;
            currentRedLightY = 0;

            currentGreenLightX = width / 2;
            currentGreenLightY = height / 2;


            double minIterationDuration = SPEED; // 50 frames / sec

            //game loop
            while (!IsGameOver)
            {
                if (IsCollision())
                {
                    score += 10;
                }

                DateTime startIterationTime = System.DateTime.Now;
                UpdateGameState();
                Render();
                DateTime endIterationTime = System.DateTime.Now;
                TimeSpan iterationDuration = endIterationTime - startIterationTime;
                if (iterationDuration.TotalMilliseconds < minIterationDuration)
                    Thread.Sleep(Convert.ToInt32(minIterationDuration - iterationDuration.TotalMilliseconds));
                Application.DoEvents();
            }

        }


        public void UpdateGameState()
        {
            if (keyState != null)
            {
                switch (keyState.KeyCode)
                {
                    case Keys.Left:
                        currentRedLightX = Math.Max(0, currentRedLightX - 5);
                        break;
                    case Keys.Right:
                        currentRedLightX = Math.Min(width - redLightImageW, currentRedLightX + 5);
                        break;
                }
            }


            if ((currentRedLightY + 5) >= (height - redLightImageH))
            {
                currentRedLightY = 0;
            }
            else
            {
                currentRedLightY += 5;
            }
        }


        public void Render()
        {
            g.Clear(System.Drawing.Color.White);
            g.DrawImage(redLightImage, currentRedLightX, currentRedLightY);
            g.DrawImage(greenLightImage, currentGreenLightX, currentGreenLightY);
        }


        public bool IsCollision()
        {
            if (currentRedLightX >= (currentGreenLightX - 15) && // 15 = 1/2 greenLightImageW
                currentRedLightX <= (currentGreenLightX + 45) && // 45 = 1 1/2 greenLightImageW
                currentRedLightY >= (currentGreenLightY - 10) && // 10 = 1/2 greenLightImageH
                currentRedLightY <= (currentGreenLightY + 30))  // 30 = 1 1/2 greenLightImageH
                return true;
            else
                return false;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Render();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
                keyState = e;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
                keyState = e;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
                keyState = e;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
                keyState = e;
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
                Stop();
            }

        }

    }
}
NSingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cdaly33Commented:
Setup a timer on load then start it in the Start method and stop it in the Stop method.  You can evaluate the time played using the Elapsed property of the timer.

http://msdn.microsoft.com/en-us/library/system.timers.timer(VS.71).aspx
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Or you can just add it into to your already existing game loop:

            //game loop
            DateTime startTime = DateTime.Now;  //    <--------- record start time before the loop begins
            while (!IsGameOver)
            {
                if (IsCollision())
                {
                    score += 10;
                }

                DateTime startIterationTime = System.DateTime.Now;
                UpdateGameState();
                Render();
                DateTime endIterationTime = System.DateTime.Now;
                TimeSpan iterationDuration = endIterationTime - startIterationTime;
                TimeSpan elapsed = DateTime.Now.Subtract(startTime);  //    <--------- calculate elapsed time
                label1.Text = ((int)elapsed.TotalSeconds).ToString() + " seconds";  //    <--------- somehow display elapsed seconds
                if (iterationDuration.TotalMilliseconds < minIterationDuration)
                    Thread.Sleep(Convert.ToInt32(minIterationDuration - iterationDuration.TotalMilliseconds));
                Application.DoEvents();
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NSingAuthor Commented:
Thanks.  That was the approach that I was attempting to take but was not capturing the time outside the game loop..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.