最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c语言kbhit函数头文件,c

运维笔记admin69浏览0评论

c语言kbhit函数头文件,c

c语言kbhit函数头文件,c

我试图获得与控制台交互的快速时间事件类型,并设法使用conio库获得了它。 不幸的是,我正在从事的项目要求代码必须在Windows和Linux上都可以编译,但我想不出一种改变它的方法。

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

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

  1. 暂无评论