Snake game



#include <iostream.h>
#include <graphics.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
 void gameover(int len)
 {
  int i,hscore;
  len=len-6;

  settextstyle(4,HORIZ_DIR,4);

  ifstream fin("sgame.txt");
  fin>>hscore;
  fin.close();

  if(len>hscore)
  {
   char *name;
   hscore=len;
   ofstream fout("sgame.txt");
   fout<<hscore<<name;
   fout.close();
  }


  for(i=0;i<4;i++)
  {
   cleardevice();
   outtextxy(getmaxx()/2-20,getmaxy()/2-100,"  Your score :");
   gotoxy(53,10);
   cout<<len;
   outtextxy(getmaxx()/2-20,getmaxy()/2-50,"  High score :");
   gotoxy(53,13);
   cout<<hscore;
   delay(200);

   outtextxy(getmaxx()/2,getmaxy()/2+100,"  GAME OVER");
   delay(600);
  }
  closegraph();
  exit(0);
 }


void main()
 {
  int gd=DETECT,gm;
  char ch,prech;
  int snake[100][2],maxx,maxy,eggx,eggy,len=6,i;
  initgraph (&gd,&gm,"c:\\tc\\bgi");

  randomize();
  maxx=getmaxx();
  maxy=getmaxy();
  snake[0][0]=maxx/2;
  snake[0][1]=maxy/2;
  eggx=random(maxx-20)+10;
  eggy=random(maxy-20)+10;
  setfillstyle(SOLID_FILL, getmaxcolor());

  settextjustify(CENTER_TEXT, CENTER_TEXT);
  settextstyle(4,HORIZ_DIR,5);
  setcolor(2);
  setbkcolor(1);
  outtextxy(maxx/2,20,"SNAKE GAME");
  settextstyle(8,HORIZ_DIR,2);
  outtextxy(maxx/2,100,"press 'ESC' to QUIT");
  outtextxy(maxx/2,125,"press 'p' to Pause the game");
  outtextxy(maxx/2,maxy/2,"Controlls :");
  outtextxy(maxx/2,maxy/2+50,"w = up");
  outtextxy(maxx/2,maxy/2+75," s = down");
  outtextxy(maxx/2,maxy/2+100,"a = left");
  outtextxy(maxx/2,maxy/2+125," d = right");
  outtextxy(maxx/2,maxy/2+175,"or use directional keys");

  for(i=1;i<100;i++) //initializes array snake with 0
  {
   snake[i][0]=-10;
   snake[i][1]=-10;
  }
  getch(); //press any key to continue

  setcolor(15);

  while(!(snake[0][0]<=0||snake[0][0]>=maxx||snake[0][1]<=0||snake[0][1]>=maxy))
  {
   delay(50);
   cleardevice();
   circle(snake[0][0],snake[0][1],8);
   floodfill(snake[0][0],snake[0][1],getmaxcolor());

   for(i=0;i<=len;i++)
   {
    circle(snake[i][0],snake[i][1],6);
    floodfill(snake[i][0],snake[i][1],getmaxcolor());
   }
   putpixel(snake[0][0]-1,snake[0][1]-1,15);
   gotoxy(71,1);
   cout<<"score: "<<len-6;


   for(i=1;i<len;i++)
   {
    if(!(snake[0][0]==snake[1][0]&&snake[0][1]==snake[1][1]))
    {             //if snake collides with itself
     if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1])
     {
      gameover(len);
     }
    }
   }


   //if snake eats the egg
   if( (snake[0][0]>=eggx-10 && snake[0][0]<=eggx+10) && (snake[0][1]>=eggy-10 && snake[0][1]<=eggy+10) )
   {
    eggx=random((maxx-20))+10;
    eggy=random((maxy-20))+10;
    len++;
   }

   circle(eggx,eggy,3);
   floodfill(eggx,eggy,getmaxcolor());

   //shifting of snake's body
   for(i=len;i>0;i--)
   {
    snake[i][0]=snake[i-1][0];
    snake[i][1]=snake[i-1][1];
   }


   //if a key is pressed on keyboard
   if(kbhit())
   {
    if(prech!=ch)
    {
     prech=ch;
    }
    ch=getch();

   }


   switch(ch)
   {


    case 'w':   //move up
    case 72:
    if(prech=='s'||prech==80)
    {
     ch='s';
    }
    else
    {
     snake[0][1]=snake[0][1]-9;
    }
    break;


    case 's':   //move down
    case 80:
    if(prech=='w'||prech==72)
    {
     ch='w';
    }
    else
    {
     snake[0][1]=snake[0][1]+9;
    }
    break;


    case 'a':   //move left
    case 75:
    if(prech=='d'||prech==77)
    {
     ch='d';
    }
    else
    {
     snake[0][0]=snake[0][0]-9;
    }
    break;


    case 'd':   //move right
    case 77:
    if(prech=='a'||prech==75)
    {
     ch='a';
    }
    else
    {
     snake[0][0]=snake[0][0]+9;
    }
    break;


    case 'p':  //pause game
    gotoxy(35,20);
    cout<<"Game paused";
    while(!kbhit())
    {
    }
    break;


    case char(27):   //EXIT
    gameover(len);

    default:
    ch=prech;

   }//end of switch
  }//end of while

  gameover(len);
}//end of main

Comments

Popular Posts