C-窗口/打印没有出现

时间:2014-07-10 19:28:39

标签: c ncurses

伙计们我不知道为什么没有出现窗口边框/打印件。我哪里错了?它正在预先工作。我添加了注释括号中的部分。即使我删除了这些部件,它也停止了工作。

#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int y = 2;
    int x = 2;
    int oldy = 2;
    int oldx = 2;
    char m = 'l';

    initscr();
    WINDOW* win;
    win = newwin(41,40, 0 ,0);
    /*WINDOW* win2 = newwin(10,10, 40 ,0);*/
    box(win, ACS_VLINE, ACS_HLINE);
    /*box(win2, ACS_VLINE, ACS_HLINE);*/
    mvwprintw(win,1, 1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    mvwprintw(win,2, 1, "X X   X     X   X          X       X X");
    mvwprintw(win,3, 1, "X X X X XXX X X X XXXXXXXX X XXXXX X X");
    mvwprintw(win,4, 1, "X X X     X   X X X          X     X X");
    mvwprintw(win,5, 1, "X XXXXXXX XXXXX X X XXXXXXXXXX XXXXX X");
    mvwprintw(win,6, 1, "X       X X     X X          X       X");
    mvwprintw(win,7, 1, "XXXXXXX X XXXXXXX XXXXXXXXXX XXXXXXXXX");
    mvwprintw(win,8, 1, "X       X X       X        X X       X");
    mvwprintw(win,9, 1, "X XXXXXXX X XXXXXXX XXXXXX   X XXXXX X");
    mvwprintw(win,10, 1, "X X       X X             XXXX X X   X");
    mvwprintw(win,11, 1, "X X XXXXX X XXXX XXXXXXXX      X X XXX");
    mvwprintw(win,12, 1, "X X X     X XXXX X       XXXXXXX X X X");
    mvwprintw(win,13, 1, "X   X XXX X    X X XXXXX X   X X   X X");
    mvwprintw(win,14, 1, "XXXXX X X XXXX X   X   X X X X XXXXX X");
    mvwprintw(win,15, 1, "X     X X X    XXXXX X X X X X     X X");
    mvwprintw(win,16, 1, "X XXXXX X X XXXX     X X X X XXXXX X X");
    mvwprintw(win,17, 1, "X X     X X    X XXXXX     X         X");
    mvwprintw(win,18, 1, "X X XXXXX XXXX X X   XXXXXXXXXXXXXXXXX");
    mvwprintw(win,19, 1, "X   X          X   X                 O");
    mvwprintw(win,20, 1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    wmove(win,2,2);
    wrefresh(win);
    /*wrefresh(win2);*/


    while(m != 'q')
    {
            /*mvwprintw(win2,41,0,"22");*/
            /*wrefresh(win2);*/
            m=getch();
            /*mvwprintw(win2,40,0,"Please use the following keys to navigate through the maze.(W) Up (S) Down (D) Right (A) Left");*/
            wrefresh(win);

    switch(m)
        {
            case 'S': if(mvwinch(win,(oldy+1),x)=='X')
                        {
                        mvwprintw(win,40,0,"Sorry, you cant move there.");  
                        wrefresh(win);
                        wmove(win,oldy,oldx);
                        wrefresh(win);
                        }

                      else
                        {
                        wmove(win,(y=oldy+1),x);
                        wrefresh(win);
                        oldy = y;
                        }
                break;

            case 'D': if(mvwinch(win,y,(oldx+1))=='X')
                        {
                        mvwprintw(win,40,0,"Sorry, you cant move there.");  
                        wrefresh(win);
                        wmove(win,oldy,oldx);
                        wrefresh(win);
                        }

                      else
                        {
                        wmove(win,y,x=(oldx+1));
                        wrefresh(win);
                        oldx = x;
                        }
                break;

            case 'A': if(mvwinch(win,y,(oldx-1))=='X')
                        {
                        mvwprintw(win,40,0,"Sorry, you cant move there.");  
                        wrefresh(win);
                        wmove(win,oldy,oldx);
                        wrefresh(win);
                        }

                      else
                        {
                        wmove(win,y,x=(oldx-1));
                        wrefresh(win);
                        oldx = x;
                        }
                break; 

            case 'W': if(mvwinch(win,(oldy-1),x)=='X')
                        {
                        mvwprintw(win,40,0,"Sorry, you cant move there.");  
                        wrefresh(win);
                        wmove(win,oldy,oldx);
                        wrefresh(win);
                        }

                      else
                        {
                        wmove(win,y=(oldy-1),x);
                        wrefresh(win);
                        oldy = y;
                        }   
                break;

            default: m = 'q';

        }

    }

    delwin(win);
    /*delwin(win2);*/
    endwin();

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您打印的所有内容都在win,但getch()stdscr(默认的全屏窗口)读取。从stdscr读取会导致它跳到窗口堆栈的顶部,遮挡win

使用wgetch(win)