Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ N课程:箭头键不工作_C++_Ncurses_Arrow Keys - Fatal编程技术网

C++ N课程:箭头键不工作

C++ N课程:箭头键不工作,c++,ncurses,arrow-keys,C++,Ncurses,Arrow Keys,我正在尝试编写一个snake克隆,我刚刚开始编写代码,但是我在让键盘工作时遇到了一些问题。当我点击箭头键时,它似乎没有收到信号。这是我的密码 #include <iostream> #include <unistd.h> #include <ncurses.h> struct Snake{ int x, y; char s = 'O'; // logo } snake; int main() { initscr(); no

我正在尝试编写一个snake克隆,我刚刚开始编写代码,但是我在让键盘工作时遇到了一些问题。当我点击箭头键时,它似乎没有收到信号。这是我的密码

#include <iostream>
#include <unistd.h>
#include <ncurses.h>

struct Snake{
    int x, y;
    char s = 'O'; // logo
} snake;

int main()
{

    initscr();
    noecho();
    curs_set(0);
    keypad(stdscr, true);
    nodelay(stdscr, true);
    start_color();
    init_pair(1, COLOR_MAGENTA, COLOR_BLACK );
    attron(COLOR_PAIR(1));
    int HEIGHT, WIDTH;

    getmaxyx(stdscr, HEIGHT, WIDTH);

    for (int x = 0; x < WIDTH-1; x++)
        mvaddch(0, x, '*');

    for (int y = 0; y < HEIGHT-2; y++)
        mvaddch(y, WIDTH-1, '*');

    for (int x = 0; x < WIDTH-1; x++)
        mvaddch(HEIGHT-2, x, '*');

    for (int y = 0; y < HEIGHT-2; y++)
        mvaddch(y, 0, '*');


    snake.x = WIDTH/2;
    snake.y = HEIGHT/2;
    mvaddch(snake.y, snake.x, snake.s);
    refresh();


    char key;
    while((key = getch()) != 'q')
    {
        mvaddch(snake.y, snake.x, ' ');
        switch(key)
        {
        case KEY_RIGHT:
            snake.x +=1;    
            break;

        case KEY_LEFT:
            snake.x -=1;    
            break;

        case KEY_UP:
            snake.y -=1;    
            break;

        case KEY_DOWN:
            snake.y +=1; 
            break;
        }

        mvaddch(snake.y, snake.x, snake.s);

        usleep(100000);
        refresh();
    }

    getch();
    erase();
    endwin();
}
#包括
#包括
#包括
结构蛇{
int x,y;
char s='O';//徽标
}蛇;
int main()
{
initscr();
noecho();
curs_集(0);
键盘(stdscr,真);
节点延迟(stdscr,真);
启动颜色();
初始对(1,颜色为洋红色,颜色为黑色);
attron(颜色对(1));
int高度、宽度;
getmaxyx(stdscr、高度、宽度);
对于(int x=0;x
使用
wchar\u t
代替char来存储箭头键代码

看看这个:


中的底线是,
char
保证为ASCII字符集提供足够的空间,因为其数量接近256位。但是,Unicode编码需要比
char
更大的空间。

char
不够大,无法容纳
键,因为它是
char
范围之后开始的字符集的一部分

类似地,
wchar\u t
足够大,但是(请阅读手册页)或是给定示例的正确类型,正如编译器在询问时告诉您的那样


int
chtype
的大小也不一样,但出于实际目的,curses库使用的任何
int
都可以放入
int
)。

您应该始终使用
-Wall
进行编译(至少)。如果您这样做,编译器会警告您,
char
key
)不够大,无法容纳大小写的值。
key\u RIGHT
等。@rici感谢您,它正在工作。