admin 管理员组

文章数量: 887044

c语言kbhit函数头文件,有没有办法在标准C中替换kbhit()和getch()函数?

我试图用控制台获得快速时间事件类型的交互,并设法使用conio库获取它。可悲的是,我正在开发的这个项目需要在Windows和Linux上编译代码,我无法找到一种方法来改变它。有没有办法在标准C中替换kbhit()和getch()函数?

有什么我可以做得到理想的效果还是应该放弃这个概念?以下是我创建的函数的代码。

#include

#include

#include

#include

#include

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);

}

+0

你的标题,但代码C]说 “标准C” ++? –

+0

您应该使用_kbhit()。 kbhit自1994年以来已被弃用。在标准C中没有等价物。请查看.html以了解相应的Linux。 –

本文标签: c语言kbhit函数头文件 有没有办法在标准C中替换kbhit()和getch()函数