我正在制作一个游戏,在其中您必须破解一个系统,例如:键入您在屏幕上看到的数字:10秒内输入12345。我想知道如何在10秒钟后警告玩家,例如在屏幕上显示“ Too slow !!!!!!”(太慢!!!!)。我尝试过sleep()
函数,但是在sleep()
函数运行时会停止程序!
规则:启动程序时,出现在屏幕上:
Enter code: Hack 1.
如果键入1并输入出现一个随机数,则必须覆盖。如果失败,则会出现:
Hacking failed!!!!!!!!.
如果您太慢,它将显示:
Too slow!!!!!!!
但是那件事“太慢了!!!!!” 仅在程序结束时发生!
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
time_t start, end;
double need;
int i;
double number;
int t = 15;
z:
printf("Enter the code: Hack 1 :");
scanf("%d", &i);
if(i == 123456789)
{
printf("Enter the room.");
}
if(i == 1)
{
printf("You've got %d seconds. Press 1 to start hacking the system:", t);
scanf("%d", &i);
if(i == 1)
{
//Appears a random number and time starts counting
time (&start);
srand(time(NULL));
double rn = (rand() % 1000000000000000000);
printf("%f type here: ", rn);
scanf("%lf", &number);
if(number == rn)
{
//Time ends
time (&end);
//Calculate elapsed time
need = difftime (end, start);
//If you're too late
if(need > t)
{
printf("Too late!!!!!!!!!!!");
}
else
{
//If you success
printf("System hacked. Enter the room. ");
t--;
goto z;
}
}
else
{
//If you fail
printf("Hacking failed!!!!!!!!!!");
}
}
}
}
这是执行此操作的一种方法,但是它需要conio.h
Windows和DOS外部通常不提供的方法。它在检查计时器的同时检查键盘输入。
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TIMEOUT 10
int main(void)
{
int code;
int entry;
int key;
time_t mark;
time_t now;
mark = time(NULL);
srand((unsigned)mark);
code = rand();
printf("The code is %d\n", code);
printf("Enter the code\n");
entry = 0;
while (entry < code) {
while (!_kbhit()) {
now = time(NULL);
if (now - mark > TIMEOUT) {
printf("\nTimeout failure!\n");
exit (1);
}
}
key = _getche();
entry = entry * 10 + key - '0';
}
if (entry == code)
printf("\nSuccess\n");
else
printf("\nIncorrect code\n");
return 0;
}
程序输出:
The code is 19911
Enter the code
1984
Timeout failure!
更多程序输出:
The code is 20326
Enter the code
29881
Incorrect code
然后再次:
The code is 20156
Enter the code
20156
Success
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句