Ncurses - 多个窗口和刷新

mik*_*098 6 c windows ncurses

我正在写一个小学校项目。这是一个单词下降的游戏 - 单词从顶部移动到底部。我有一个想法,制作两个窗口(一个带有界面,第二个带有移动对象)。正如您在代码中看到的那样,单词是随机的。问题是输入。我正在使用 mvwsacanw 来写这个词。当单词在不同的窗口中移动时,有没有办法在第二个窗口中写任何东西?现在这个词正在下降,当它到达底部时,第二个窗口打开,我可以输入这个词。

希望有人会帮助我。

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

void moving(WINDOW *move)
{
    int j,random;
    char *cmp=(char*)malloc(10*sizeof(char));
    char word[6];

    wclear(move);
    box(move, 0, 0);
    mvwprintw(move, 1, 1, "PIS");
    wrefresh(move);

    srand (time (NULL));
    random=2+rand()%7;
    for(j=0; j< random ; j++) //random word
    {
        word[j]= rand()%26+'a';
    }

    int poz = 2+rand()%24; //random position of moving word


    for(int i=1; i<18; i++)
    {
        wclear(move);
        box(move,0,0);
        mvwprintw(move,i, poz, word);
        wrefresh(move);
        usleep(300000);
    }
}

void interface(WINDOW *ui)
{
    wclear(ui);
    char *cmp=(char*)malloc(10*sizeof(char));
    box(ui, 0, 0);
    mvwprintw(ui,1,1,"wpisz wyraz: ");
    mvwscanw(ui,2,1, "%s",cmp);

    mvwprintw(ui, 3, 1, "->%s",cmp);
    wrefresh(ui);
}


int main(int argc, char *argv[])//int argc, const char * argv[])
{
    int x,y;
    int sc = 3;
    initscr();
    noecho();
    curs_set(FALSE);

    getmaxyx(stdscr, y,x);

    WINDOW *move = newwin(y-5, x-1, 0, 0);
    WINDOW *ui = newwin(sc+2, x, y-5, 0);

    while(1)
    {
    moving(move);
    interface(ui);
    wclear(move);
    wclear(ui);
    }

    delwin(move);
    delwin(ui);
    endwin();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Unr*_*gle 1

您无法使用当前的代码结构来做到这一点。您将单词“下降阶段”和“输入阶段”保留在单独的函数中,因此使它们同时工作的唯一方法是某种多线程。假设这不是您想要做的,您可以尝试将这两个功能合并到一个函数中。在伪代码中:

pick random word
pick random position
set i = 0
set input = {} //empty array
do
> print word at (i, pos)
> set stoptime = time() + DELAY
> do
>> set c = getch()
>> append c to input
>> print interface
> while (time() < stoptime)
> i++
while (i < 18)
Run Code Online (Sandbox Code Playgroud)

将 timeout() 设置为适当的延迟,会给人一种一切都同时发生的印象。这绝对不是最有效的解决方案,但是简单明了,考虑到您正在做一个学校项目,它应该没问题