Creatin a" main"一个游戏的ncurses窗口

时间:2017-07-24 17:44:16

标签: window ncurses

我试图创建一个窗口作为我整个游戏的边界。所以它将是"主要"窗口。我发现最好的办法就是创造一个幽灵"窗口和另一个,你正在使用的真实窗口,尺寸稍微小一点。

但我仍然无法做到这一点:这是我最好的解决方案。

#include <ncurses.h>

WINDOW* createWindow(int, int, int, int);
WINDOW* createRealWindow(int, int, int, int);

int main ()
{
    initscr();
    cbreak();

    char str[50];
    WINDOW *back_window, *real_window;
    int height=12, width=80;
    int x=(COLS-width)/2, y=(LINES-height)/2;

    back_window = createWindow(x, y, width, height);
    real_window = createRealWindow(x+1, y+1, width, height);
    wprintw(real_window, "Type: ");
    wgetstr(real_window, str);
    wrefresh(real_window);
    wprintw(real_window, "You typed %s.", str);
    wrefresh(real_window);

    napms(5000);
    endwin();

    return 0;
}

WINDOW* createWindow(int x, int y, int height, int width)
{
    WINDOW *local;

    local = newwin(width, height, y, x);
    box(local, 0, 0);
    wrefresh(local);

    return local;
}

WINDOW* createRealWindow(int x, int y, int height, int width)
{
    WINDOW *local;

    local = newwin(width-2, height-2, y+1, x+1);
    box(local, ' ', ' ');
    wrefresh(local);

    return local;
}

这是我的输出: my output

谢谢你们!

编辑:我已经找到了下面带有代码的解决方案,但我不知道这会在其他具有不同分辨率的计算机中表现如何。想法? PS:我使用1920 x 1080。

int main ()
{
    initscr();
    cbreak();

    char str[50];
    WINDOW *back_window, *real_window;
    int height=22, width=78;
    int x=(COLS-width-1)/2, y=(LINES-height-2)/2;

    back_window = createWindow(x, y, width, height);
    real_window = createRealWindow(x+1, y+1, width, height);
    wprintw(real_window, "Type: ");
    wgetstr(real_window, str);
    wrefresh(real_window);
    wprintw(real_window, "You typed %s.", str);
    wrefresh(real_window);

    napms(5000);
    endwin();

    return 0;
}

WINDOW* createWindow(int x, int y, int height, int width)
{
    WINDOW *local;

    local = newwin(width+2, height+2, y, x);
    box(local, 0, 0);
    wrefresh(local);

    return local;
}

WINDOW* createRealWindow(int x, int y, int height, int width)
{
    WINDOW *local;

    local = newwin(width, height, y, x);
    wrefresh(local);

    return local;
}

1 个答案:

答案 0 :(得分:0)

back_window 未显示,因为 real_window 模糊了它(由于大小/位置)。但使用subwin是首选方法:

#include <ncurses.h>

int main ()
{
    initscr();
    cbreak();

    char str[50];
    WINDOW *back_window, *real_window;
    int height=12, width=80;
    int x=(COLS-width)/2, y=(LINES-height)/2;

    back_window = newwin(height, width, y, x);
    box(back_window, 0, 0);
    wrefresh(back_window);

    real_window = subwin(back_window, height-2, width-2, y+1, x+1);

    wprintw(real_window, "Type: ");
    wgetstr(real_window, str);
    wrefresh(real_window);
    wprintw(real_window, "You typed %s.", str);
    wrefresh(real_window);

    napms(5000);
    endwin();

    return 0;
}