我将如何着手实施以不同的速度秒表? [英] How would I go about implementing a stopwatch with different speeds?

查看:155
本文介绍了我将如何着手实施以不同的速度秒表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想要类似于秒表类,但具有额外的属性 Speed ,这将决定定时器更改分钟的速度。我不太确定我将如何执行此操作。

Ideally I would like to have something similar to the Stopwatch class but with an extra property called Speed which would determine how quickly the timer changes minutes. I am not quite sure how I would go about implementing this.

编辑

因为人们不太明白为什么我想这样做。考虑玩足球游戏,或任何体育游戏。一半是以分钟为单位计算的,但是游戏的时间框架显着较低,即在2.5分钟左右的时间内播放45分钟。

Since people don't quite seem to understand why I want to do this. Consider playing a soccer game, or any sport game. The halfs are measured in minutes, but the time-frame in which the game is played is significantly lower i.e. a 45 minute half is played in about 2.5 minutes.

推荐答案

简单的乘法不起作用的原因是它不会加快时间的传递 - 该因素适用于已经过去的所有时间,因为

The reason your simple "multiplication" doesn't work is that it doesn't speeding up the passing of time - the factor applies to all time that has passed, as well as time that is passing.

因此,如果您将速度因子设置为 3 然后等待10分钟,您的时钟将正确读取30分钟。但是如果你改变因子为 2 ,你的时钟将立即读取20分钟,因为乘法适用于已经过去的时间。这显然是不正确的。

So, if you set your speed factor to 3 and then wait 10 minutes, your clock will correctly read 30 minutes. But if you then change the factor to 2, your clock will immediately read 20 minutes because the multiplication is applied to time already passed. That's obviously not correct.

我不认为秒表是你想测量系统时间的类。

I don't think the stopwatch is the class you want to measure "system time" with. I think you want to measure it yoruself, and store elapsed time in your own variable.

假设你的目标项目真的是一个游戏,你可能会有你的游戏循环在代码中的某处。每次通过循环,您可以使用常规秒表对象来测量已经过了多少实时。乘以你的加速因子的值,并将它添加到一个单独的游戏时间计数器。这样,如果您降低速度因子,您只需减少应用于传递时间的因素,而不是您已记录的时间。

Assuming that your target project really is a game, you will likely have your "game loop" somewhere in code. Each time through the loop, you can use a regular stopwatch object to measure how much real-time has elapsed. Multiply that value by your speed-up factor and add it to a separate game-time counter. That way, if you reduce your speed factor, you only reduce the factor applied to passing time, not to the time you've already recorded.

如果需要,你可以将所有这些行为包装到你自己的秒表类中。如果你这样做,那么我建议你计算/累积经过的时间每次它被请求和每次因素改变。所以你有一个类这样的东西(注意,我已经跳过字段声明和一些简单的私人方法为简洁 - 这只是一个粗略的想法):

You can wrap all this behaviour into your own stopwatch class if needs be. If you do that, then I'd suggest that you calculate/accumulate the elapsed time both "every time it's requested" and also "every time the factor is changed." So you have a class something like this (note that I've skipped field declarations and some simple private methods for brevity - this is just a rough idea):

public class SpeedyStopwatch 
{
    // This is the time that your game/system will run from
    public TimeSpan ElapsedTime
    {
       get 
       { 
           CalculateElapsedTime();
           return this._elapsedTime;
       }
    }

    // This can be set to any value to control the passage of time
    public double ElapsedTime
    {
       get  { return this._timeFactor; }
       set 
       { 
           CalculateElapsedTime();
           this._timeFactor = value;
       }
    }

    private void CalculateElapsedTime()
    {
       // Find out how long (real-time) since we last called the method
       TimeSpan lastTimeInterval = GetElapsedTimeSinceLastCalculation();

       // Multiply this time by our factor
       lastTimeInterval *= this._timeFactor;

       // Add the multiplied time to our elapsed time
       this._elapsedTime += lastTimeInterval;
    }
 }

这篇关于我将如何着手实施以不同的速度秒表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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