C++ Project Source Code

   C++ is a very fast and strong language. Now-a-days many programming languages are in the market which offer much better graphics usability. In schools and colleges programming is started from C and C++. Because after learning it, you can easily pick many other languages.


So here is a Snake Game Source Code which may help you in your project :-
 #include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
int food_x=30,food_y=10,x=1,y=0,gameover=1,score=0,option;
struct snake
  { int snake_x;
    int snake_y;
     snake  *next;
     }*end_part,*p,*r,*n,*mouth;
char ch;
void food();
void update();
void input();
void setup();
int main()
{      r= new snake;
       r->next=NULL;
       r->snake_x=35;
       r->snake_y=12;
       p= new snake;
       p->next=r;
       p->snake_x=34;
       p->snake_y=12;
       end_part=new snake;
       end_part->next=p;
       end_part->snake_x=33;
       end_part->snake_y=12;
 mouth=r;
   k:  cout<<"\n\t\t\tSAIKAPIAN SNAKE GAME";
     cout<<"\n\n\t\tLET SEE HOW MUCH YOU CAN SCORE"   ;
     cout<<"\n\n\n\t\t\t\tpress:I for UP";
     cout<<"\n\n\t\t\t\tpress:J for LEFT";
     cout<<"\n\n\t\t\t\tpress:L for RIGHT";
     cout<<"\n\n\t\t\t\tpress:K for DOWN";
     cout<<"\n\n\t\tI\n\tJ\t\tL\n\t\tK";
     cout<<"\n\npress any key to pause and press any of I J K L to continue";
     cout<<"\n1.START THE GAME ";
     cout<<"\n2.EXIT";
     cin>>option;
     clrscr();
     switch(option)
     {case 1:
     while(gameover)
    {    clrscr();

  setup();  delay(300);
  input();
  update();
     }
  clrscr();
  cout<<"gameover!";
  cout<<"\nSCORE = "<<score ;
  getch();
  delete p,n,r,end_part,mouth;
  break;
  case 2: break;
  default: cout<<"\nWRONG CHOICE!!! Try Again "; goto k;break;}
  return 0;}
  void setup()
   {  for(int i=0;i<160;i++)     //horizontal lines
       {  cout<<"*";
       if(i==79)
  gotoxy(1,24);
       }
      gotoxy(1,2);       //left vertical line
      for(i=0;i<22;i++)
       cout<<"*\n";
      gotoxy(80,2);     //right vertical line
      for(i=0;i<23;i++)
       { cout<<"*\n";
  gotoxy(80,2+i);
       }
 n=end_part;
       while(n!=mouth)
       { gotoxy(n->snake_x,n->snake_y)   ;
  cout<<"O";
  if(mouth->snake_x==n->snake_x&&mouth->snake_y==n->snake_y)
  gameover=0;
  n=n->next;
   gotoxy(mouth->snake_x,mouth->snake_y)  ;
   cout<<"X"; }
      if(mouth->snake_x==food_x&&mouth->snake_y==food_y)
  food();
 gotoxy(food_x,food_y);
 cout<<"o";
     }
   void food()
    {  score++;p=new snake;
       p->next=end_part;
       end_part=p;    k:  randomize();
      food_x=random(76) +4;
      food_y=random(20) +4;
      if(food_x==mouth->snake_x&&food_y==mouth->snake_y)
      goto k  ;
 }
   void input()
    { if(kbhit())
    {
       ch=getche();
    switch(ch)
    { case 'j':
 x= -1,y=0;break;
     case 'l':
       x=1;y=0;break;
    case 'i':
      x=0;y= -1;break;
   case 'k':
      x=0;y=1;break;
 default : while(!kbhit());
  break;
      }
    }    }
    void update()
       { p=end_part;
       r=p->next;
       while(r!=NULL)
 { p->snake_x=r->snake_x;
   p->snake_y=r->snake_y;
   p=p->next;
   r=r->next;
   }
    mouth->snake_x=mouth->snake_x+x;
       mouth->snake_y=mouth->snake_y+y;
       if(mouth->snake_x==80||mouth->snake_x==1||mouth->snake_y==1||mouth->snake_y==24)
    gameover=0;
    }

Happy Coding....

Comments

Post a Comment