我正在尝试为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()停止功能.
你需要打电话refresh()(之前usleep).
stdscr(通过接下来的两个呼叫暗示),并getch和refresh同newwin和wrefresh.在没有仔细观察的情况下,我只更改了所有的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 .
| 归档时间: |
|
| 查看次数: |
8017 次 |
| 最近记录: |