在C语言编程(从键盘输入时)(如何阅读箭头键)作弊code [英] (How to read arrow keys) Cheat code in C programming (when inputted from the keyboard)
问题描述
我用C做一个游戏,当用户输入科纳米code(作弊code),程序应打印出正确的答案。
(编辑#3)发现,我的程序没有读箭头键每当我输入它,我怎么做会这样呢?请。见下文
I am making a game in C, and when the user input the Konami code (cheat code), the program should print out the correct answer. (edit #3) found out that my program does not "read" the arrow keys whenever I input it, how do I make it so? pls. see my comment below
下面是我更新code(编辑#2):
//尝试KONAMI
Here is my updated code (edit #2): //try konami
#include<stdio.h>
#include<string.h>
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!");
}
现在,我的问题是,它总是打印大声笑!即使我输入作弊code正确。 。
Now, my problem is it always prints Lol! even though I input the cheat code correctly . . .
请注意:这是我的介绍类节目(我在C第一次过)。到目前为止,涉及教训是基础,循环,数组和字符串(没有功能了,但是我明白,现在,没有结构进一步PLS谢谢。)
note: this is for my intro class in programming (my first time in c too). lessons covered so far are basics, loops, arrays and strings (no functions yet but i understood that now, no structures and further pls. thank you :)
推荐答案
我的问题是我如何检查来自用户
我听到你想知道,当用户presses箭头键?我不得不做这在C ++,它原来是一个非常有启发性的经验,所以虽然你使用C而不是C ++我仍然会分享我记住了。
I'm hearing that you want to know when the user presses an arrow key? I had to do this in C++ and it turned out to be a very enlightening experience, so although you are using C and not C++ i will still share what i remember.
箭头键
字节需要的,而不是一个序列中的一个和通常在硬件级实现的!
Arrow keys
take a sequence of bytes instead of one AND are usually implemented at the hardware level!
虽然任何操作使用的是应该有支持(再见可移植性)系统,你需要寻找的期限为键codeS
和窗口
招呼他们的虚拟键codeS
Though whatever operating system you are using should have support (goodbye portability), the term you need to be looking for is key codes
and in windows
they call them Virtual-Key Codes
在窗口
我不得不学会与WIN32API编程。
On Windows
I had learn to program with the win32api.
我可以轻松地显示你的键codeS
的Windows(和我将他们从该链接粘贴),但他们没有对你的好,除非你想提交到Windows事件驱动型的程序(也许你已经是?),这些codeS是从键获取在你的程序
事件,并不 ascii- codeS
,我会去尽量承担具体到窗口
。
I can easily show you the key codes
for windows (and i will paste them from that link) but they are no good to you unless you want to commit to a windows event driven type program (maybe you already are?), these codes are fetched from the key
events in your program and are not ascii-codes
and i would go as far to assume specific to 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仍然不得不面对的事件,这一次!关键codeS这里被称为 XK_ [关键]
即 XK_left
。
这些关键codeS被定义在头文件&LT; X11 / keysymdef.h&GT;
- 的Xlib编程手册
These key codes are defined in the header <X11/keysymdef.h>
- Xlib programming manual
请参见头在线
#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 */
希望这可以设置在正确的道路上,每当我需要箭头
现在我只是使用 WASD
。
编辑:这是Windows的,我凝结下来从C ++游戏我这个C程序做,这是一个很大code的,任何看起来像它不应该存在可能不应该,但它编译并读取方向键,让你需要的一切是在这个源ATLEAST。
This is the windows one, i condensed it down from the C++ game i made to this C program,it's a lot of code, anything that looks like it shouldn't be there probably shouldn't, but it compiles and reads the arrow keys, so everything you need is in this source atleast.
WINDOWS;
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <Tchar.h>
//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<EventsRead; ++i){
type = checkType(eventBuffer,i);
if(type == 'k'){ // event type was key
int key = checkKey(eventBuffer, i);
int arrow_key_pressed = FALSE;
switch (key){
case key_up:
case key_down:
case key_left:
case key_right:
// IF ANY ARROW KEY WAS PRESSED, EXECUTION WILL "COLLAPSE" TO HERE
arrow_key_pressed = TRUE;
break;
case key_escape:
// escape was pressed
Running = 0;
break;
default:
// no case was pressed
break;
}
clearScreen();
if(arrow_key_pressed){
const char *str = "Arrow key was pressed";
outputString(str, 20, 12); // roughly centre
}else {
const char *str = "Arrow key was not pressed";
outputString(str, 20, 12);
}
}else if(type == 'm'){
// MOUSE CLICK, I'M NOT COVERING SORRY.
}
}
}
}
return 0;
}
char checkType(INPUT_RECORD *buffer, int i){
if(buffer[i].EventType == MOUSE_EVENT){
return ('m');
}else if(buffer[i].EventType == KEY_EVENT){
return ('k');
}else return (0);
}
int checkKey(INPUT_RECORD *buffer, int i){
return (buffer[i].Event.KeyEvent.wVirtualKeyCode);
}
void outputString(const char *str, SHORT col, SHORT row){
SHORT fs_len = strlen(str);
int i;
for(i = 0; i<fs_len; ++i){
outputChar(str[i], col++, row);
}
}
void outputChar(char ch, SHORT col, SHORT row){
p_consoleBuffer[row*consoleWidth+col].Char.AsciiChar = ch;
p_consoleBuffer[row*consoleWidth+col].Attributes = 240;
SHORT CH = consoleHeight, CW = consoleWidth;
COORD charBufSize = {CW,CH};
COORD charPos = {0,0};
SMALL_RECT writeArea = {0,0,CW,CH};
WriteConsoleOutputA(whnd,p_consoleBuffer,charBufSize,charPos,&writeArea);
}
void clearScreen(void){
int i;
for(i = 0; i<(consoleHeight*consoleWidth); ++i){
//fill it with white-backgrounded spaces
p_consoleBuffer[i].Char.AsciiChar = ' ';
p_consoleBuffer[i].Attributes =
BACKGROUND_BLUE |
BACKGROUND_GREEN |
BACKGROUND_RED |
BACKGROUND_INTENSITY;
}
// clear screen
SHORT CH = consoleHeight, CW = consoleWidth;
COORD charBufSize = {CW,CH};
COORD charPos = {0,0};
SMALL_RECT writeArea = {0,0,CW,CH};
WriteConsoleOutputA(whnd,p_consoleBuffer,charBufSize,charPos,&writeArea);
}
/* Remember i did this in c++, i made a class with these ints
to represent the virtual key codes, you can define them how you
want like i did or use windows actual definitions.
//special keys
key_escape = 0x1B;
key_space = 0x20;
key_home = 0x24;
key_end = 0x23;
key_pgUp = 0x21;
key_pgDown = 0x22;
key_caps = 0x13;
key_shift = 0x10;
key_ctrl = 0x11;
key_backspace = 0x08;
key_tab = 0x09;
key_enter = 0x0D;
key_alt = 0x12;
key_delete = 0x2E;
//directional keys
key_left = 0x25;
key_up = 0x26;
key_right = 0x27;
key_down = 0x28;
//number keys
key_0 = 0x30;
key_1 = 0x31;
key_2 = 0x32;
key_3 = 0x33;
key_4 = 0x34;
key_5 = 0x35;
key_6 = 0x36;
key_7 = 0x37;
key_8 = 0x38;
key_9 = 0x39;
// alphabet keys
key_a = 0x41;
key_b = 0x42;
key_c = 0x43;
key_d = 0x44;
key_e = 0x45;
key_f = 0x46;
key_g = 0x47;
key_h = 0x48;
key_i = 0x49;
key_j = 0x4A;
key_k = 0x4B;
key_l = 0x4C;
key_m = 0x4D;
key_n = 0x4E;
key_o = 0x4F;
key_p = 0x50;
key_q = 0x51;
key_r = 0x52;
key_s = 0x53;
key_t = 0x54;
key_u = 0x55;
key_v = 0x56;
key_w = 0x57;
key_x = 0x58;
key_y = 0x59;
key_z = 0x5A;
*/
LINUX:(X11)这是我使用的示例程序,它实际上是在我刚开始编程,我想这样做我自己,我不记得它是从哪里来的或者是谁写的,所以我不能归功于他们不幸的是,但这里是源;
LINUX: (X11) This is that example program i used, it was actually when i first started programming that i wanted to do this myself, and i cannot remember where it came from or who wrote it, so i cannot credit them unfortunately, but here is the source;
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
/*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;
}
这篇关于在C语言编程(从键盘输入时)(如何阅读箭头键)作弊code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!