現在、C++ コンソール アプリケーションで簡単なゲームをプログラミングしています。最初のアクションが実行されたときに開始し、タイマーが 5 分などの事前に定義された時間に達したときにゲームを停止するタイマーをリアルタイムで表示したいと考えています。C ++でこれを行う方法がわからないので、これを行う方法について何かアイデアがあるかどうか疑問に思っていましたか?
前もって感謝します、ジョン。
現在、C++ コンソール アプリケーションで簡単なゲームをプログラミングしています。最初のアクションが実行されたときに開始し、タイマーが 5 分などの事前に定義された時間に達したときにゲームを停止するタイマーをリアルタイムで表示したいと考えています。C ++でこれを行う方法がわからないので、これを行う方法について何かアイデアがあるかどうか疑問に思っていましたか?
前もって感謝します、ジョン。
ゲームの開始時に gettime() を使用して、開始時間を取得できます。ゲーム中に、同じ方法を使用して、開始時刻から差し引いて、目的の期間を確認します。この目的のために別のプロセスを作成することができます
#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;
}
#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.