ncurses应用程序中的sleep()

use*_*327 4 c sleep ncurses

我正在尝试为ncurses中的应用程序制作文本动画.

用户按下一个键,选择一个方向,文本网格中的一个对象应该从网格的一个单元格移动到给定方向的下一个单元格,等待它移动前500ms.我用的代码是

while (!checkcollisions(pos_f, input)) { // Checks if it can move to next grid
    pos_f = moveobject(pos_f, input, ".."); // Moves object to next cell
    usleep(50000);
}
Run Code Online (Sandbox Code Playgroud)

但是当我执行它时,它不是移动,等待和再次移动,而是等待很长时间,并且对象突然出现在网格的最后一个单元格中,而不显示动画.

这是因为ncurses如何工作?我已经尝试过使用其他解决方案,比如select()停止功能.

tor*_*rek 7

你需要打电话refresh()(之前usleep).


更新:新的引擎收录,编辑代码段(在几个评论)指出,真正的问题,这是在同一个ncurses的刷新:混合stdscr(通过接下来的两个呼叫暗示),并getchrefreshnewwinwrefresh.
更新2:使用完整的代码,加上一些黑客,我得到了它的工作(对于一些"工作"的价值,我显然没有正确地调用printmap(),我编造了一个虚假的"地图"文件).

在没有仔细观察的情况下,我只更改了所有的getch()to wgetch(win.window),所有mvprintw调用mvwprintw(使用相同的窗口),并删除了至少一个不需要的getch/wgetch.然后问题的核心:

                while (!checkcollisions(pos_f, input)) {
-                       pos_f = moveobject(pos_f, input, "..");
-                       // sleep + wrefresh(win.window) doesn't work, neither does refresh()
+                       struct position new_pos = moveobject(pos_f, input, "..");
+                       printmap(pos_f, new_pos);
+                       pos_f = new_pos;
+                       wrefresh(win.window);
+                       fflush(stdout);
+                       usleep(50000);
                }
Run Code Online (Sandbox Code Playgroud)

上面的调用printmap肯定是错误的,但你仍然需要在循环中做一些事情来改变win.window(或者stdscr你提出的其他窗口或其他什么); 然后你需要强制它刷新,并fflush(stdout)在睡觉前强制输出到stdout .