本文介绍了在C#中计时游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#Windows应用程序中为游戏计时,玩游戏时必须在游戏界面上显示经过的时间.

how do i time a game in c# windows application,the elapsed time has to be displayed on the interface of the game when the game being played.

推荐答案


 Timer _timer;
const int UPDATE_TIME_MS = 1000;//Updates every second
DateTime _startTime;
TimeSpan _elapsedTime;
 ...

 private StartMonitoringTime()
{
    _timer = new System.Timers.Timer(UPDATE_TIME_MS);
    _timer.Elapsed += Timer_Elapsed;
    _startTime = DateTime.Now;
    _timer.Start();
}

...
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    _elapsedTime = DateTime.Now - _startTime;
    //Finally you expse the _elapsedTime to your UI (via binding or whatever)
}



这篇关于在C#中计时游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:39
查看更多