为什么不用颜色在ncurses中工作?

时间:2018-01-06 22:58:44

标签: c colors ncurses

我几乎完成了我的项目,但我无法改变任何颜色的窗户或文字 我尝试了attronattroff,但它不起作用。全部编译,但没有任何反应,仍然是默认颜色。 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;
}

1 个答案:

答案 0 :(得分:3)

在这个块中

    attron(COLOR_PAIR(1));
    mvwaddch(win, obj2_y, obj2_x, ' ');

您已在stdscr中启用了颜色对1,但正在向win添加字符(不同的窗口)。 attron仅影响您在其上设置属性的窗口。这可能是你所看到的问题。

在另一个地方,您正在执行getch(对于stdscr),这可能会影响win的刷新,但您似乎在&#39 ; ve添加了wrefresh来弥补这一点。