THE SNAKE GAME

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

class snake
{   int i,j,k,k1,f,flag,num,num1,num2,eat,e;

    int x1[600],y1[600];
    char c;
   public:
     void zero()
      {    f=1;       eat=5;
      for(k=0;k<600;k++)
      {   x1[k]=0;
          y1[k]=0;
      }
      for(k=0;k<eat;k++)
      {
        x1[k]=20;  y1[k]=20+k;
      }


     }
     void drawbk();
     void object(int x,int y,int n);
     void move();
     void snd();
     void load();
};
void snake::load()
{  setcolor(GREEN);
   outtextxy(250,450,"powered by RKGAMES");
   for(k=0;k<70;k++)
   {

   setcolor(BLUE);
   outtextxy(250,250,"LOADING..");
   line(250+k,260,250+k,270);
   delay(50);
   }

   cleardevice();
   floodfill(50,50,WHITE);
}
void snake::snd()
{   nosound();
    for(int x=4000;x>1;x--)
    {sound(x);
     delay(1);
    }
   nosound();
}



void snake::object(int x,int y,int n)
{   for(e=10;e>0;e--)
  {
    setcolor(WHITE);
    circle(x1[n-1],y1[n-1],e);
    floodfill(x1[n-1],y1[n-1],WHITE);
  }
  for(k=n-1;k>0;k--)
  { x1[k]=x1[k-1];
    y1[k]=y1[k-1];
  }
  x1[0]=x;  y1[0]=y;
  for(k=0;k<n;k++)
  {

      for(e=10;e>0;e--)
      {
      setcolor(RED);
      circle(x1[k],y1[k],e);

      }
  }
}
void snake::drawbk()
{   for(e=10;e>0;e--)
   {
    rectangle(10-e,10-e,610+e,410+e);
   }
    rectangle(10,420,610,500);
    setcolor(BLUE);


}

void snake::move()
{  load();
   i=20; j=20;  flag=5;
   c='s';
      randomize();
       num1=(rand()%30+1)*2*10;
       num2=(rand()%20+1)*2*10;
       setcolor(GREEN);
       for(k1=10;k1>0;k1--)
       circle(num1,num2,k1);
       floodfill(num1,num2,GREEN);
       outtextxy(50,430,"a=left d=right w=up s=down");
       outtextxy(50,450,"e=exit ");
      while(1&&f==1)
      {    if(num1==i&&num2==j)
       {       num1=(rand()%30+1)*2*10;
           num2=(rand()%20+1)*2*10;

           for(e=10;e>0;e--)
           {
           setcolor(GREEN);
           circle(num1,num2,e);
           }
           eat++;

       }

      if(i==0||i==620||j==0||j==420)
      { snd();
        setcolor(RED);

        outtextxy(300,300,"GAMEOVER");

        delay(500);
        break;
      }
      for(k=2;k<eat;k++)
      {
         if(x1[0]==x1[k]&&y1[0]==y1[k])
         { setcolor(RED);
           snd();     f=0;
           outtextxy(300,300,"GAMEOVER");
           delay(500);
           break;
         }
      }

      drawbk();
      if(kbhit())
      {
      c=getch();

      if(c=='a'&&flag==2)
     {   c='d';

    }
     if(c=='s'&&flag==3)
     {    c='w';
    }
     if(c=='d'&&flag==0)
     {         c='a';
    }
     if(c=='w'&&flag==1)
     {      c='s';
    }

      }
     if(c=='a'&&flag!=2)
     {   flag=0;
         delay(150);
         object(i-=20,j,eat);

    }
     if(c=='s'&&flag!=3)
     {  flag=1;
        delay(150);
        object(i,j+=20,eat);
    }
     if(c=='d'&&flag!=0)
     {    flag=2;
          delay(150);
          object(i+=20,j,eat);
    }
     if(c=='w'&&flag!=1)
     {   flag=3;
        delay(150);
        object(i,j-=20,eat);
    }
     if(c=='e')
     { break; }




     }
}
void main()
{    clrscr();
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
    floodfill(10,10,WHITE);
    snake s;
    s.zero();
    s.move();
    closegraph();


}




Comments

  1. Thank you so much sir ! It helped me a lot ... I have also got the same semester project ... Thank you best one

    ReplyDelete

Post a Comment

Popular posts from this blog