clock_t start = clock();
clock_t end = clock();
int T = (int)((end-start)/ CLOCKS_PER_SEC);
void interface1() {
//setbkmode(OPAQUE);
setcolor(WHITE);
char str1[10], str2[10], str3[10], str4[10];
sprintf_s(str1, " %d", player.life);
sprintf_s(str2, " %d", player.intagla);
sprintf_s(str3, " %d", enemy->speed);
settextstyle(30, 0, "宋体");
outtextxy(444, 100, str1);
outtextxy(429, 70, "生命");
outtextxy(429, 140, "积分");
outtextxy(444, 170, str2);
outtextxy(390, 210, "小车速度");
outtextxy(444, 240, str3);
//clock_t start = clock();
//clock_t end = (clock() - start) / CLOCKS_PER_SEC;
//int time = (int)(end);
sprintf_s(str4, " %d", T);
outtextxy(390, 280, "游戏时间");
outtextxy(390, 310, str4);
}
void ends() { //结束界面
mciSendString("stop /images/racing.mp3", NULL, 0, NULL);
cleardevice();//清除窗口
settextstyle(128, 0, "宋体");
outtextxy(0, 256, "游戏结束");
settextstyle(40, 0, "宋体");
outtextxy(128, 385, "最终得分:");
outtextxy(128, 450, "游戏时间:");
char str3[10];
char str4[10];
sprintf_s(str3, " %d", player.intagla);
outtextxy(300, 385, str3);
sprintf_s(str4, " %d", T);
outtextxy(300, 450, str4);
}
T和end的计算放到interface1中的sprintf_s(str4, " %d", T);前面
clock_t start;// start = clock(); 放到游戏开始的地方。
clock_t end;// = clock();
int T;// = (int)((end-start)/ CLOCKS_PER_SEC); 上面3个需要全局变量。
void interface1() {
//setbkmode(OPAQUE);
setcolor(WHITE);
char str1[10], str2[10], str3[10], str4[10];
sprintf_s(str1, " %d", player.life);
sprintf_s(str2, " %d", player.intagla);
sprintf_s(str3, " %d", enemy->speed);
settextstyle(30, 0, "宋体");
outtextxy(444, 100, str1);
outtextxy(429, 70, "生命");
outtextxy(429, 140, "积分");
outtextxy(444, 170, str2);
outtextxy(390, 210, "小车速度");
outtextxy(444, 240, str3);
//clock_t start = clock();
//clock_t end = (clock() - start) / CLOCKS_PER_SEC;
//int time = (int)(end);
end = clock();//
T = (int)((end-start)/ CLOCKS_PER_SEC);//
sprintf_s(str4, " %d", T);
outtextxy(390, 280, "游戏时间");
outtextxy(390, 310, str4);
}
分析和修改如下,望采纳,有后续问题随时再沟通,谢谢!
你在初始化的时候定义了clock_t start和clock_t end两个变量,然后在计算时间的时候用的是这两个变量。
这两个变量实际上是在游戏开始的时候定义的,这样定义的话,start和end的值都是游戏开始的时间,计算出来的时间永远都是0。
要想解决这个问题,需要将start和end定义成全局变量,在游戏开始的时候初始化,在每一次计算时间的时候更新end的值。这样才能准确计算出游戏运行的时间。
下面是一种实现方法:
clock_t start;
clock_t end;
int T;
void startGame() {
// 游戏开始,初始化变量
start = clock();
end = start;
T = 0;
}
void updateGameTime() {
// 更新游戏时间
end = clock();
T = (int)((end - start) / CLOCKS_PER_SEC);
}
void interface1() {
// 计算并输出游戏时间
updateGameTime();
char str4[10];
sprintf_s(str4, " %d", T);
outtextxy(390, 280, "游戏时间");
outtextxy(390, 310, str4);
}
void ends() {
// 计算并输出游戏时间
updateGameTime();
char str4[10];
sprintf_s(str4, " %d", T);
outtextxy(300, 450, str4);
}
基于easyX制作的C语言小游戏
如有帮助,望采纳
https://blog.csdn.net/weixin_42098648/article/details/127698502