在C中滚动文本

问题描述 投票:0回答:3

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

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

任何人都可以帮助改变代码并做到这一点吗?

c text scroll banner
3个回答
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)我不滚动打印位置,我滚动我要求的文本print(c)我只是在原始输入字符串中放了一个空格,因为它比通过另一个机制放入空格更容易。

哦是的,我删除了clear()电话,它让屏幕太乱了,真的看不到。我们一遍又一遍地覆盖相同的max_x细胞,无需保持清除整个屏幕。

我希望这有帮助。


0
投票

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


-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(); 
               }   
 }
© www.soinside.com 2019 - 2024. All rights reserved.