在C中滚动文本

时间:2011-02-28 09:01:40

标签: c text scroll banner

我想在屏幕上连续滚动文字。例如,

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;
}

任何人都可以帮忙更改代码吗?

3 个答案:

答案 0 :(得分:0)

只需在数组中使用索引,并在正确的屏幕位置输出一个带有putchar()的字符。试试吧 看看这个链接可能会帮助你 http://www.dreamincode.net/code/snippet1964.htm

答案 1 :(得分:0)

你的问题很有趣,我无法阻止,直到我让它“工作”:

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()
#include <stdlib.h> // For malloc()

int main(int argc, char* argv[])
{

    char *text = "Hello, how are you? ";
    char *scroll;
    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();

    getmaxyx(stdscr, max_y, max_x);
    scroll = malloc(2 * max_x + 1);

    for (i=0; i< 2*max_x; i++) {
            scroll[i] = text[i % text_length];
    }

    scroll[2*max_x - 1]='\0';


    // Scroll text back across the screen
    for (i=0; i < 10000; i++) {

            mvaddnstr(0,0,&scroll[i%max_x], max_x);
            refresh();
            usleep(20000);
    }
    // Wait for a keypress before quitting
    getch();

    endwin();

    return 0;
}

请注意,我已经被骗:)(a)我将字符串复制到足够大以至于总是填满屏幕(宽度的两倍)(b)我不滚动打印位置,我滚动(c)我只是在原始输入字符串中放了一个空格,因为它比通过其他机制放入空格更容易。

哦是的,我删除了clear()电话,它让屏幕太乱了,真的看不到。我们一遍又一遍地覆盖相同的max_x单元格,不需要一直清除整个屏幕。

我希望这会有所帮助。

答案 2 :(得分:-4)

 1. 

    /*this one is easy to understan*/


                #include <stdio.h> 
            #include <conio.h> 
            #include <string.h> 
            //library for sleep() function 
            #include <unistd.h> 
            void main() 
            { 
                int i,j,n,k; 

            //text to be srolled 
                  char t[30]="akhil is a bad boy"; 

            n=strlen(t); 

                for(i=0;i<n;i++) 
                 { 
                    printf("\n"); 

                   //loop for printing spaces 

                      for(j=20-i;j>0;j--) 
                         { 
                                printf(" "); 
                         } 

                  /*printing text by adding a                  character every time*/ 

                 for(k=0;k<=i;k++) 
                  { 
                      printf("%c",t[k]); 
                   } 

            // clearing screen after every iteration 

            sleep(1); 
            if(i!=n-1) 
            clrscr(); 
               }   
 }