initscr() 搞乱了显示
initscr() is messing up display
我是 C++ 和 ncurses 的新手。当我将 initscr()
添加到我的代码以允许用户输入来控制播放器时,显示屏在启动时不显示,然后当您按下按钮时显示屏会显示,但它变得很奇怪。仅当我将 initscr()
添加到我的 main()
方法时才会发生这种情况。为什么会这样,我该如何解决?
#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() {
//initscr();
Setup();
while(!gameOver) {
Draw();
Input();
Logic();
}
//endwin();
return 0;
}
您将 curses 与非 curses 屏幕控制混合在一起,看到了库之间冲突的结果。
如果你要使用诅咒,就用那个。仔细阅读 NCURSES Programming HOWTO 以获取有关正确操作的信息。 (那里的文档也适用于大多数 PDCurses。)
tl;dr:摆脱 system()
对屏幕执行操作的调用,并使用 *addstr()
和 *printw()
函数打印输出。别忘了 [w]refresh()
.
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/attrib.html 使用 ncurses 并包含一个使用 clear() 的示例。
可以在以下位置找到其他示例:
https://www.gnu.org/software/ncurses/
http://invisible-island.net/ncurses/ncurses.html
http://www.c-for-dummies.com/ncurses/source_code/index.php(示例)
我是 C++ 和 ncurses 的新手。当我将 initscr()
添加到我的代码以允许用户输入来控制播放器时,显示屏在启动时不显示,然后当您按下按钮时显示屏会显示,但它变得很奇怪。仅当我将 initscr()
添加到我的 main()
方法时才会发生这种情况。为什么会这样,我该如何解决?
#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() {
//initscr();
Setup();
while(!gameOver) {
Draw();
Input();
Logic();
}
//endwin();
return 0;
}
您将 curses 与非 curses 屏幕控制混合在一起,看到了库之间冲突的结果。
如果你要使用诅咒,就用那个。仔细阅读 NCURSES Programming HOWTO 以获取有关正确操作的信息。 (那里的文档也适用于大多数 PDCurses。)
tl;dr:摆脱 system()
对屏幕执行操作的调用,并使用 *addstr()
和 *printw()
函数打印输出。别忘了 [w]refresh()
.
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/attrib.html 使用 ncurses 并包含一个使用 clear() 的示例。
可以在以下位置找到其他示例:
https://www.gnu.org/software/ncurses/
http://invisible-island.net/ncurses/ncurses.html
http://www.c-for-dummies.com/ncurses/source_code/index.php(示例)