Wednesday, June 12, 2013

Snake Game (Revised)

















/*
Snake Game (Revised) - 12 June 2013
*/

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


void main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   void *body,*food,*tail1,*tail2,*tail3,*tail4,*head1,*head2,*head3,*head4;
   int x, y,X[5000],Y[5000],i=3,maxx,maxy,speed=100,bo=10,t[10],score=0,hscore=20;
   unsigned int size;
   char a='d',b,scor[4],hs[4];
   int k=2,l,r1,r2,f=0,z=100,first=0,second=1;;
   r1=300;
   r2=350;

   ifstream infile("rattle.txt");
   infile.getline(hs,4);
   infile.close();
   hscore = atoi(hs);

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk) {  /* an error occurred */
      printf("Graphics error: %s", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
   //playagain:
   maxx = getmaxx(); //right
   maxy = getmaxy(); //bottom

   //snake location at the begining
   y = 160;
   x = 80;
   for(int j=0; j<=3; j++) { //printing whole body at first
X[i-j]=x-bo*(j+1);
Y[i-j]=y;
   }

   //body
   setfillstyle(1,2);
   bar(10,10,20,20);
   setcolor(0);
   setlinestyle(0,1,3);
   line(9,9,21,21);
   line(9,21,21,9);
   size = imagesize(10,10,20,20);
   body = malloc(size);
   getimage(10,10,20,20,body);
   cleardevice();

   setcolor(0);
   //tail1-right
   t[0]=20; t[1]=10;
   t[2]=10; t[3]=10;
   t[4]=20; t[5]=15;
   t[6]=10; t[7]=20;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail1 = malloc(size);
   getimage(10,10,20,21,tail1);
   cleardevice();

   //tail2-left
   t[0]=10; t[1]=10;
   t[2]=20; t[3]=10;
   t[4]=10; t[5]=15;
   t[6]=20; t[7]=20;
   t[8]=10; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail2 = malloc(size);
   getimage(10,10,20,21,tail2);
   cleardevice();

   //tail3-up
   t[0]=10; t[1]=20;
   t[2]=10; t[3]=10;
   t[4]=15; t[5]=20;
   t[6]=20; t[7]=10;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail3 = malloc(size);
   getimage(10,10,20,21,tail3);
   cleardevice();

   //tail4-down
   t[0]=10; t[1]=10;
   t[2]=10; t[3]=21;
   t[4]=15; t[5]=10;
   t[6]=20; t[7]=21;
   t[8]=20; t[9]=10;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail4 = malloc(size);
   getimage(10,10,20,21,tail4);
   cleardevice();

   //head1-right
   setlinestyle(1,1,1);
   setcolor(2);
   fillellipse(10,10,10,5);
   setcolor(4);
   //eyes
   fillellipse(15,7,1,1);
   fillellipse(15,13,1,1);
   size = imagesize(10,5,20,15);
   head1 = malloc(size);
   getimage(10,5,20,15,head1);

   //head2-left
   //eyes
   fillellipse(5,7,1,1);
   fillellipse(5,13,1,1);
   size = imagesize(0,5,10,15);
   head2 = malloc(size);
   getimage(0,5,10,15,head2);
   cleardevice();

   //head3-up
   //eyes
   setcolor(2);
   fillellipse(12,12,5,10);
   setcolor(4);
   fillellipse(9,6,1,1);
   fillellipse(15,6,1,1);
   size = imagesize(7,2,18,12);
   head3 = malloc(size);
   getimage(7,2,18,12,head3);

   //head4-down
   //eyes
   fillellipse(9,17,1,1);
   fillellipse(15,17,1,1);
   size = imagesize(7,12,18,22);
   head4 = malloc(size);
   getimage(7,12,18,22,head4);
   cleardevice();

   //food
   setcolor(15);
   setfillstyle(1,15);
   fillellipse(10,10,3,5);
   size = imagesize(5,3,15,17);
   food = malloc(size);
   getimage(5,3,15,17,food);
   cleardevice();

    /* repeat until a key is pressed */
   maxx=getmaxx();
   maxy=getmaxy();
   setlinestyle(0,1,3);
   setcolor(2); // green
   rectangle(0,0,maxx,maxy); //outermost rectangle
   setlinestyle(0,1,2);
   line(maxx-150,0,maxx-150,maxy); //seperating line

   setcolor(8);
   setfillstyle(1,2); // green
   bar(maxx-145,5,maxx-5,60);  //bg of RATTLE SNAKE text
   bar(maxx-145,65,maxx-5,205); // bg of Instruction
   bar(maxx-145,210,maxx-5,320); //bg of Information

   int maze=1;
   if(maze>=1) { //the rectangular bar
   setfillstyle(1,2); // solid, green
   bar(4,4,maxx-154,14); //top
   bar(4,4,14,maxy-4); //left
   bar(4,maxy-4,maxx-154,maxy-14); //bottom
   bar(maxx-154-10,4,maxx-154,maxy-4); //right
   }

   setcolor(15); // white
   settextstyle(0,0,1); // FONT, DIR, charsize
   delay(300); //Suspends execution for interval (milliseconds)
   outtextxy(maxx-125,30,"RATTLE SNAKE");
   delay(300);
   setcolor(15);
   outtextxy(maxx-140,70,"INSTRUCTIONS");
   delay(200);
   outtextxy(maxx-140,90,"Commands     Key");
   delay(300);
   outtextxy(maxx-140,110,"Move Up      w");
   delay(50);
   outtextxy(maxx-140,130,"Move Down    s");
    delay(50);
   outtextxy(maxx-140,150,"Move Right   d");
    delay(50);
   outtextxy(maxx-140,170,"Move Left    a");
    delay(50);
   outtextxy(maxx-140,190,"Exit         0");
   delay(200);
   outtextxy(maxx-140,220,"INFORMATION");

   setcolor(15);
   delay(200);
   itoa(score,scor,10);
   outtextxy(maxx-140,240,"Score");

   setcolor(15);
   outtextxy(maxx-50,240,scor);
   delay(200);
   outtextxy(maxx-140,260,"Level       1");
   delay(200);
   outtextxy(maxx-140,300,"High Score  ");
   outtextxy(maxx-50,300,hs);

   for(j=0; j<=1000; j++) {
putpixel(random(maxx-150),random(maxy),6);
delay(1);
   }

   while (a!='0') {
       // plot new image
      if(a=='d')
   putimage(x, y, head1, XOR_PUT);
      if(a=='a')
   putimage(x, y, head2, XOR_PUT);
      if(a=='w')
   putimage(x, y, head3, XOR_PUT);
      if(a=='s')
   putimage(x, y, head4, XOR_PUT);

      for(j=0; j<k; j++) {
  putimage(X[i-j], Y[i-j], body, XOR_PUT); //body
      }

      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail1, XOR_PUT);

      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail2, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
   putimage(X[i-j], Y[i-j], tail3, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
   putimage(X[i-j], Y[i-j], tail4, XOR_PUT);

      delay(speed);

      if(second==1) {
setcolor(15);
outtextxy(maxx-140,360,"Press Any Key...");
getch();
setcolor(0); //changing the color of text to background color
outtextxy(maxx-140,360,"Press Any Key...");
a='d';

second=2;
      }


      if(first==1) {
setcolor(15);
outtextxy(maxx-140,360,"Congratulations  ");
sound(100);
delay(300);
nosound();
getch();
setcolor(0);
outtextxy(maxx-140,360,"Congratulations  ");
second++;
first=2;
      }

// erase old image
      if(a=='d')
   putimage(x, y, head1, XOR_PUT);
      if(a=='a')
   putimage(x, y, head2, XOR_PUT);
      if(a=='w')
   putimage(x, y, head3, XOR_PUT);
      if(a=='s')
   putimage(x, y, head4, XOR_PUT);

      for(j=0; j<k; j++) {
  putimage(X[i-j], Y[i-j], body, XOR_PUT);
      }

      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail1, XOR_PUT);

      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
   putimage(X[i-j], Y[i-j], tail2, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
   putimage(X[i-j], Y[i-j], tail3, XOR_PUT);

      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
   putimage(X[i-j], Y[i-j], tail4, XOR_PUT);

      if(f==0) {
putimage(r1,r2,food,XOR_PUT);
f=1;
      }

      z--;

      //collision of head and food
      if((x>=r1 && y>=r2 && x<=r1+10 && y<=r2+10) || (x<=r1 && y<=r2 && x>=r1-10 && y>=r2-10) || z==0)
      {
if(z!=0) {
sound(800);
delay(20);
setcolor(2); // score up
score += 10;
outtextxy(maxx-50,240,scor);
itoa(score,scor,10);

//changing the maze
if(score>=100) { //100
maze=2;
//level=2;
setfillstyle(1,2); // solid, green
bar(maxx-140,260,maxx-40,280); //hiding Level 1
//bar(maxx-140,280,maxx-40,295); //hiding Maze 1
setcolor(15); //white
outtextxy(maxx-140,260,"Level       2");
//outtextxy(maxx-140,280,"Maze        2");
}

if(score>=200) { //200
maze=3;
//level=3;
setfillstyle(1,2); // solid, green
bar(maxx-140,260,maxx-40,280); //hiding Level 2
setcolor(15); //white
outtextxy(maxx-140,260,"Level       3");
}

if(score>=350) { //350
maze=4;
//level=4;
setfillstyle(1,2); // solid, green
bar(maxx-140,260,maxx-40,280); //hiding Level 3
setcolor(15); //white
outtextxy(maxx-140,260,"Level       4");
}

   //this maze will be loaded after the user gets score of 100
   if(maze==2) {
   //adding maze
   bar(104,104,maxx-254,114); //100px below than top
   bar(104,maxy-104,maxx-254,maxy-114); //100px above than bottom
   }
   if(maze==3) {
   bar(104,154,114,maxy-164); //left bar
   bar(maxx-264,154,maxx-254,maxy-164); // right bar
   }
   if(maze==4) {
   bar(204,204,maxx-354,214); // middle bar
   }

if(score>hscore) {
strcpy(hs,scor);
setfillstyle(1,0);
bar(maxx-140,337,maxx-20,348);
setcolor(14);
outtextxy(maxx-50,340,hs);
outtextxy(maxx-140,340,"High Score  ");

if(first==0) {
first=1;
}
}
setcolor(15); //text color of score
outtextxy(maxx-50,240,scor);
k++;
nosound();
}

z=100;

putimage(r1,r2,food,XOR_PUT); //food co-ordinate

repr1:
r1=random(450);
   if(r1<50)
      goto repr1;

repr2:
r2=random(400);
   if(r2<50)
goto repr2;

f=0;
      }


      i++;
      X[i]=x;
      Y[i]=y;

      b=a;
      if(kbhit()) //receving keys from keyboard
      {
rep:
a=getche();

if((b=='d' && a=='a') || (b=='a' && a=='d'))
a=b;

if((b=='w' && a=='s') || (b=='s' && a=='w'))
a=b;

if(a!='d' && a!='a' && a!='w' && a!='0' && a!='s')
a=b;
      }

     //moving the snake
      if(a=='w')
y -= bo;

      if(a=='s')
y += bo;

      if(a=='a')
x -= bo;

      if(a=='d')
x += bo;


//If collide - Game Over
if(maze>=1) {
//10 must be subtracted
if(x<=14 || x>=maxx-154-20 || y<=14 || y>=maxy-14-10) //left, right, top, bottom
a='0'; //Game Over
} // end if(maze==1)

if(maze>=2) {
//10 must be subtracted
//added maze
if(x>=104-10 && y>=104-10 && x<=maxx-254 && y<=114) //below than top
a='0'; //Game Over

if(x>=104-10 && y>=maxy-114-10 && x<=maxx-254 && y<=maxy-104) //above than bottom
      a='0'; //Game Over
} // end if(maze==2)

if(maze>=3) {
//score 200+
if(x>=104-10 && y>=154-10 && x<=114 && y<=maxy-164) //left
a='0'; //Game Over

if(x>=maxx-264-10 && y>=154 && x<=maxx-254 && y<=maxy-164) //right
      a='0'; //Game Over
} // end if(maze==3)

if(maze==4) {
if(x>=204-10 && y>=204-10 && x<=maxx-354 && y<=214) //below than top
a='0'; //Game Over
}// end if(maze==4)

   } //end for loop

   if(score>hscore)
   {
ofstream onfile("rattle.txt");
onfile<<scor;
onfile.close();
   }

   setcolor(15);
   outtextxy(maxx-140,390,"    Gameover");

   /*
   outtextxy(maxx-140,410,"Press 1 to Play Again");
   scanf("%c",ch);
   if(ch==1) {
goto playagain;
   }
   */
   sound(100);
   delay(400);
   nosound();
   getch();
   /* clean up */
   free(body);
   closegraph();
}

No comments:

Post a Comment