Created
May 2, 2011 02:27
-
-
Save rjz/951117 to your computer and use it in GitHub Desktop.
C program to bounce an asterisk across the screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> /* for memset() */ | |
#include <unistd.h> /* for usleep() */ | |
#define MAXLEN 80 | |
int main(void) { | |
int spaces = 0, | |
iterator = 1; | |
char buffer[MAXLEN]; | |
while( 1 ) { | |
/* fill buffer with spaces */ | |
memset( buffer, 0, MAXLEN ); | |
memset( buffer, ' ', spaces ); | |
/* add the bouncing asterisk where it belongs */ | |
buffer[spaces] = '*'; | |
/* move to the next spot */ | |
spaces += iterator; | |
/* check to make sure we're onscreen. If we're headed off, bounce the other way */ | |
if( spaces == MAXLEN ) { | |
iterator = -1; | |
} | |
else if( spaces == 0 ) { | |
iterator = 1; | |
} | |
/* output: clear screen with VT100 escape, then print buffer */ | |
printf("\33[2K\r"); | |
printf("%s", buffer); | |
/* pause for 10000 microseconds */ | |
usleep( 10000 ); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment