Ncurses - multiple windows and update

I am writing a small school project. This is a game of falling words - the word moves from the top to the bottom. I had the idea to make two windows (one with an interface and the second with a moving object). Words are randomized, as you can see in the code. The problem is typing. I use mvwsacanw to spell a word. Is there a way to write something in the second window while the word moves in another window? While the word falls, and when it reaches the bottom, a second window opens, and I can enter the word.

Hope someone helps me.

#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;
}
+4
source share
2 answers

. , - . , , , . :

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)

, -(), , , . , , , , .

+1

:

nodelay(your_window, TRUE);

!

+1

Source: https://habr.com/ru/post/1568653/


All Articles