Initscr()正在混乱显示

initscr() is messing up display

本文关键字:混乱 显示 Initscr      更新时间:2023-10-16

我是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(示例)