Write a C program to design flying characters Screen Saver

By | 03.12.2016

C program to design flying characters Screen Saver


Write a C program to design flying characters Screen Saver. This C program will display flying characters just like a Screen Saver.

Here is a set of characters that will access randomly and position on the screen will also random and on the random position program will display random character from assigned set of characters until user does not press any key.


Below is the source code of the C program to design flying characters Screen Saver which is successfully compiled and run on the windows system.The Output of the program is shown below.


SOURCE : :


#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
 
/*will return character based on passed number*/
char getCHAR(int n)
{
    char ok[]="~!@#$%^&*:;'\",{}|<>=";
    return ok[n];
}
 
/*function to identify that any key hit by the keyboard*/
int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;
  
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  
  ch = getchar();
  
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
  
  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }
  
  return 0;
}
  
/*move cursor on specified position on screen*/
void gotoxy(int x,int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
 
int main(void) {
    int x,y;
    long int i,j;
    i=j=0;
 
    x=1; y=20;
    system("clear");
 
    /*program will run until any key hit*/
    while(!kbhit())
    {
        x=rand()%((150-1+1)+1);
        y=rand()%((50-1+1)+1);
        gotoxy(x,y);
        printf("%c", getCHAR(rand()%((20-1+1)+1))); fflush(stdout);
        ++i;
        if(i==10000) {++j; i=0;}
        if(j==100000){ i=0;}
        if(j<=100)
            usleep(30000);
        if(j<=1000)
            usleep(3000);
        if(j<=10000)
            usleep(300);
    }
    return 0;
}

Also Read : : Write a C program for Macro Processor

 

0 0 votes
Article Rating
Category: Advance Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments