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屏幕控制并查看库之间的冲突结果。
如果要使用诅咒,请使用它。仔细阅读ncurses编程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(示例)
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何克服提升精神AST混乱
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Arduino LCD 显示屏显示混乱的字母
- Initscr()正在混乱显示
- 访问Ximea相机并使用OpenCV设置预定义的分辨率时,由于相机默认分辨率的大小为Mat,因此显示混乱的输出