adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Snake game in C

Snake game in C

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

 int x = 10, y = 10;
 int dl;
 char ch[100] ={'x','o','o'};
 int  i = 0;
 int f_x, f_y;
 int size = 3;
 int score = 0;
 int pause = 0;
 int ax[100] ={ 10,9,8};
 int ay[100] ={10,10,10};
 char status[10] = "Start";

 void delay(int);
 void gotoxy(int ,int);
 void init();
 void XY();
 void snake_print();
 void food();
 int matchOver();
 void drawBox();
 void print_food();
 void Main();
 void snk();
 
 int main()
   {
    char ans;
    do{
      init();
      drawBox();
      snake_print();
      food();
      snk();
      gotoxy(33,24);
      printf("Again Play :- ");
      scanf("%c",&ans);
    }while(ans == 'y' || ans == 'Y');
     getch();
     return 0;
  }
  
 void init()
 {
    x = 10;
    y = 10;
    size = 3;
    score = 0;
    pause = 0;
    ch[0] = 'x';
    ch[1] = 'o';
    ch[2] = 'o';

    ax[0] = 10;
    ax[1] = 9;
    ax[2] = 8;

    ay[0] = ay[1] = ay[2] = 10;
  }

 void XY()
   {
     int j;
     for(j = size - 1; j > 0; j--)
      {
       ax[j] = ax[j - 1];
       ay[j] = ay[j - 1];
       }
     ax[0] = x;
     ay[0] = y;
   }
   void snake_print()
    {
     int j;
     for(j = 0; j < size; j++)
      {
   gotoxy(ax[j],ay[j]);
   printf("%c",ch[j]);
      }
    }

 void food()
  {
   lvl1:
   f_x = (rand() % 80) ;
   if( f_x <= 1 || f_x >= 86 || f_x == x )
       goto lvl1;
   lvl2:
   f_y = (rand() % 80)  ;
   if( f_y <= 2 || f_y >= 20 || f_y == y)
      goto lvl2;

 }
 int matchOver()
   {
     if( x <= 1 || x >= 85)
 {
   return 0;
 }
     if( y <= 1 || y >= 20)
      {
  return 0;
      }
     return 1;
   }
 void drawBox()
  {
     int i, j;
   gotoxy(27,0);
   printf("<>X<> Sanke Game <>X<> ");
   gotoxy(60,21);
   printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",218,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,191);
   gotoxy(60,22);
   printf("%c SCORE  : %4d \t  %c",179,score,179);
   gotoxy(60,23);
   printf("%c STATUS : %-10s %c",179,status,179);
   gotoxy(60,24);
   printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",192,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,217);

    // Instructions
   gotoxy(5,21);
   printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",218,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,191);
   gotoxy(5,22);
   printf("%c Press 'Esc' to Exit. %c",179,179);
   gotoxy(5,23);
   printf("%c Press 'p' to Pause.  %c",179,179);
   gotoxy(5,24);
   printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",192,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,196,217);
    // Author name
     gotoxy(30,26);
    printf("Developed By : Om prakash");
     //top    bottom
    for(i = 1; i <= 85;i++)
      {
       gotoxy(i,1);
       printf("%c",220);
       gotoxy(i,20);
       printf("%c",220);
     }
     // left    right
    for(i = 2; i <= 20;i++)
    {
      gotoxy(1,i);
      printf("%c",219);
      gotoxy(85,i);
      printf("%c",219);
     }
   }

  void food_print()
   {
    gotoxy(f_x,f_y);
    printf("%c",15);
   }
   
 
 void Main(int f)
  {
   switch(f)
   {
    case 2: pause = 0;
    y+=1;
    dl = 3000;
    break;
   case 8: pause = 0;
    y-=1;
    dl = 3000;
    break;
   case 4: pause = 0;
    x-=1;
    dl = 2000;
   break;
    case 6:pause = 0;
    x+=1;
    dl = 2000;
    break;
    case 81:
      pause = 1;
      gotoxy(50,9);
      strcpy(status,"Pause   ");
      break;
    }
    delay(dl);
    if( pause == 0) {
      system("cls");
      gotoxy(50,9);
      strcpy(status,"Playing");
      //printf("Status : %s",status);
      snake_print();
      XY();
    }
   
    i++;
    food_print();
    drawBox();
    
    if(x == f_x && y == f_y)  {
       food();
       score ++;
       ch[size] = 'o';
       size++;
    }
  }

 void snk()
  {
   int flag = 0 ;
   char move;
   food_print();
   drawBox();
   getch();
   while(1)
    {
     if(kbhit())
      {
      move = getch();
      //condition for move
      if(flag == 8 && move == 80)
 continue;
      if(flag == 2 && move == 72)
 continue;
      if(flag == 4 && move == 77)
 continue;
      if(flag == 6 && move == 75)
 continue;
      switch(move)
      {
       case 77:flag = 6;
  break;
       case 75:
  flag = 4;
  break;
       case 72 : flag = 8;
  break;
       case 80: flag = 2;
        break;
       case 'p': flag = 81;
  break;
       }
       // for Exit from game
       gotoxy(0,27);
       if(move == 27)
      break;

      }
      if( matchOver() == 0)
      {
   gotoxy(35,10);
   printf("Game Over");
   gotoxy(0,28);
      break;
      }
    // Main Logic
       Main(flag);
   }
  } 
  
 void delay(int d)
 {
  int i,j;
  for(i = 0; i < 10000; i++)
    {
     for(j = 0; j < d *2; j++)
      {
   }
    }
 }

 void gotoxy(int x, int y)           //definition of gotoxy function//
    {
     COORD pos ={x,y};
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
   }

Output :-





Post a Comment

0 Comments