2014-03-31 137 views
0

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

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

#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); 
} 
+0

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

+0

您应该使用_kbhit()。 kbhit自1994年以来已被弃用。在标准C中没有等价物。请查看http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html以了解相应的Linux。 – cup

回答

1

不是真的,因为他们无法在某些 实现来实现的,只能以高昂的代价上 他人实施。

虽然不是标准,有ncurses便携库支持这些类型的操作。

0

有两种选择

  1. 找一些抽象的细节离开你跨平台库(SDL可能很适合这个。)
  2. 做抽象自己。

要做2.你将不得不定义你自己的输入函数版本。然后,您为每个平台创建不同的实现。这意味着您的核心代码在各种平台上保持通用。

1

你可以写你自己的getch()。

#include <termios.h> 
#include <unistd.h> 
#include <stdio.h> 

/* reads from keypress, doesn't echo */ 
int getch(void) 
{ 
    struct termios oldattr, newattr; 
    int ch; 
    tcgetattr(STDIN_FILENO, &oldattr); 
    newattr = oldattr; 
    newattr.c_lflag &= ~(ICANON | ECHO); 
    tcsetattr(STDIN_FILENO, TCSANOW, &newattr); 
    ch = getchar(); 
    tcsetattr(STDIN_FILENO, TCSANOW, &oldattr); 
    return ch; 
} 
+0

Conio与标准没有多大关系,而OP询问标准C. –

相关问题