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("*");
}



 

No comments:

Post a Comment

Popular Posts