initscr()搞乱了显示

时间:2017-09-29 20:18:53

标签: c++ ncurses

我是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;
}

2 个答案:

答案 0 :(得分:2)

您正在将curses与非curses屏幕控制混合,并查看库之间的冲突结果。

如果您打算使用curses,只需使用它即可。仔细阅读NCURSES Programming HOWTO以获取有关正确执行此操作的信息。 (该文档也适用于大多数 PDCurses 。)

tl; dr:摆脱system()调用以在屏幕上执行操作,并使用*addstr()*printw()函数打印输出。不要忘记[w]refresh()

答案 1 :(得分:0)

相关问题