Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么不';颜色在教室里不起作用吗?_C_Colors_Ncurses - Fatal编程技术网

C 为什么不';颜色在教室里不起作用吗?

C 为什么不';颜色在教室里不起作用吗?,c,colors,ncurses,C,Colors,Ncurses,我几乎完成了我的项目,但我无法更改窗口或文本的任何颜色。 我尝试了attron和attroff,但没有成功。全部已编译,但未发生任何事件,仍为默认颜色。沃特隆也不工作 #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 s

我几乎完成了我的项目,但我无法更改窗口或文本的任何颜色。
我尝试了
attron
attroff
,但没有成功。全部已编译,但未发生任何事件,仍为默认颜色。沃特隆也不工作

#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;
}
#包括“stdio.h”
#包括“ncurses.h”
#包括“math.h”
#包括“unistd.h”
#包括“stdlib.h”
双PI=3.15927;
bool drawObject(窗口*win,整数**obj,整数大小,整数y,整数x);
布尔clearObject(窗口*win,整数**obj,整数大小,整数y,整数x);
布尔支点(窗口*win,整数y,整数x,字符c);
布尔支点(窗口*win,整数y,整数x,字符c)
{
bool r=mvwinch(win,y,x)!=0x20;
mvwaddch(赢,y,x,c);
返回r;
}
bool drawObject(窗口*win,整数**obj,整数大小,整数y,整数x)
{
int k;
布尔r=假;
对于(k=0;k78)
{
obj1_x=78;
}
如果(obj1_y<2)
{
obj1_y=2;
}
如果(obj1_y>38)
{
obj1_y=38;
}
isCrash=drawObject(win、obj1_def、obj1_size、obj1_y、obj1_x);
如果(isCrash==true)
{
mvwprintw(状态,0,0,“[%d,%d][%d,%d]”,obj1_x,obj1_y,obj2_x,obj2_y);
mvwprintw(状态为1,0,“崩溃!!!”;
打破
}
obj1_t+=obj1_阶跃;
如果(obj1_t>17.215)
{
obj1_t=1.5;
}
//第二个目标
attron(颜色对(1));
mvwaddch(胜利,胜利,胜利,胜利);
mvwaddch(胜利,目标是,目标是x+1,);
mvwaddch(胜利,胜利,胜利,胜利);
mvwaddch(赢,obj2_y-1,obj2_x,”);
mvwaddch(win,obj2_y-1,obj2_x-1,”);
attroff(颜色对(1));
雷弗斯(赢);
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;
如果(obj2_x<1)
{
obj2_x=1;
}
如果(obj2_x>78)
{
obj2_x=78;
}
如果(obj2_y<2)
{
obj2_y=2;
}
如果(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||
MVJ绞车(win,obj2_y-1,obj2_x)!=0x20||
MVJ绞车(win,obj2_y-1,obj2_x-1)!=0x20;
mvwaddch(胜利,目标是,目标是+1’-);
mvwaddch(胜利,胜利,胜利,胜利);
mvwaddch(胜利,胜利,胜利,胜利);
mvwaddch(赢,obj2_y-1,obj2_x,“\\”);
mvwaddch(win,obj2_y-1,obj2_x-1,“\\”);
雷弗斯(赢);
如果(isCrash==true)
{
mvwprintw(状态,0,0,“[%d,%d][%d,%d]”,obj1_x,obj1_y,obj2_x,obj2_y);
mvwprintw(状态为1,0,“崩溃!!!”;
打破
}
obj2_t+=obj2_阶跃;
如果(obj2_t>30)
{
obj2_t=0;
}
wmove(win,0,0);
mvwprintw(状态,0,0,“[%d,%d][%d,%d]”,obj1_x,obj1_y,obj2_x,obj2_y);
雷弗斯(赢);
(身份);;
美国LEEP(100000);
}
(身份);;
getch();
德尔温(温);
德尔温(地位);
endwin();
用于(k=0;k
在此块中

    attron(COLOR_PAIR(1));
    mvwaddch(win, obj2_y, obj2_x, ' ');
您已在
stdscr
中打开了颜色对-1,但正在向
win
添加字符(另一个窗口)。
attron
仅影响正在设置属性的窗口。这可能就是你看到的问题

在另一个地方,您正在执行
getch
(用于
stdscr
),这可能会干扰
win
的刷新,但您似乎添加了
wrefresh
,以补偿这一点。

在此块中

    attron(COLOR_PAIR(1));
    mvwaddch(win, obj2_y, obj2_x, ' ');
您已在
stdscr
中打开了颜色对-1,但正在向
win
添加字符(另一个窗口)。代码