简体   繁体   English

我不能让我的 function 计算已经过去了多少时间并相应地打印东西

[英]I can't make my function calculate how much time has passed and print stuff accordingly

bool IsGameEnded()
{
    static int i = 0;

    i++;
    if (i == 10)
        return true;
    return false;
}

int main()
{

   bool GameEnd = false;
   
   float ElapsedTime = 0;
   while(!GameEnd)
   {
       chrono::steady_clock::time_point StartingTime = chrono::steady_clock::now();

       

       if (ElapsedTime > 10)
       {
           ElapsedTime = 0;
           draw();
       }

       GameEnd = IsGameEnded();
      
       chrono::steady_clock::time_point EndingTime = chrono::steady_clock::now();
       ElapsedTime = ElapsedTime + chrono::duration_cast<chrono::milliseconds>(EndingTime - StartingTime).count();
   }

   return 0;
}

I wan't to make a snake game.我不想做蛇游戏。 It will be based on time.它将基于时间。 For example screen will update every 5 seconds or so.例如屏幕将每 5 秒左右更新一次。 For that I used chrono library.为此,我使用了 chrono 库。 I am not used to this trying o learn it so I might have missed something obvious.我不习惯这种尝试学习它,所以我可能错过了一些明显的东西。 But the problem is main function doesn't get get into the if block.但问题是主要的 function 没有进入 if 块。 So it draws nothing to the console.所以它不会吸引控制台。

I tried debugging (with running line by line).我尝试调试(逐行运行)。 It is not actually like a running program becasue time intervals get long but it enters if block every time.它实际上并不像一个正在运行的程序,因为时间间隔很长,但它每次都会进入 if 阻塞。 Also if I make the if condition 2 nanoseconds it also works but since cout function can not print so fast I need it to be a lot longer than that.此外,如果我将 if 条件设为 2 纳秒,它也可以工作,但由于 cout function 不能打印得这么快,我需要它比这长得多。 While Debugging I also realised that "StartingTime" and "EndingTime" variables don't get initiated (unless I directly stop on them).在调试时,我还意识到“StartingTime”和“EndingTime”变量不会被启动(除非我直接停止它们)。 The interesting part is If ı add cout after if block, after a while program starts entering the If block.有趣的部分是 If ı add cout after if 块,一段时间后程序开始进入 If 块。

When you do:当你这样做时:

chrono::duration_cast<chrono::milliseconds>(EndingTime - StartingTime).count();

not enough time has passed, and the count of milliseconds always returns 0. This means you always add 0 to ElapsedTime and it never crosses 10.没有足够的时间过去,毫秒计数总是返回 0。这意味着你总是将 0 添加到ElapsedTime并且它永远不会超过 10。

One fix is to use a smaller resolution:一种解决方法是使用较小的分辨率:

chrono::duration_cast<chrono::nanoseconds>(EndingTime - StartingTime).count();

as you mentioned in the question, and adjust the if condition appropriately.正如您在问题中提到的,并适当调整if条件。

However, the best fix would be to change ElapsedTime from a float to a chrono::duration (of the appropriate unit) since that is the unit that the variable represents.但是,最好的解决方法是将ElapsedTimefloat更改为 chrono chrono::duration (适当的单位),因为这是变量所代表的单位。 This would let you avoid having to do .count() on the duration as well.这也可以让您避免在持续时间上执行.count()

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

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