(如何读取箭头键)C编程中的作弊码(从键盘输入时)

我在C中制作游戏,当用户输入Konami代码(作弊码)时,程序应打印出正确的答案。 (编辑#3)发现我的程序在输入时没有“读取”箭头键,我该怎么做呢? 请。 请参阅下面的评论

这是我更新的代码(编辑#2)://尝试konami

#include #include main() { int c; char cheat[] = {24,24,25,25,27,26,27,26,98,97}; //thanks to Vicky for clarifying this char guess[100]; printf("Enter guess: "); scanf("%c", &guess); //just checking if the cheat array is right, and yes it is. I'll remove this later for ( c = 0; c < 11; c++ ) { printf("%c", cheat[c]); } //comparison of the guess (input) to the cheat code if (strcmp (cheat, guess) == 0 ) printf("\nYou win!"); else printf("\nLol!"); } 

现在,我的问题是它总是打印Lol! 即使我正确输入了作弊码。 。 。

注意:这是我编程的介绍类(我第一次在c中)。 到目前为止所涵盖的课程是基础知识,循环,数组和字符串(没有function但我明白现在,没有结构和更多请。谢谢:)

my question is how do i check it coming from the user

我听说你想知道用户何时按下箭头键? 我必须在C ++中做到这一点,结果certificate这是一个非常有启发性的经历,所以尽管你使用C而不是C ++,我仍然会分享我记得的东西。

Arrow keys采用一系列字节而不是一个AND通常在硬件级别实现!

虽然您使用的操作系统应该具有支持(再见可移植性),但您需要寻找的术语是key codes ,在windows它们称之为虚拟键代码

Windows我学会了用win32api编程。

我可以很容易地向您显示Windows的key codes (我将从该链接粘贴它们)但它们对您没有好处,除非您想要提交到Windows事件驱动型程序(也许您已经是?),这些代码是从你的程序中的key事件中取出并且不是ascii-codes ,我会尽量假设特定于windows

 VK_LEFT 0x25 LEFT ARROW key VK_UP 0x26 UP ARROW key VK_RIGHT 0x27 RIGHT ARROW key VK_DOWN 0x28 

在linux中,我仍然不得不处理事件,这次使用X11,哦,我与X11的欢乐! 这里的XK_[key]代码叫做XK_[key]XK_left

这些键代码在头文件 – Xlib编程手册中定义

在线查看标题

 #define XK_Left 0xff51 /* Move left, left arrow */ #define XK_Up 0xff52 /* Move up, up arrow */ #define XK_Right 0xff53 /* Move right, right arrow */ #define XK_Down 0xff54 /* Move down, down arrow */ 

希望这可以让你在正确的道路上,每当我需要arrow keys现在我只是使用wasd


编辑:这是Windows的一个,我从我对这个C程序制作的C ++游戏中浓缩了它,这是很多代码,任何看起来不应该在那里的东西可能不应该,但它编译并读取箭头键,所以你需要的所有东西都至少在这个来源中。

视窗;

 #include  #include  #include  #include  //directional keys #define key_left 0x25 #define key_up 0x26 #define key_right 0x27 #define key_down 0x28 // to quit #define key_escape 0x1B char checkType(INPUT_RECORD *buffer, int i); int checkKey(INPUT_RECORD *buffer, int i); void outputString(const char *str, SHORT col, SHORT row); void outputChar(char ch, SHORT col, SHORT row); void clearScreen(void); CHAR_INFO *p_consoleBuffer; HANDLE whnd; // handle to write console HANDLE rhnd; // handle to read console SMALL_RECT winSize; COORD buffSize; SHORT consoleHeight = 25; SHORT consoleWidth = 60; DWORD Events = 0; // Event count DWORD EventsRead = 0; // Events read from console int _tmain(int argc, _TCHAR* argv[]){ // set up handles for read/writing: whnd = GetStdHandle(STD_OUTPUT_HANDLE); rhnd = GetStdHandle(STD_INPUT_HANDLE); SetConsoleTitle(TEXT("DEMO WIN PROG")); SMALL_RECT winLen = {0,0,((SHORT)consoleWidth-1),((SHORT)consoleHeight-1)}; COORD bLen = {consoleWidth,consoleHeight}; // width / height winSize = winLen; // set console size SetConsoleWindowInfo(whnd, TRUE, &winSize); // set the buffer size coords SetConsoleScreenBufferSize(whnd, buffSize); CHAR_INFO consoleBuffer[consoleHeight*consoleWidth]; p_consoleBuffer = consoleBuffer; CONSOLE_CURSOR_INFO cci; cci.dwSize = 1; cci.bVisible = FALSE; SetConsoleCursorInfo(whnd, &cci); clearScreen(); int Running = 1; char type = 0; while(Running) { //game loop GetNumberOfConsoleInputEvents(rhnd, &Events); if(Events != 0){ // something happened // create buffer the size of how many Events & store INPUT_RECORD eventBuffer[Events]; // fills buffer with events and save count in EventsRead ReadConsoleInput(rhnd, eventBuffer, Events, &EventsRead); DWORD i; for(i = 0; i 

LINUX:(X11)这是我使用的示例程序,它实际上是我第一次开始编程时我想自己做这个,而且我不记得它来自哪里或者是谁写的,所以我不能不记得他们,但是这是源头;

 #include  #include  #include  #include  #include  #include  #include  /*Linux users will need to add -ldl to the Makefile to compile *this example. */ Display *dis; Window win; XEvent report; GC green_gc; XColor green_col; Colormap colormap; /* Try changing the green[] = below to a different color. The color can also be from /usr/X11R6/lib/X11/rgb.txt, such as RoyalBlue4. A # (number sign) is only needed when using hexadecimal colors. */ char green[] = "#00FF00"; int main() { dis = XOpenDisplay(NULL); win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), BlackPixel(dis, 0)); XMapWindow(dis, win); colormap = DefaultColormap(dis, 0); green_gc = XCreateGC(dis, win, 0, 0); XParseColor(dis, colormap, green, &green_col); XAllocColor(dis, colormap, &green_col); XSetForeground(dis, green_gc, green_col.pixel); XSelectInput(dis, win, ExposureMask | KeyPressMask | ButtonPressMask); XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497); XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398); XFlush(dis); while (1) { XNextEvent(dis, &report); switch (report.type) { case Expose: fprintf(stdout, "I have been exposed.\n"); XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497); XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398); XFlush(dis); break; case KeyPress: /*Close the program if q is pressed.*/ if (XLookupKeysym(&report.xkey, 0) == XK_q) { exit(0); } break; } } return 0; }