21

我正在用 C 语言创建一个控制台应用程序。这是一个角色跌落的游戏,用户必须按下键盘上的特定键。我不知道如何在不暂停下降字符的情况下检测用户按下了哪个键。当我使用 scanf 时,程序等待输入并且一切都暂停。请尽快帮助我!

4

3 回答 3

11

有一个函数被调用kbhit(),或者_kbhit它在<conio.h>它返回的库中,true或者false取决于是否按下了一个键。所以你可以用这样的东西:

while (1){
    if ( _kbhit() )
        key_code = _getch();
        // do stuff depending on key_code
    else 
        continue;

也使用getch()or _getchwhich 直接从控制台而不是从缓冲区读取字符。您可以在此处阅读有关conio.h函数的更多信息,它们可能对您想要做的事情非常有用。

注意:conio.h不是标准库,实现可能因编译器而异。

于 2013-11-09T11:18:04.173 回答
6

您可能会寻找ncurses

ncurses(新curses)是一个提供API的编程库,允许程序员以独立于终端的方式编写基于文本的用户界面。它是一个用于开发在终端仿真器下运行的“类 GUI”应用软件的工具包。

还要检查C/C++: Capture characters from standard input without waiting for enter to be press

#include <conio.h>

if (kbhit()!=0) {
    cout<<getch()<<endl;
}
于 2013-11-09T11:02:49.867 回答
4

我认为这可能是您正在寻找的非阻塞键盘输入。

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

                switch (KB_code)
                {

                    case KB_ESCAPE:

                        QuitGame=true;

                    break;
                 }//switch
             }//if kb
          }//void

至于掉下来的角色……给你。

矩阵下降数字

如果您在 Windows 上,则代码:

/* The Matrix  falling numbers */

#include <iostream>
#include <windows.h> 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
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;i<XMAX;i++) y_coord[i]=   rand() % YMAX;

  while(!QuitGame)
  {
      /* simple keyboard input */
      simple_keyboard_input();

      /* draw falling numbers */
      draw_falling_numbers();

  }

  /* restore text color */
  setcolor(7);
  clrscr( );
  cout<<" \n";

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}

/* functions  */

void draw_falling_numbers()
{

    for(x=0;x<=XMAX;x++)
    {
        /* generate random number */
        int MatixNumber=rand() % 2 ;

        /* update falling number */
        y_coord[x]=y_coord[x]+1;

        if (y_coord[x]>YMAX) y_coord[x]=0;

        /* draw dark color */
        setcolor(2);
        gotoxy(x ,y_coord[x]-1); cout<<"  "<<MatixNumber<<"   ";

        /* draw light color */
        setcolor(10);
        gotoxy(x ,y_coord[x]); cout<<"  "<<MatixNumber<<"   ";
    }
    /* wait some milliseconds */
    Sleep(50);
    //clrscr( );
}


void draw_platform()
{
  setcolor(7);
 gotoxy(platformX ,platformY);cout<<"       ";

 gotoxy(platformX ,platformY);cout<<"ÜÜÜÜÜÜ";
 setcolor(7);
 Sleep(5);
}




void simple_keyboard_input()
{
    if (kbhit())
      {
            KB_code = getch();
            //cout<<"KB_code = "<<KB_code<<"\n";

            switch (KB_code)
            {

                case KB_ESCAPE:

                    QuitGame=true;

                break;

                case KB_LEFT:
                           //Do something
                    platformX=platformX-4;if(platformX<3) platformX=3;
                break;

                case KB_RIGHT:
                           //Do something     
                    platformX=platformX+4;if(platformX>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);
}
于 2014-05-17T18:55:57.390 回答