在哪里放置initscr()? 我正在试着用C++做一个贪吃蛇游戏。我正在使用ncurses包进行密钥输入,我知道我需要将initscr()放在某个地方以启用密钥输入,但我不知道放在哪里。我在主方法的顶部尝试了,但这导致了显示出现意外问题。我还试着将它放在Input()方法中,但这也会导致显示出现问题。我不知道该把它放在哪里,或者我是否需要添加其他东西来让它工作,但是有人能帮我吗 #include <iostream> #include <curses.h> using namespace std; bool gameOver; bool pressed; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("clear"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else cout << " "; if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; } void Input() { switch (getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } void Logic() { switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } } int main() { Setup(); while(!gameOver) { Draw(); Input(); Logic(); } return 0; } #包括 #包括 使用名称空间std; 布尔·加莫弗; 布尔压榨; const int width=20; const int height=20; 整数x,y,水果x,水果,分数; 枚举eDirection{STOP=0,左、右、上、下}; 编辑主任; 无效设置(){ gameOver=false; dir=停止; x=宽度/2; y=高度/2; 果x=rand()%宽度; 果味=兰特()%高度; 得分=0; } 作废提款(){ 系统(“清除”); 对于(int i=0;i

在哪里放置initscr()? 我正在试着用C++做一个贪吃蛇游戏。我正在使用ncurses包进行密钥输入,我知道我需要将initscr()放在某个地方以启用密钥输入,但我不知道放在哪里。我在主方法的顶部尝试了,但这导致了显示出现意外问题。我还试着将它放在Input()方法中,但这也会导致显示出现问题。我不知道该把它放在哪里,或者我是否需要添加其他东西来让它工作,但是有人能帮我吗 #include <iostream> #include <curses.h> using namespace std; bool gameOver; bool pressed; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("clear"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else cout << " "; if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; } void Input() { switch (getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } void Logic() { switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } } int main() { Setup(); while(!gameOver) { Draw(); Input(); Logic(); } return 0; } #包括 #包括 使用名称空间std; 布尔·加莫弗; 布尔压榨; const int width=20; const int height=20; 整数x,y,水果x,水果,分数; 枚举eDirection{STOP=0,左、右、上、下}; 编辑主任; 无效设置(){ gameOver=false; dir=停止; x=宽度/2; y=高度/2; 果x=rand()%宽度; 果味=兰特()%高度; 得分=0; } 作废提款(){ 系统(“清除”); 对于(int i=0;i,c++,ncurses,C++,Ncurses,initscr应该被称为“早”(在任何I/O之前)。但是,您的示例显示了getch(可能是curses,可能是conio)和stdio的混合(除非刷新输出,来回切换到curses,否则无法工作) 你运行代码,告诉我你注意到了什么(或者如果你是cba:它会破坏显示,正如问题中所说的那样)。 int main() { initscr(); // this Setup(); while(!gameOver) { Draw(); Input();

initscr
应该被称为“早”(在任何I/O之前)。但是,您的示例显示了
getch
(可能是curses,可能是conio)和stdio的混合(除非刷新输出,来回切换到curses,否则无法工作)


你运行代码,告诉我你注意到了什么(或者如果你是cba:它会破坏显示,正如问题中所说的那样)。
int main() {
    initscr(); // this
    Setup();
    while(!gameOver) {
        Draw();
        Input();
        Logic();
    }
    endwin(); // and this
    return 0;
}