Saturday, December 12, 2015

Simple Animation using Arrow Keys in C Program

Program #25

Description:
Write simple animation using Arrow key (Up, Down, Left and Right) only in C Program.


/* Arrow Programming in C Program
UP Arrow    - 72
DOWN Arrow  - 80
LEFT Arrow  - 75
RIGHT Arrow - 77
*/

#include<conio.h>

/* Global variable declaration, Row and Column */
int row=13, col=40;
void main(){
  /* Local variable declaration */
  char inputKey, arrowKey;

 /* Prototype declaration */
  void moveUp();
  void moveDown();
  void moveLeft();
  void moveRight();
 
/* Clear the Screen */
  clrscr();
 
/* Move Cursor to Center of the Screen by Default */
  gotoxy(col, row);

  /* Detect Arrow Keys in C Program
     Press ESCAPE - 27 to
     Get out from the Indefinite loop
  */

  while ( (inputKey = getch()) != 27){

    /* Find Arrow Key or not */
    if (inputKey == '\0'){

      /* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72:
    //printf("UP");
    moveUp();
    break;

 case 80:
    //printf("DOWN");
    moveDown();
    break;

 case 75:
    //printf("LEFT");
    moveLeft();
    break;

 case 77:
    //printf("RIGHT");
    moveRight();
    break;
      }
    }
  }
}

/* Move Cursor - Upward */
void moveUp(){
 if (row != 1){
   row--;
 } else if (row < 1){
   row = 1;
 }

/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Downward */
void moveDown(){
 if (row != 25){
   row++;
 } else if (row > 25){
   row = 25;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Left Side */
void moveLeft(){
 if (col != 1){
   col--;
 } else if (col < 1){
   col = 1;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}


/* Move Cursor - Right Side */
void moveRight(){
 if (col != 80){
   col++;
 } else if (col > 80){
   col = 80;
 }
 
/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}



 

Get Alphabet Input using C Program

Program #24

Description:
To detect only character input either lower/upper character and print.  Rest of the keypress are ignored.

#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];

int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);
 clrscr();

 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }

 printf("\nPlease Enter Alphabets value [a-zA-Z]:\n");
 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }

    /* Accept only Lower Case charaters, else ignore */
    else if (c >= 97 && c <= 122){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }

    /* Accept only Upper Case charaters, else ignore */
    else if (c >= 65 && c <= 90){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
  printf("          ");
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}



 

To Detect or Recognize Arrow Keys in C Program


Program #23

Description:
To Detect/Recognize Arrow Keys in C Program


/* Program to Detect Arrow Keys in C Program
UP Arrow    - 72
DOWN Arrow  - 80
LEFT Arrow  - 75
RIGHT Arrow - 77
*/

void main(){
  char inputKey, arrowKey;

 /* Clear the Screen */
  clrscr();
 
/* Detect Arrow Keys in C Program
     Press ESCAPE - 27 to
     Get out from the Indefinite loop
  */
  while ( (inputKey = getch()) != 27){

  /* Find Arrow Key or not */
    if (inputKey == '\0'){
     
/* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72: printf("UP"); break;
 case 80: printf("DOWN"); break;
 case 75: printf("LEFT"); break;
 case 77: printf("RIGHT"); break;
      }
    }
  }
}



 

Detect Function Keys in C Program

Program #22

Description:
To get the Function key values using getch()


void main(){
  char inputKey;
 
/* Clear the Screen */
  clrscr();

  /* Detect Function Keys in C Program */
  while ( (inputKey = getch()) != '\r'){
    printf("%c:%d\n", inputKey, inputKey);
  }
}

 

Simple Animation - Drawing Border using C Program


Program #21

Description:
To draw Border in the Screen using dots.  All dots are in multi-colors. Once the border drawing completed.  System will prints success message.


#include<dos.h>
#include<conio.h>
#include<stdlib.h>

/* global prototype declaration */
void setTextColor();

int main(){
  int leftPos=1, rightPos=25;
  int botPos=1, topPos=80;

 /* prototype declaration */
  void drawLeftBorder(int);
  void drawRightBorder(int);
  void drawTopBorder(int);
  void drawBottomBorder(int);
  void initialMessage();
  void finalMessage();
 
/* Clear Screen */
  clrscr();

  /* Initial Message */
  initialMessage();

  for(;!kbhit();){

     /* take a random color */
     setTextColor();

     if (botPos == 1 && topPos == 80 && leftPos <= 25 && rightPos >= 1){
       drawLeftBorder(leftPos++);
       drawRightBorder(rightPos--);
     }
     if (leftPos == 25 && rightPos == 1 && topPos >= 1 && botPos <= 80){
       drawBottomBorder(botPos++);
       drawTopBorder(topPos--);
     }

     if(leftPos == 25 && rightPos == 1 && botPos == 80 && topPos == 1){
       finalMessage();
       getch();

       /* End the Program Execution */
       return 0;
     }
  }
  getch();
  return 0;
}

/*  Draw Left Border */
void drawLeftBorder(int leftPos){
 gotoxy(1,leftPos);
 cprintf("*",leftPos);
 delay(100);
}

/*  Draw Right Border */
void drawRightBorder(int rightPos){
 gotoxy(80,rightPos);
 cprintf("*",rightPos);
 delay(100);
}

/*  Draw Top Border */
void drawTopBorder(int topPos){
 gotoxy(topPos,1);
 cprintf("*",topPos);
 delay(100);
}

/*  Draw Bottom Border */
void drawBottomBorder(int bottomPos){
 gotoxy(bottomPos, 25);
 cprintf("*",bottomPos);
 delay(100);
}

void finalMessage(){
  int i;
  gotoxy(30,12);
  setTextColor();
  cprintf("Border Drawing Completed!");
  gotoxy(30,13);
  for(i=1;i<=25;i++){
    printf("_");
    delay(100);
  }
}

void initialMessage(){
  int i;
  gotoxy(30,13);
  setTextColor();
  cprintf("Press any Key to EXIT!");
  gotoxy(30,14);
  for(i=1;i<=22;i++){
    printf("-");
    delay(100);
  }
}

void setTextColor(){
  int a;
  a = random(15);
  if (a == 0) {
    /* Restrict BLACK color */
    setTextColor();
  } else {
    textcolor(a);
  }
}



 

Friday, December 11, 2015

Get number input using C Program


Program #20

Description:
Get number [0-9] only as input from the user.  Even if user press characters, system will ignore and wait for valid input.  Once input reaches MAXLENGTH (user defined length) program will stop the execution.


#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];
int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);

 clrscr();
 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }
 printf("\nPlease Enter Integer value [0-9]:\n");

 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }
   
/* Accept only Numbers, else ignore */
    else if (c >= 48 && c <= 57){
      userInput[charCounter++] = c;
     
/* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
 
  /* Clear the Output area */
  printf("          ");
 
  /* Start to print on perfect position */
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}


 

 

Identify Key Press using C program


Program #19


Description:
Identify Key Press using C program

int main(){
 char c=' ';
 clrscr();
 printf("\nPlease press a SPACE Bar to EXIT:\n");
 for(;!kbhit();){
    c = getch();
    if (c == ' '){
      return 0;
    } else {
      printf("%c:%d\t",c,c);
    }
 }
 getch();
 return 0;
}


 

Popular Posts