我正在尝试与控制台进行快速时间事件类型的交互,并且我设法使用 conio 库获得了它。遗憾的是,我正在从事的项目要求代码在 Windows 和 Linux 上都可以编译,我想不出改变它的方法。
我可以做些什么来获得预期的效果,或者我应该放弃这个概念吗?下面是我创建的函数的代码。
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void battle(int x) {
srand(time(0));
printf("Use Q to dodge projectile attacks, E to block melee attacks and W to attack when the boss is stunned. Button mashing will not work! Press any key to start the battle.\n\nYou face Brutus, Lord Incarcerator!\n\n");
getchar();
bool ok;
int dealt = 0 ,recieved = 0 , state, prev = 0;
time_t start, end;
double elapsed;
while(dealt < 5 && recieved < 3)
{
do
{
state = rand() % 3 + 1;
}
while(prev == state);
prev = state;
time(&start);
switch(state)
{
case(1):
ok = 1;
printf("Brutus uses Hook Attack!\n\n");
do
{
time(&end);
elapsed = difftime(end, start);
if(kbhit())
{
if( getchar() == 'q')
{
printf("Dodged!\n\n");
ok = 0;
break;
}
else
{
break;
}
}
}
while(elapsed < 3);
if(ok)
{
printf("You took damage!\n\n");
recieved++;
break;
}
break;
case(2):
ok = 1;
printf("Brutus is stunned!\n\n");
do
{
time(&end);
elapsed = difftime(end, start);
if(kbhit())
{
if( getchar() == 'w')
{
printf("You dealt damage!\n\n");
dealt++;
ok = 0;
break;
}
else
{
break;
}
}
}
while(elapsed < 3);
if(ok)
{
printf("Too slow!\n\n");
break;
}
break;
case(3):
ok = 1;
printf("Brutus uses Blood Slam!\n\n");
do
{
time(&end);
elapsed = difftime(end, start);
if(kbhit())
{
if( getchar() == 'e')
{
printf("Blocked!\n\n");
ok = 0;
break;
}
else
{
break;
}
}
}
while(elapsed < 3);
if(ok)
{
printf("You took damage!\n\n");
recieved++;
break;
}
break;
}
}
if(recieved >= 3)
{
printf("Battle lost; you will now respawn in town.");
getchar();
}
else
{
printf("Battle won!");
getchar();
}
}
int main()
{
battle(2);
}