使用 ncurses 为游戏创建 "main" window
Creatin a "main" window with ncurses for a game
我正在尝试创建一个 window 作为我整个游戏的边框。所以它将是 "main" window。我发现最好的方法是创建一个 "ghost" window 和另一个你将使用的真实的,在它的内部尺寸略小。
但我仍然不能正确地创建它:这是我最好的解决方案。
#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;
}
back_window没有出现,因为real_window遮住了它(由于size/position).但是使用 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;
}
我正在尝试创建一个 window 作为我整个游戏的边框。所以它将是 "main" window。我发现最好的方法是创建一个 "ghost" window 和另一个你将使用的真实的,在它的内部尺寸略小。
但我仍然不能正确地创建它:这是我最好的解决方案。
#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;
}
back_window没有出现,因为real_window遮住了它(由于size/position).但是使用 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;
}