简体   繁体   English

Timer C#用于游戏开发

[英]Timer C# use in game development

I have a game in C# and I need to allow tournament mode in which each round will be of 2 minutes. 我有一个C#游戏,我需要允许比赛模式,其中每轮将是2分钟。 How can I display the time from 0:00 up till 2:00 on the form? 如何在表格上显示从0:00到2:00的时间?

I have this in a constructor: 我在构造函数中有这个:

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Start();

And this is the Event Handler 这就是事件处理程序

    void Timer_Tick(object sender, EventArgs e)
    {
        this.textBox1.Text = DateTime.Now.ToLongTimeString();
    }

but I don't know how I can begin the time from 0:00 instread of the current time.. I tried creating a DateTime instance but when I do myDateTime.ToString(); 但我不知道如何从当前时间的0:00开始的时间开始..我尝试创建一个DateTime实例但是当我做myDateTime.ToString(); in the event handler, it just remains 0:00. 在事件处理程序中,它只是0:00。

I tried searching but I can't find anything related. 我试过搜索,但找不到任何相关内容。 Thanks a lot ! 非常感谢 !

Save current time to field when you are starting timer: 在启动计时器时将当前时间保存到字段:

_startTime = DateTime.Now;
timer.Start();

And calculate difference later: 然后计算差异:

void Timer_Tick(object sender, EventArgs e)
{
    this.textBox1.Text = (DateTime.Now - _startTime).ToString(@"mm\:ss");
}
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value. 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);

void Timer_Tick(object sender, EventArgs e)
    {
        label1.Text = stopWatch.ElapsedTicks.ToString();
    }

You can store a DateTime.Now when you start the timer and then in every timer tick handler calculate how much time has passed between DateTime.Now and the stored start date. 您可以在启动计时器时存储DateTime.Now,然后在每个计时器滴答处理程序中计算DateTime.Now与存储的开始日期之间经过的时间。 If you have a pause, you will need to also keep track of how long the game has been paused. 如果你有暂停,你还需要跟踪游戏暂停的时间。

Considering the inconviniences with the above method, I would suggest you declare a StopWatch somewhere, instantiate and start it where you call timer.Start and then in your timer tick just read the Elapsed property of the StopWatch. 考虑到上述方法的不便之处,我建议你在某个地方声明一个StopWatch ,实例化并在你调用timer.Start的地方启动它,然后在你的计时器中,只需读取StopWatch的Elapsed属性。 You can even Stop and Start (pause) it if you need. 如果需要,您甚至可以停止和启动(暂停)它。

You need a member variable that is in scope for both the timer initialization and the Timer_Tick event handler. 您需要一个成员变量,它在定时器初始化和Timer_Tick事件处理程序的作用域内。

class Something
{
    DateTime _myDateTime;
    Timer _timer;

    public Something()
    {
        _timer = new Timer();
        _timer.Interval = 1000;
        _timer.Tick += Timer_Tick;

        _myDateTime = DateTime.Now;
        _timer.Start();

    }

    void Timer_Tick(object sender, EventArgs e)
    {
        var diff = DateTime.Now.Subtract(_myDateTime);
        this.textBox1.Text = diff.ToString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM