在C#中计时游戏 [英] timing a game in c#

查看:167
本文介绍了在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.

推荐答案

就我个人而言,我会使用BackgroundWorker每秒发送一次进度事件.毫无疑问,有些人会建议使用Timer,但我鄙视Timer,并认为它们是前VB程序员的后备,他们迫切希望坚持旧的(不良的)习惯,这些习惯是在他们的编程初期就学到的.

YMMV.
Personally, I would use a BackgroundWorker that sends out a progress event once every second. Some folks will undoubtedly suggest the use of a Timer, but I despise Timers, and consider them to be the fallback of ex-VB programmers that desperately want to cling to old (bad) habits, learned in their programming infancy.

YMMV.


尽管JSOP表示不使用计时器,但冒被投票的风险,我建议他们使用它.这很简单.

创建计时器并设置更新间隔(需要更新UI的频率),然后启动计时器.同时存储开始时间,以便您可以继续计算经过的时间;

Although JSOP says not to use Timers, at the risk of being down voted I suggest them... It is quite simple.

Create your timer and set up the update interval (how often the UI needs to be updated) and then start the timer. Also store the start time so you can keep calculating the elapsed time;

 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)
}


下午好!

最简单的方法是使用时间跨度(请参阅链接).基本上,您将在开始播放时设置一个datetime对象,并间隔计算当前时间和开始时间之间的时间跨度.

http://msdn.microsoft.com/en-us/library/system.timespan.aspx [^ ]

关于上述解决方案1,我同意100%的后台工作人员在计时器上工作.请记住,它不在UI线程上,因此还有很多开销.

您可以使用计时器,但是请记住它们并不是那么精确,并且计算将在UI线程上进行.您要在此处执行的操作并不多,但是如果您在UI线程之外大量运行,它将累加并影响性能.

总结一下,如果可以的话,除非确实需要,否则请使用后台工作程序(甚至只是一个新的system.threading.thread).

祝您好运,并告诉我们是否还有其他方法可以帮助您.
Good afternoon!

The easiest way would be to use a timespan (see link). Basically, you''ll set a datetime object when you start playing, and at intervals calculate the timespan between the current time and the start time.

http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^]

Regarding solution 1 above, I agree 100% on the background worker over the timer. Just remember that it''s not on the UI thread, so there''s a good bit more overhead.

You can use a timer, but remember they''re not that terribly accurate, and the calculations would be on the UI thread. What you''re trying to do here isn''t that much, but if you''re running alot off the UI thread, it''s going to add up and affect performance.

To summarize, if you can, use a background worker (or even just a new system.threading.thread) unless you really need to.

Good luck, and let us know if there''s anything else we can do to help.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆