我对编程完全陌生。我正在尝试制作一个非常基本的 Ncurses 游戏。我的问题是我有一些非常重复的代码,我在其中获取一个字符串,计算它的长度,将它除以 2,然后从列数中减去。我这样做是为了让我的文本在屏幕上居中。我想通过制作一个函数来简化这件事,但我不知道如何制作一个返回 Ncurses 函数的函数 mvprintw(y,x,string)
这是我的代码,以便您更好地理解:
#include <iostream>
#include <ncurses.h>
#include <string.h>
int main(){
initscr();
int x,y;
getmaxyx(stdscr, y, x);
mvprintw(0,x/2-(strlen("************")/2), "************");
mvprintw(1,x/2-(strlen("Welcome")/2), "Welcome");
mvprintw(2,x/2-(strlen("************")/2), "************");
refresh();
getch();
endwin();
return 0;
}
最佳答案
您弄清楚了要执行的操作所依赖的参数,因此您知道要传递什么。然后就像编写操作一样简单,同时用参数名称替换实际参数。
void center_text(int y, int x, char const* text) {
mvprintw(0,x/2-(strlen(text)/2), text);
}
一旦你有了它,就使用它:
getmaxyx(stdscr, y, x);
center_text(0, x, "************");
center_text(1, x, "Welcome");
center_text(2, x, "************");
关于c++ - 如何制作一个返回 Ncurses 函数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40556377/