Ein einfacher Fließtext



  • Guten Tag,

    weiß jemand von Euch, wie ich es hinbekommen kann, dass der Text, der nun in der Eingabeaufforderung steht, nach links aus dem Bild herausläuft und dann von rechts wieder in das Bild hineinläuft? Bisher konnte ich das Ganze nicht lösen. Für Antworten wäre ich sehr dankbar.

    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    void gotoxy (const int x, const int y)
    {
    	COORD xy;
    	xy.X=x;
    	xy.Y=y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
    }
    
    void delay( int iDelaytime )
    {
    	clock_t last, current;
    	int bOk;
    	bOk = 1;
    	last = clock();
    
    	while (bOk)
    	{
    		current = clock();
    		if ((current - last) > iDelaytime)
    		{
    			bOk = 0;
    		}
    	}
    }
    
    int main()
    {
    
    char cArray[100];
    char cTemp[50];
    int i;
    int x=10;
    int y=10;
    int speed=1;
    
    strcpy(cArray,"Olli weiß nicht weiter");
    gotoxy(x,y);
    
    	if (cTemp>0)
    	{
    		x=x+1;
    		gotoxy(x,y);
    		printf(cArray);	
    	}
    
    getchar ();
    return 0;
    
    }
    


  • Ich konnte meine Lösung bisher ein wenig verändern. Nun läuft der Fließtext nach rechts aber sobald das erste Zeichen an der letzten Stelle angekommen ist. läuft der Text zurück. Ich möchte aber, dass er weiterläuft.

    /* Der Fließtext */
    
    #include <windows.h>
    #include <stdio.h>
    
    #include <time.h>
    
    void gotoxy (const int x, const int y)
    {
    	COORD xy;
    	xy.X=x;
    	xy.Y=y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
    }
    
    void delay( int iDelaytime )
    {
    	clock_t last, current;
    	int bOk;
    	bOk = 1;
    	last = clock();
    
    	while (bOk)
    	{
    		current = clock();
    		if ((current - last) > iDelaytime)
    		{
    			bOk = 0;
    		}
    	}
    }
    
    int main()
    {
    	int posx=39;
    	int posy=10;
    	int speedx=1;
    
    	gotoxy(posx,posy);
    	printf("Olli ist verrueckt");
    	while (1)
    	{
    		if (posx >= 79)
    		{
    			speedx = -1;
    		}
    		if (posx <= 0)
    		{
    			speedx = 1;
    		}
    
    		gotoxy(posx,posy);
    		printf("                                                  ");
    		posx = posx + speedx;
    		gotoxy(posx,posy);
    		printf("Olli ist verrueckt");
    		delay(50);
    
    	}
    	break;
    	getchar();
    	return 0;
    }
    

    Für hilfreiche Vorschläge wäre ich sehr dankbar!



  • Ich bin nochmals einen Schritt weiter gekommen. Nun läuft der Fließtext nach links (sollte er in der eigentlichen Aufgabenstellung auch) aber anstelle weiter zu laufen, scheint die Zeile nun blockiert zu sein und er springt immer wieder eine Zeile weiter. wenn ich den Zeilenumbruch in Zeile 43 weglasse, wird's noch verrückter

    [cpp]
    /* Der Fließtext */
    
    #include <windows.h>
    #include <stdio.h>
    
    #include <time.h>
    
    void gotoxy (const int x, const int y)
    {
    	COORD xy;
    	xy.X=x;
    	xy.Y=y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
    }
    
    void delay( int iDelaytime )
    {
    	clock_t last, current;
    	int bOk;
    	bOk = 1;
    	last = clock();
    
    	while (bOk)
    	{
    		current = clock();
    		if ((current - last) > iDelaytime)
    		{
    			bOk = 0;
    		}
    	}
    }
    
    int main() {
    
    	//Meine Variablen
    	char cArray[50];
    	int posx=39;
    	int posy=10;
    	int speedx=1;
    
    	gotoxy(posx,posy);
    	strcpy(cArray,"Wer kann mir helfen?\n");
    	while (1)
    	{
    		if (posx >= 79)
    		{
    			speedx = -1;
    		}
    		if (posx <= 0)
    		{
    			speedx = 1;
    		}
    
    		gotoxy(posx,posy);
    		printf("                                                  ");
    		posx = posx - speedx;
    		gotoxy(posx,posy);
    		printf(cArray);
    		delay(50);	
    	}
    	return 0;
    }
    [/cpp]
    

    Kann man hier irgendwo sinnvoll ein "break;" einbauen? Ich hab's versucht aber es hat nie gepasst.



  • Hi !
    Guckst du:

    #include <windows.h>
    #include <stdio.h>
    
    void hide_cursor(void)
    {
    	CONSOLE_CURSOR_INFO cci = {0};
    	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    	cci.bVisible = FALSE;
    	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    }
    
    void gotoxy ( int x, int y )
    {
        COORD xy; xy.X=x; xy.Y=y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
    }
    
    int main ( void )
    {
    	char* s = "Big Brother for president!";
    	int len = strlen(s);
        int xmax = 80, x=20, y=12, i;
        hide_cursor();
        while ( 1 ) {
    		gotoxy ( 0, y );
    		for ( i = 0; i < x; i++ ) 
    			putchar ( ' ' );
    		if ( x == xmax )
    			x = 0;
    		gotoxy ( x, y );
    		for ( i = 0; i < len && i + x < 80; i++ ) 
    			putchar ( s [ i ] );
    		x++;
    		Sleep(100);
        }
        return 0;
    }
    


  • #include <stdio.h>
    
    #include <conio.h>    // platform-specific: kbhit()
    #include <windows.h>  // platform-specific: GetStdHandle(), GetConsoleScreenBufferInfo(),
                          // SetConsoleCursorPosition(), CreateThread(), TerminateThread()
    
    void gotoxy( short x, short y )
    {
        HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
        COORD coord = { x, y };
        SetConsoleCursorPosition( hstdout, coord );
    }
    
    short get_console_width( void )
    {
        HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo( hstdout, &csbi );
    	return csbi.dwSize.X;
    }
    
    typedef struct bouncing_marquee_data_tag {
    	char const *text;
    	short y;
    	short unsigned delay;
    } bouncing_marquee_data_t;
    
    long unsigned __stdcall bouncing_marquee( void *param )
    {
    	bouncing_marquee_data_t *data = param;
    
    	char format[ 10 ];
    	int width = get_console_width();
    	int length = strlen( data->text );
    	int x = -length;
    	int direction = 1;
    
    	for( ;; ) {
    
    		if( x < 1 ) {
    
    			gotoxy( 0, data->y );
    			printf( "%s ", ( data->text + -x ) );
    
    		} else if( x < width - length - 1 ) {
    
    			gotoxy( x - 1, data->y );
    			printf( " %s ", data->text );
    
    		} else {
    
    			gotoxy( x - 1, data->y );
    			sprintf( format, " %%.%us", width - x - 1 );
    			printf( format, data->text );
    		}
    
    		Sleep( data->delay );
    
    		if( direction ) {
    
    			if( ++x < width )
    				continue;
    
    			x = width - 1;
    			direction = 0;
    
    		} else { 
    
    			if( --x > -length )
    				continue;
    
    			x = -length;
    			direction = 1;
    		}
    	}
    
    	return 0;
    }
    
    int main( void )
    {
    	bouncing_marquee_data_t const param = { "Olli weiss nicht weiter", 1, 125 };
    	int unsigned i = 0;
    
    	HANDLE hthread = CreateThread( NULL, 0, bouncing_marquee, (void*) &param, 0, NULL );
    
    	if( !hthread ) {
    
    		fprintf( stderr, "Couldn't create thread.\n\n" );
    		return EXIT_FAILURE;
    	}
    
    	puts( "\n\n" );
    
    	for( ; !kbhit(); ++i ) {
    
    		gotoxy( 1, 3 );
    		printf( "%5i", i );
    		Sleep( 1000 );
    	}
    
    	TerminateThread( hthread, 0 );
    	putchar( '\n' );
    }
    

    // edit: goddamit tab-size!



  • #include <windows.h>
    #include <stdio.h>
    
    void hide_cursor(void)
    {
    	CONSOLE_CURSOR_INFO cci = {0};
    	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    	cci.bVisible = FALSE;
    	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    }
    
    void gotoxy ( int x, int y )
    {
        COORD xy; xy.X=x; xy.Y=y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
    }
    
    int main ( void )
    {
    	char* s = "Big Brother for president!";
    	int len = strlen(s);
        int xmax = 80, x=20, y=12, i, j;
        hide_cursor();
        while ( 1 ) {
    		gotoxy ( 0, y );
    		for ( i = 0; i < x; i++ ) 
    			putchar ( ' ' );
    		if ( x == xmax )
    			x = 0;
    		gotoxy ( x, y );
    		for ( i = 0; i < len && i + x < 80; i++ ) 
    			putchar ( s [ i ] );
    		/////////////////// Added code for more impressive animation :D ///////////////////////////
    		if ( i != len )
    		{
    			gotoxy ( 0, y );
    			for ( j = i; j < len; j++ )
    				putchar ( s [ j ] );
    		}
    		/////////////////////////////////////////////////////////////
    		x++;
    		Sleep(100);
        }
        return 0;
    }
    


  • Vielen herzlichen Dank! 😃

    Die Codes sind echt großartig. Wobei Swordfish's Code wohl noch nicht ganz für einen Anfänger wie mich geeignet ist. Aber sehr nett, dass du dir so eine Arbeit für mich machst. Ihr habt auf jeden Fall Einen gut bei mir. Falls ich Euch mal begegne spendier ich ein Bier.



  • #include <Windows.h> /* wegen Sleep */
    
    int main()
    {
      char *s="Olli weiss nicht weiter                                                        ";
      int i,j;
      for(i=0;i<79;++i)
      {
        printf("%s%.*s",s+i,i,s);
        Sleep(200);
        j=79; while(j--) putchar('\b');
      }
      return 0;
    }
    


  • Auch sehr nett. Danke. Aber bei C++ bin ich noch nicht angelangt. Hilft mir aber sicherlich für die Zukunft.


  • Mod

    ollihagel schrieb:

    Auch sehr nett. Danke. Aber bei C++ bin ich noch nicht angelangt. Hilft mir aber sicherlich für die Zukunft.

    Das ist schon C. Das "C++" kommt davon, dass wir kürzlich (d.h. vor ein paar Stunden 🙂 ) die Code-Tags geändert haben. Nun sind die alten, allgemeinen Codetags zu C++-Tags geworden. Wutz' Beitrag ist noch von vor der Änderung.

    Siehe:
    http://www.c-plusplus.net/forum/310187


Anmelden zum Antworten