Continuous keyboard input in C

后端 未结 3 1497
猫巷女王i
猫巷女王i 2021-02-07 05:18

I am creating a console application in C. This is a game in which characters are falling down and user has to press that specific key on the keyboard. I don\'t know how to detec

3条回答
  •  醉话见心
    2021-02-07 05:58

    I think this might be the non-blocking keyboard input you are looking for.

    void simple_keyboard_input()  //win32 & conio.h
        {
            if (kbhit())
              {
                    KB_code = getch();
                    //cout<<"KB_code = "<

    And as for the characters falling down.. here you go.

    The Matrix  falling numbers

    Code for if you are on Windows:

    /* The Matrix  falling numbers */
    
    #include 
    #include  
    
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    
    #define KB_UP 72
    #define KB_DOWN 80
    #define KB_LEFT 75
    #define KB_RIGHT 77
    #define KB_ESCAPE 27
    #define KB_F8 66
    
    
    /* Variables*/
    
    char screen_buffer[2000]={' '};
    int y_coord[2000]={0};
    int x=0, y=0,dy=0;
    int XMAX=77;
    int YMAX=23;
    int KB_code=0;
    bool QuitGame=false;
    int platformX=35, platformY=23;
    
    /* function prototypes*/
    
    void gotoxy(int x, int y);
    void clrscr(void);
    void setcolor(WORD color); 
    void simple_keyboard_input();  
    void draw_falling_numbers();
    void draw_platform();
    
    /*  main  */
    
    int main(void)
    {
      /* generate random seed */
      srand ( time(NULL) );
    
      /* generate random number*/
      for(int i=0;iYMAX) y_coord[x]=0;
    
            /* draw dark color */
            setcolor(2);
            gotoxy(x ,y_coord[x]-1); cout<<"  "<74) platformX=74;
                    break;
    
                    case KB_UP:
                               //Do something                     
                    break;
    
                    case KB_DOWN:
                               //Do something                     
                    break;
    
                }        
    
          }
    
    }
    
    
    void setcolor(WORD color)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
        return;
    }
    
    
    void gotoxy(int x, int y)
    {
      static HANDLE hStdout = NULL;
      COORD coord;
    
      coord.X = x;
      coord.Y = y;
    
      if(!hStdout)
      {
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
      }
    
      SetConsoleCursorPosition(hStdout,coord);
    }
    
    
    void clrscr(void)
    {
      static HANDLE hStdout = NULL;      
      static CONSOLE_SCREEN_BUFFER_INFO csbi;
      const COORD startCoords = {0,0};   
      DWORD dummy;
    
      if(!hStdout)               
      {
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hStdout,&csbi);
      }
    
      FillConsoleOutputCharacter(hStdout,
                                 ' ',
                                 csbi.dwSize.X * csbi.dwSize.Y,
                                 startCoords,
                                 &dummy);    
      gotoxy(0,0);
    }
    

提交回复
热议问题