在c++; 即时通讯目前正在编程一个简单的游戏在C++控制台应用程序。我想实时显示一个计时器,当它执行第一个动作时开始,当计时器达到预定时间(如5分钟)时停止游戏。我不知道如何在C++中这样做,所以我想知道是否有人对此有什么想法?

在c++; 即时通讯目前正在编程一个简单的游戏在C++控制台应用程序。我想实时显示一个计时器,当它执行第一个动作时开始,当计时器达到预定时间(如5分钟)时停止游戏。我不知道如何在C++中这样做,所以我想知道是否有人对此有什么想法?,c++,visual-studio,visual-c++,C++,Visual Studio,Visual C++,提前感谢您,John。您可以在游戏开始时使用gettime()来获取开始时间。在游戏过程中,使用相同的方法从开始时间中减去,以检查所需的持续时间。您可以为此创建一个单独的流程#include//ctime仍然非常有用 #include <ctime> // ctime is still quite useful clock_t start = clock(); // gets number of clock ticks since prog

提前感谢您,John。

您可以在游戏开始时使用gettime()来获取开始时间。在游戏过程中,使用相同的方法从开始时间中减去,以检查所需的持续时间。您可以为此创建一个单独的流程

#include//ctime仍然非常有用
#include <ctime>                // ctime is still quite useful
clock_t start = clock();        // gets number of clock ticks since program start

clock_t end = 5 * CLOCKS_PER_SEC; // this is 5 seconds * number of ticks per second

// later, in game loop
if (clock() - start) > end { // clock() - start returns the current ticks minus the start ticks. we check if that is more than how many we wanted.
clock_t start=clock();//获取自程序启动以来的时钟滴答数 时钟结束=5*每秒时钟;//这是5秒*每秒的滴答数 //稍后,在游戏循环中 if(clock()-start)>end{//clock()-start返回当前的刻度减去开始刻度。我们检查这是否超过我们想要的数值。
#包括
#包括
int main()
{
无符号整数x_小时=0;
无符号整数x_分钟=0;
无符号整数x_秒=0;
无符号整数x_毫秒=0;
无符号整数totaltime=0,倒计时时间(单位:秒)为0,时间(左)为0;
时钟开始时间,计数时间;
倒计时时间单位为10;//1分钟为60,1小时为3600
x_startTime=clock();//开始时钟
time\u left=倒计时\u time\u,单位为秒-x秒;//更新计时器
同时(剩余时间>0)
{
x_countTime=clock();//更新计时器差
x_毫秒=x_countTime-x_startTime;
x_秒=(x_毫秒/(时钟/秒))-(x_分钟*60);
x_分钟=(x_毫秒/(时钟/秒))/60;
x_小时=x_分钟/60;
time_left=倒计时_time_,单位为秒-x秒;//减法得到差值
printf(“\n您还有%d秒”,剩余时间,倒数时间(以秒为单位);
}
printf(“\n\n\n超时\n\n”);
返回0;
}

对于任何寻找答案的人,您可以使用EasyLogg++性能跟踪功能

参见;以及样品


(披露,我是这个库的主要作者,不想做广告,但因为这个功能可以解决这个问题,所以提到这个问题)

你可以使用
SetTimer
来结束游戏。你需要更明确地了解你遇到的问题,跟踪时间或者让它在控制台中实时显示?
 #include <stdio.h>
#include <time.h>


int main ()
{


    unsigned int x_hours=0;
    unsigned int x_minutes=0;
    unsigned int x_seconds=0;
    unsigned int x_milliseconds=0;
    unsigned int totaltime=0,count_down_time_in_secs=0,time_left=0;

    clock_t x_startTime,x_countTime;
    count_down_time_in_secs=10;  // 1 minute is 60, 1 hour is 3600


    x_startTime=clock();  // start clock
    time_left=count_down_time_in_secs-x_seconds;   // update timer

    while (time_left>0) 
    {
        x_countTime=clock(); // update timer difference
        x_milliseconds=x_countTime-x_startTime;
        x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_minutes*60);
        x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
        x_hours=x_minutes/60;




        time_left=count_down_time_in_secs-x_seconds; // subtract to get difference 


        printf( "\nYou have %d seconds left  ",time_left,count_down_time_in_secs);
    }


    printf( "\n\n\nTime's out\n\n\n");

return 0;
}