C语言中的滚动文本
我想在屏幕上连续滚动文本。例如,
text = "Hello, how are you"
输出应该是:
Hello, how are you Hello, how are you Hello, how are you Hello, how are you
并且从右向左旋转。
到目前为止我已经编译了这个:
#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()
int main(int argc, char *argv[]) {
char *text = argv[1];
char *text = "Hello, how are you";
int text_length;
int i, max_x, max_y;
// Get text length
text_length = strlen(text);
// Initialize screen for ncurses
initscr();
// Don't show cursor
curs_set(0);
// Get terminal dimensions
// getmaxyx(stdscr, max_y, max_x);
// Clear the screen
clear();
// Scroll text back across the screen
while (1) {
getmaxyx(stdscr, max_y, max_x);
if ((max_x - text_length) <= 1)
i = max_x;
else
i = (max_x - text_length);
for (i; i > 0; i--) {
clear();
mvaddstr(0, i, text);
refresh();
usleep(20000);
}
}
// Wait for a keypress before quitting
getch();
endwin();
return 0;
}
Can anybody help to change the code and do that?
I would like to scroll a text continuously in the screen. For example,
text = "Hello, how are you"
The output should be:
Hello, how are you Hello, how are you Hello, how are you Hello, how are you
and rotating from right to left.
So far I have compiled this:
#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()
int main(int argc, char *argv[]) {
char *text = argv[1];
char *text = "Hello, how are you";
int text_length;
int i, max_x, max_y;
// Get text length
text_length = strlen(text);
// Initialize screen for ncurses
initscr();
// Don't show cursor
curs_set(0);
// Get terminal dimensions
// getmaxyx(stdscr, max_y, max_x);
// Clear the screen
clear();
// Scroll text back across the screen
while (1) {
getmaxyx(stdscr, max_y, max_x);
if ((max_x - text_length) <= 1)
i = max_x;
else
i = (max_x - text_length);
for (i; i > 0; i--) {
clear();
mvaddstr(0, i, text);
refresh();
usleep(20000);
}
}
// Wait for a keypress before quitting
getch();
endwin();
return 0;
}
Can anybody help to change the code and do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题很有趣,我无法停止,直到我让它“工作”:
请注意,我已经作弊了:)(a)我将字符串复制到足够大以始终填充屏幕(宽度的两倍) (b) 我不滚动打印位置,而是滚动我要求打印的文本 (c) 我只是在原始输入字符串中添加一个空格,因为它比添加空格更容易通过另一种机制。
哦,是的,我删除了
clear()
调用,它使屏幕太混乱而无法真正看到。不过,我们会一遍又一遍地覆盖相同的max_x
单元格,无需不断清除整个屏幕。我希望这有帮助。
Your problem was intriguing enough I couldn't stop until I'd made it "work":
Note that I've cheated :) (a) I duplicate the string to more than large enough to always fill the screen (twice the width) (b) I don't scroll the print location, I scroll the text that I ask to print (c) I just put a space in the original input string because it was easier than putting in a space through another mechanism.
Oh yeah, I removed the
clear()
call, it made the screen too messy to really see. We're over-writing the samemax_x
cells over and over though, no need to keep clearing the entire screen.I hope this helps.
只需使用数组的索引,然后使用 putchar() 在正确的屏幕位置输出一个字符。尝试一下
看看这个链接也许会对你有帮助
http://www.dreamincode.net/code/snippet1964.htm
Just use an index into the array and output one character with putchar() at the correct screen location.Try it
take a look at this link may be it will help u out
http://www.dreamincode.net/code/snippet1964.htm