为什么颜色在 ncurses 中不起作用?
Why don't colours work in ncurses?
我几乎完成了我的项目,但是我无法更改 windows 或文本的任何颜色。
我尝试了 attron
和 attroff
但它不起作用。一切都已编译,但什么也没发生,仍然是默认颜色。 Wattron 也不行。
#include "stdio.h"
#include "ncurses.h"
#include "math.h"
#include "unistd.h"
#include "stdlib.h"
double PI = 3.15927;
bool drawObject(WINDOW *win, int **obj, int size, int y, int x);
bool clearObject(WINDOW *win, int **obj, int size, int y, int x);
bool drawPoint(WINDOW *win, int y, int x, char c);
bool drawPoint(WINDOW *win, int y, int x, char c)
{
bool r = mvwinch(win, y, x) != 0x20;
mvwaddch(win, y, x, c);
return r;
}
bool drawObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
bool r = false;
for (k = 0; k < size; ++k)
{
r |= drawPoint(win, y + obj[k][0], x + obj[k][1], (char)obj[k][2]);
}
wrefresh(win);
return r;
}
bool clearObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
for (k = 0; k < size; ++k)
{
mvwaddch(win, y + obj[k][0], x + obj[k][1], ' ');
}
wrefresh(win);
}
int main(int argc, char **argv)
{
int k = 0;
WINDOW *win, *status;
int ox = 40, oy = 20;
bool isCrash = false;
int obj1_size = 6;
int **obj1_def = (int**)malloc(sizeof(int*) * obj1_size);
for (k = 0; k < obj1_size; ++k)
{
obj1_def[k] = (int*)malloc(sizeof(int) * 3);
}
obj1_def[0][0] = 0;
obj1_def[0][1] = 1;
obj1_def[0][2] = (int)'\';
obj1_def[1][0] = 0;
obj1_def[1][1] = -1;
obj1_def[1][2] = (int)'/';
obj1_def[2][0] = 1;
obj1_def[2][1] = 0;
obj1_def[2][2] = (int)'W';
obj1_def[3][0] = 0;
obj1_def[3][1] = 0;
obj1_def[3][2] = (int)'H';
obj1_def[4][0] = -1;
obj1_def[4][1] = 0;
obj1_def[4][2] = (int)'H';
obj1_def[5][0] = -2;
obj1_def[5][1] = 0;
obj1_def[5][2] = (int)'A';
double obj1_step = PI / 50;
double obj1_t = 1.5;
int obj1_a = 4;
int obj1_x = 0, obj1_y = 0;
double obj2_step = PI / 20;
double obj2_t = 0;
int obj2_a = 3;
int obj2_b = 10;
int obj2_x = 0, obj2_y = 0;
initscr();
refresh();
startcolor();
init_pair(1, COLOR_RED, COLOR_BLACK);
win = newwin(40, 80, 0, 0);
status = newwin(2, 80, 40, 0);
wrefresh(win);
wrefresh(status);
while (true)
{
//1 object
clearObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
obj1_x =(int)(3 * obj1_t * sin(obj1_t * 0.5) * cos(obj1_t));
obj1_y =(int)(2 * obj1_t * cos(obj1_t * 1.5) * cos(obj1_t));
obj1_x += 50;
obj1_y += 20;
if (obj1_x < 1)
{
obj1_x = 1;
}
if (obj1_x > 78)
{
obj1_x = 78;
}
if (obj1_y < 2)
{
obj1_y = 2;
}
if (obj1_y > 38)
{
obj1_y = 38;
}
isCrash = drawObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj1_t += obj1_step;
if (obj1_t > 17.215)
{
obj1_t = 1.5;
}
//2nd object
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
mvwaddch(win, obj2_y, obj2_x+1, ' ');
mvwaddch(win, obj2_y, obj2_x-1, ' ');
mvwaddch(win, obj2_y-1, obj2_x, ' ');
mvwaddch(win, obj2_y-1, obj2_x-1, ' ');
attroff(COLOR_PAIR(1));
wrefresh(win);
obj2_x =(int)(obj2_a * obj2_t - obj2_b * sin(obj1_t));
obj2_y =(int)(obj2_a - obj2_b * cos(obj1_t));
obj2_y += 20;
if (obj2_x < 1)
{
obj2_x = 1;
}
if (obj2_x > 78)
{
obj2_x = 78;
}
if (obj2_y < 2)
{
obj2_y = 2;
}
if (obj2_y > 38)
{
obj2_y = 38;
}
isCrash = mvwinch(win, obj2_y, obj2_x+1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x-1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x-1) != 0x20;
mvwaddch(win, obj2_y, obj2_x+1, '-');
mvwaddch(win, obj2_y, obj2_x-1, '-');
mvwaddch(win, obj2_y, obj2_x, '-');
mvwaddch(win, obj2_y-1, obj2_x, '\');
mvwaddch(win, obj2_y-1, obj2_x-1, '\');
wrefresh(win);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj2_t += obj2_step;
if (obj2_t > 30)
{
obj2_t = 0;
}
wmove(win,0,0);
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
wrefresh(win);
wrefresh(status);
usleep(100000);
}
wrefresh(status);
getch();
delwin(win);
delwin(status);
endwin();
for (k = 0; k < obj1_size; ++k)
{
free(obj1_def[k]);
}
free(obj1_def);
return 0;
}
在这个区块中
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
您在 stdscr
中打开了 color-pair-1,但正在向 win
添加字符(不同的 window)。 attron
仅影响您设置属性的 window。这可能就是您遇到的问题。
在另一个地方,你正在做 getch
(对于 stdscr
),这可能会干扰 win
的刷新,但你似乎已经添加了 [=17] =] 来弥补这一点。
我几乎完成了我的项目,但是我无法更改 windows 或文本的任何颜色。
我尝试了 attron
和 attroff
但它不起作用。一切都已编译,但什么也没发生,仍然是默认颜色。 Wattron 也不行。
#include "stdio.h"
#include "ncurses.h"
#include "math.h"
#include "unistd.h"
#include "stdlib.h"
double PI = 3.15927;
bool drawObject(WINDOW *win, int **obj, int size, int y, int x);
bool clearObject(WINDOW *win, int **obj, int size, int y, int x);
bool drawPoint(WINDOW *win, int y, int x, char c);
bool drawPoint(WINDOW *win, int y, int x, char c)
{
bool r = mvwinch(win, y, x) != 0x20;
mvwaddch(win, y, x, c);
return r;
}
bool drawObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
bool r = false;
for (k = 0; k < size; ++k)
{
r |= drawPoint(win, y + obj[k][0], x + obj[k][1], (char)obj[k][2]);
}
wrefresh(win);
return r;
}
bool clearObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
for (k = 0; k < size; ++k)
{
mvwaddch(win, y + obj[k][0], x + obj[k][1], ' ');
}
wrefresh(win);
}
int main(int argc, char **argv)
{
int k = 0;
WINDOW *win, *status;
int ox = 40, oy = 20;
bool isCrash = false;
int obj1_size = 6;
int **obj1_def = (int**)malloc(sizeof(int*) * obj1_size);
for (k = 0; k < obj1_size; ++k)
{
obj1_def[k] = (int*)malloc(sizeof(int) * 3);
}
obj1_def[0][0] = 0;
obj1_def[0][1] = 1;
obj1_def[0][2] = (int)'\';
obj1_def[1][0] = 0;
obj1_def[1][1] = -1;
obj1_def[1][2] = (int)'/';
obj1_def[2][0] = 1;
obj1_def[2][1] = 0;
obj1_def[2][2] = (int)'W';
obj1_def[3][0] = 0;
obj1_def[3][1] = 0;
obj1_def[3][2] = (int)'H';
obj1_def[4][0] = -1;
obj1_def[4][1] = 0;
obj1_def[4][2] = (int)'H';
obj1_def[5][0] = -2;
obj1_def[5][1] = 0;
obj1_def[5][2] = (int)'A';
double obj1_step = PI / 50;
double obj1_t = 1.5;
int obj1_a = 4;
int obj1_x = 0, obj1_y = 0;
double obj2_step = PI / 20;
double obj2_t = 0;
int obj2_a = 3;
int obj2_b = 10;
int obj2_x = 0, obj2_y = 0;
initscr();
refresh();
startcolor();
init_pair(1, COLOR_RED, COLOR_BLACK);
win = newwin(40, 80, 0, 0);
status = newwin(2, 80, 40, 0);
wrefresh(win);
wrefresh(status);
while (true)
{
//1 object
clearObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
obj1_x =(int)(3 * obj1_t * sin(obj1_t * 0.5) * cos(obj1_t));
obj1_y =(int)(2 * obj1_t * cos(obj1_t * 1.5) * cos(obj1_t));
obj1_x += 50;
obj1_y += 20;
if (obj1_x < 1)
{
obj1_x = 1;
}
if (obj1_x > 78)
{
obj1_x = 78;
}
if (obj1_y < 2)
{
obj1_y = 2;
}
if (obj1_y > 38)
{
obj1_y = 38;
}
isCrash = drawObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj1_t += obj1_step;
if (obj1_t > 17.215)
{
obj1_t = 1.5;
}
//2nd object
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
mvwaddch(win, obj2_y, obj2_x+1, ' ');
mvwaddch(win, obj2_y, obj2_x-1, ' ');
mvwaddch(win, obj2_y-1, obj2_x, ' ');
mvwaddch(win, obj2_y-1, obj2_x-1, ' ');
attroff(COLOR_PAIR(1));
wrefresh(win);
obj2_x =(int)(obj2_a * obj2_t - obj2_b * sin(obj1_t));
obj2_y =(int)(obj2_a - obj2_b * cos(obj1_t));
obj2_y += 20;
if (obj2_x < 1)
{
obj2_x = 1;
}
if (obj2_x > 78)
{
obj2_x = 78;
}
if (obj2_y < 2)
{
obj2_y = 2;
}
if (obj2_y > 38)
{
obj2_y = 38;
}
isCrash = mvwinch(win, obj2_y, obj2_x+1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x-1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x-1) != 0x20;
mvwaddch(win, obj2_y, obj2_x+1, '-');
mvwaddch(win, obj2_y, obj2_x-1, '-');
mvwaddch(win, obj2_y, obj2_x, '-');
mvwaddch(win, obj2_y-1, obj2_x, '\');
mvwaddch(win, obj2_y-1, obj2_x-1, '\');
wrefresh(win);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj2_t += obj2_step;
if (obj2_t > 30)
{
obj2_t = 0;
}
wmove(win,0,0);
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
wrefresh(win);
wrefresh(status);
usleep(100000);
}
wrefresh(status);
getch();
delwin(win);
delwin(status);
endwin();
for (k = 0; k < obj1_size; ++k)
{
free(obj1_def[k]);
}
free(obj1_def);
return 0;
}
在这个区块中
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
您在 stdscr
中打开了 color-pair-1,但正在向 win
添加字符(不同的 window)。 attron
仅影响您设置属性的 window。这可能就是您遇到的问题。
在另一个地方,你正在做 getch
(对于 stdscr
),这可能会干扰 win
的刷新,但你似乎已经添加了 [=17] =] 来弥补这一点。