Tuesday 1 September 2015

Snake Game Program in C

Sample screenshots of Game : 





Program:


#define RIGHT 1
#define UP 11
#define LEFT 111
#define DOWN 1111
#define MAXSIZE 99
#define INITIALSPEED 50
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>

int snakex[MAXSIZE],snakey[MAXSIZE];
int specialFoodX,specialFoodY,specialScore=0;
int spflag,spFood;   // it is set one only once when fifth food is generated( to get co ordinates of special food)
int speedTimer=INITIALSPEED;
void instructions();
int getDir(char key,int dir);
void motion(int dir,int length);
void check();
int  getFoodX();
int  getFoodY();
void specialFood(int flag, int numFood);
void showFood(int x,int y);
void drawSnake(int length, int color);
void defaultSnake();
void gameOver(int snakeLength);
void specialFoodConsumed();
void main(){
//www.shadowhackit.blogspot.com
   int gd = DETECT, gm;
   int foodConsumed=1;
   int dir=RIGHT;
   int snakeLength;
   char keyPressed;
   int food[2];
   instructions();
   initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
   snakeLength=3;//initially
   defaultSnake();
   setcolor(YELLOW);
   rectangle(40,40,620,340);  // Boudnary of game
   setviewport(41,41,619,339,1);  // Playing area

   start: ;
        if(foodConsumed){
        food[0]=getFoodX();
        food[1]=getFoodY();
        }
 while( !kbhit() && snakex[0]<580 && snakex[0]>0 && snakey[0]>0 && snakey[0]<300){
  foodConsumed=0;

   if((snakex[0]>=specialFoodX-1 && snakex[0]<=specialFoodX+5 && snakey[0]>=specialFoodY-1 && snakey[0]<=specialFoodY+5) || ( snakex[0]+5>=specialFoodX-1 && snakex[0]+5<=specialFoodX+5 && snakey[0]+5>=specialFoodY-1 && snakey[0]+5<=specialFoodY+5 )){
     specialFoodConsumed();

   }

   if((snakex[0]>=food[0]-1 && snakex[0]<=food[0]+5 && snakey[0]>=food[1]-1 && snakey[0]<=food[1]+5) || ( snakex[0]+5>=food[0]-1 && snakex[0]+5<=food[0]+5 && snakey[0]+5>=food[1]-1 && snakey[0]+5<=food[1]+5 ))
  {
   snakeLength++;
   if((snakeLength-3) %5==0){
      spflag=1;
      spFood=1;
       speedTimer=20;
   }

    food[0]=getFoodX();
   food[1]=getFoodY();

   }
  showFood(food[0],food[1]);
  specialFood(spflag,snakeLength-3);
  drawSnake(snakeLength,CYAN);
  dir= getDir(keyPressed,dir);
  motion(dir,snakeLength);
  delay(speedTimer);
  clearviewport();

  }

  if(snakex[0]>=580 || snakex[0]<=0 || snakey[0]<=0 || snakey[0]>=300)
  {
    gameOver(snakeLength);
  }
   keyPressed=getch();
   goto start;

}

void instructions(){
   char start;
   printf("-----------------ShadowHack's Snake Game----------------\n");
   printf("\nInstructions:\n");
   printf("1. Use left,up,right,down arrow keys to move the snake left, top, right, down respectively.\n");
   printf("2. Press 'e' anytime during the game to exit the game.\n");
   printf("3. Prevent snake from hiting the wall.\n");
   printf("4. Collect as many foods as possible to increase the score.\n");
   printf("5. Special food appears during the game which increases the speed of snake.\n");
   printf("6. To bring snake to normal speed consume special food before normal food.\n");
   printf("7. If special food is not consumed before normal food, it disappears.\n");
   printf("8. Special food increases the score thrice as much as the normal food.\n");
   printf("Press 's' to start the game or 'e' to exit....");
   while(1){
      start=getch();
      if(start=='s')
  break;
      else if(start=='e')
  exit(0);
      else{
  printf("\nPress 's' key to start the game..\n");
  continue;
      }
   }


}

int getDir(char key,int dir){

   if(key==75 && dir!=RIGHT)
      dir=LEFT;

   if(key==72 && dir!=DOWN )
      dir=UP;

   if(key==80 && dir!=UP)
      dir=DOWN;

   if(key==77 && dir!=LEFT)
      dir=RIGHT;

   if(key=='e')
      exit(0);

   return dir;

} 

void motion(int dir,int length){
   int i;

   if(dir==RIGHT){
      for(i=length-1;i>0;i--){
  snakex[i]=snakex[i-1];
  snakey[i]=snakey[i-1];
      }
      snakex[0]+=7; 
   }

   if(dir==UP){
       for(i=length-1;i>0;i--){
   snakex[i]=snakex[i-1];
   snakey[i]=snakey[i-1];
       }
       snakey[0]-=7;
   }

   if(dir==LEFT){
      for(i=length-1;i>0;i--){
  snakex[i]=snakex[i-1];
  snakey[i]=snakey[i-1];
      }

      snakex[0]-=7;
    }

    if(dir==DOWN){
 for(i=length-1;i>0;i--){
    snakex[i]=snakex[i-1];
    snakey[i]=snakey[i-1];
 }

  snakey[0]+=7;
    }

}

void drawSnake(int length,int color){
   int i;
   setfillstyle(11,color);
   for(i=0;i<length;i++){
      if(i)
       setfillstyle(SOLID_FILL,color);

      else
       setfillstyle(11,color);

      bar(snakex[i],snakey[i],snakex[i]+5,snakey[i]+5);

   }
}



void check(){
   delay(200);
   clrscr();
   rectangle(40,40,620,340);
   printf("%d %d",snakex,snakey);

}

int  getFoodX(){
   int foodX;
   foodX= rand()%570;
   return foodX;
}

int  getFoodY(){
   int foodY;
   foodY=rand()%290;
   return foodY;
}

void specialFood(int flag,int numFood){

   if(flag){
      specialFoodX=rand()%570;
      specialFoodY=rand()%290;
   }
   if(numFood%5==0&&spFood==1) {
      flag=0;
      setfillstyle(SOLID_FILL,GREEN);
      bar(specialFoodX,specialFoodY,specialFoodX+5,specialFoodY+5);
      spflag=0;
   }

}

void showFood(int x,int y){

   setfillstyle(SOLID_FILL,RED);
   bar(x,y,x+5,y+5);

}

void defaultSnake(){
   snakex[0]=340;
   snakey[0]=240;
   snakex[1]=333;
   snakey[1]=240;
   snakex[2]=326;
   snakey[2]=240;

}

void gameOver(int snakeLength){
   char score[2];
   char ex;
   setcolor(RED);
   outtextxy(260,150,"GAME OVER !!");
   setcolor(GREEN);
   score[0]=48+(snakeLength-3+specialScore)/10;
   score[1]=48+(snakeLength-3+specialScore)%10;
   outtextxy(230,170,"Your Score: ");
   outtextxy(320,170,score);
   setfillstyle(SOLID_FILL,BLACK);
   bar(334,170,390,180);

   setfillstyle(SOLID_FILL,RED);
   drawSnake(snakeLength,RED);
   printf("-----www.shadowhackit.blogspot.com-----");
   printf("Press Esc to exit...    ");
   printf("Press 'r' to restart the game..");
   while(1){
      ex=getch();
      if(ex==27)
  exit(0);
      else if(ex=='r'){

    cleardevice();
   closegraph();
   main();
  }
      else
  continue;
   }
}

void specialFoodConsumed(){

   setfillstyle(SOLID_FILL,BLACK);
   bar(specialFoodX,specialFoodY,specialFoodX+5,specialFoodY+5);
   speedTimer=50;
   spFood=0;
   specialScore+=2;

}

No comments:

Post a Comment