0
我試圖在C++中創建1分鐘的條帶動畫。我用TurboC++ 7做了這個Akki。我似乎無法使時鐘()功能符合我的需要。我想要實現的是能夠在動畫/程序執行上設置60秒的時間限制。誰能告訴我這有什麼問題?在C++中對進程設置時間限制
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
void main(){
clock_t start, point;
int gDriver = DETECT, gMode, errCode, tme;
clrscr();
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");
errCode = graphresult();
if (errCode != grOk){
cout << "Graphics error:\n" << grapherrormsg(errCode);
cout << "\n\nPress any key to continue...";
getch();
exit(1);
}
setbkcolor(0);
do{
start = clock()/CLOCKS_PER_SEC;
int x = 1, y = rand() % 480, r = rand() % 21;
setcolor(rand() % 16);
do{
if(kbhit())
break;
circle(x, y, r);
x++;
} while (x != 639);
x = rand() % 640, y = 1, r = rand() % 21;
setcolor(rand() % 16);
do{
if(kbhit())
break;
circle(x, y, r);
y++;
} while (y != 479);
x = 639, y = rand() % 480, r = rand() % 21;
setcolor(rand() % 16);
do{
if (kbhit())
break;
circle(x, y, r);
x--;
} while (x != 1);
x = rand() % 640, y = 479, r = rand() % 21;
setcolor(rand() % 16);
do{
if (kbhit())
break;
circle(x, y, r);
y--;
} while (y != 1);
point = clock()/CLOCKS_PER_SEC;
if ((point - start) >= 10)
break;
else
tme += point - start;
} while (!kbhit());
getch();
closegraph();
}
這是操作系統特定的。 –
如果程序總共運行時間超過60秒,那麼'start = clock()/ CLOCKS_PER_SEC'行應該在do-while循環之外(就在此之前)。 – ryanpattison