*/
Check out and contribute to CodePedia, the wiki for developers.
*/

View ghost hunter\MAZE_PU2.CPP

ghost hunter game v1.0

Submitted By: ravikalluri
Rating: (Not rated) (Rate It)


/* clock_t method:clock_t start;start=clock();
error- >50&<500ms 9.89%error
       >500ms 1.098%error
time_t method:accurate but app. to 1000ms
*/


#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>


int devil_radius=0;

class opponent
 {
   int x,y;
   int dir;
   int hit;
  public:
  opponent()
  {}
            opponent(int cox,int coy,int di)
     {
       x=cox;
       y=coy;
       dir=di;
       hit=0;
       }
   int getdir()
     {
      return dir;
      }
   int getx()
     {return x;}

   int gety()
     {return y;}
   void setx(int incx)
   {x+=incx;}
   void sety(int incy)
   {y+=incy;}
    void setzero()
    {x=800;y=0;}
   int gethit()          //0-no,1-yes
   {return hit;}
   void sethit(int j)
   {hit=j;}
   void changedir(int d)
   {
   dir=d;
   }
    void draw()
     {
      setcolor(14);
      setfillstyle(1,YELLOW);
      circle(x,y,14);
      floodfill(x,y,YELLOW);

      setcolor(3);
      setfillstyle(1,3);
      circle(x,y,11);
      floodfill(x,y,3);

      setcolor(9);
      setfillstyle(1,9);
      circle(x,y,8);
      floodfill(x,y,9);

      setcolor(7);
      setfillstyle(1,7);
      circle(x,y,5);
      floodfill(x,y,7);

      setcolor(13);
      setfillstyle(1,13);
      circle(x,y,2);
      floodfill(x,y,13);

     }
    void erase()
     {
      setcolor(BLACK);
      setfillstyle(1,BLACK);
      circle(x,y,14);
      floodfill(x,y,BLACK);
      circle(x,y,8);
      }


};



class chaser
{
    int x,y;
    int dir;
    int move;
    int animation;
    int ls;   // limited laser shots
    int lsx,lsy;   //co-ordinates of laser shot
    int lstf;    //if a laser has been shot,0-no,1-yes
    int lsdir;

public:
    chaser()
  {}
   chaser(int cox,int coy,int di)
     {
       x=cox;
       y=coy;
       dir=di;
       move=1;
       animation=1;
       ls=0;
       lstf=0;
       }
   int getdir()
     {
      return dir;
       }
   int getx()
     {return x;}

   int gety()
     {return y;}
   void setx(int incx)
   {x+=incx;}
   void sety(int incy)
   {y+=incy;}
    int getmove()
    {return move;}
    void setmove(int mo)
    {move=mo;}
   void changedir(int d)
   {
   dir=d;
   }
    void draw()
     {
      if(animation>360)animation=0;
      animation+=40;

      setcolor(1);
      setfillstyle(1,1);
      circle(x,y,14);
      floodfill(x,y,1);

      setfillstyle(1,RED);
      pieslice(x,y,animation-20,animation+20,14);
     }
    void erase()
     {
     setfillstyle(1,0)//adjustment
     bar(x-14,y-14,x+14,y+14);

    /*  setcolor(BLACK);
      setfillstyle(1,BLACK);
      circle(x,y,14);
      floodfill(x,y,BLACK);
      pieslice(x,y,animation-20,animation+20,14);*/

      }
      int getlstf()
      {return lstf;}
      void setlstf(int s)
      {lstf=s;}

      int getlsx()
      {return lsx;}
      int getlsy()
      {return lsy;}

      void setlsx(int x)
      {lsx=x;}
      void setlsy(int y)
      {lsy=y;}
      void setlsdir(int d)
      {lsdir=d;}
      int getlsdir()
      {return lsdir;}

      void setls(int h)
      {ls+=h;}
      int getls()
      {return ls;}
      void draw_laser()
      {
        setcolor(15);
        if((lsdir==1)&&(getpixel(lsx+21,lsy)!=7)){ lsx+=10;
        line(lsx-5,lsy,lsx+5,lsy); }
        if((lsdir==2)&&(getpixel(lsx,lsy+21)!=7)){ lsy+=10;
        line(lsx,lsy-5,lsx,lsy+5); }
        if((lsdir==3)&&(getpixel(lsx-21,lsy)!=7)){lsx-=10;
        line(lsx-5,lsy,lsx+5,lsy); }
        if((lsdir==4)&&(getpixel(lsx,lsy-21)!=7)){ lsy-=10;
        line(lsx,lsy-5,lsx,lsy+5)}
       }
       void erase_laser()
      {
        setcolor(0);
        if((lsdir==1)||(lsdir==3)) line(lsx-5,lsy,lsx+5,lsy);
        if((lsdir==2)||(lsdir==4)) line(lsx,lsy-5,lsx,lsy+5);
       }
};


class bullet
{  private:
int x,y,taken;
public:
     bullet(){}
     bullet(int xc,int yc){x=xc;y=yc;taken=0;}
     int getx(){return x;}
     int gety(){return y;}
     void settaken(){taken=1;}
     int gettaken(){return taken;}
     void draw()
     {
      setfillstyle(1,14);
      fillellipse(x,y,3,3);
      }
      void erase()
     {
      setcolor(0);
      setfillstyle(1,0);
      fillellipse(x,y,4,4);
      }
};


class monster
{
   int x,y;
   int dir;
   int hit;
   int ls_hit;
  public:
  monster()
  { hit=0;
    ls_hit=14;
    x=255;y=165;
  }
   int getdir()
     {
      return dir;
      }
   int getx()
     {return x;}

   int gety()
     {return y;}
   void setx(int incx)
   {x+=incx;}
   void sety(int incy)
   {y+=incy;}

    void setzero()
    {x=800;y=0;}
   int gethit()          //0-no,1-yes
   {return hit;}
   void sethit(int j)
   {hit=j;}

   void changedir(int d)
   {
   dir=d;
   }
   int getls_hit()
   {return ls_hit;}
   void setls_hit()
    {ls_hit--;}
   void draw()
   { setcolor(3);
     circle(x,y,14);
     setfillstyle(1,3);
     floodfill(x,y,3);

     setcolor(5);
     circle(x,y,12);
     setfillstyle(1,5);
     floodfill(x,y,5);

     setcolor(7);
     circle(x,y,9);
     setfillstyle(1,7);
     floodfill(x,y,7);

     setcolor(9);
     circle(x,y,6);
     setfillstyle(1,9);
     floodfill(x,y,9);

     setcolor(13);
     circle(x,y,3);
     setfillstyle(1,13);
     floodfill(x,y,13);

     setcolor(0);
     setfillstyle(1,0);
     fillellipse(x,y,devil_radius,devil_radius);
    }
    void erase()
    {
    setcolor(0);
  //   circle(x,y,14);
     setfillstyle(1,0);
   //  floodfill(x,y,0);
    fillellipse(x,y,14,14);
    }
};
class power
{
  private:
  int x,y,interval;
  int taken;
  int radius;
  public:
     power(){taken=0;radius=14;}

     void setx(int xc){x=xc;}
     void sety(int yc){y=yc;}
     int getx()
     {return x;}
     int gety()
     {return y;}
     void settaken(int y){taken=y;}
     int gettaken(){return taken;}
     void setradius(int r)
     {radius=r;}
     int getradius(){return radius;}
     void setinterval(int s){interval=s+1;}
     int getinterval(){return interval;}


     void draw()
     {setcolor(radius%16);
      setfillstyle(1,radius%16);
      int rad=abs(14-(radius%28));
      fillellipse(x,y,rad,rad);
     }

     void erase()
     {
      setcolor(0);
      setfillstyle(1,0);
      bar(x-14,y-14,x+14,y+14);
     // fillellipse(x,y,radius,radius);
      }
};

void border(int x1,int y1,int x2,int y2)
       {
         setcolor(7);
         setfillstyle(1,7);
         bar(x1-15,y1-15,x2+15,y2+15);
        }

void bang()
  {
    int a[9]={1000,950,900,850,800,750,700,650,700};
    for(int i=0;i<9;i++)
    { sound(a[i]);
      delay(150);
      nosound();
    }
  }

void intro()
{
  setcolor(15);
  settextstyle(1,0,4);
  outtextxy(50,80,"INSTRUCTIONS");
  outtextxy(30,110,"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  settextstyle(1,0,3);
  outtextxy(50,130,"Use arrow keys to navigate.");
  outtextxy(50,160,"Press s to shoot lasers ");
  outtextxy(50,190,"Use your shots to knock down the ghosts");
  outtextxy(140,235,"Junior Ghost");
  outtextxy(140,275,"Senior Ghost");
  outtextxy(140,315,"Ghost Hunter");
  outtextxy(140,355,"Ammu .+5 shots");

  settextstyle(1,0,1);
  outtextxy(430,10,"Author : Ravishankar");
  int x=80,y=250;
      setcolor(14);
      setfillstyle(1,YELLOW);
      fillellipse(x,y,14,14);
      setcolor(3);
      setfillstyle(1,3);
      fillellipse(x,y,11,11);
      setcolor(9);
      setfillstyle(1,9);
      fillellipse(x,y,8,8);
      setcolor(7);
      setfillstyle(1,7);
      fillellipse(x,y,5,5);
      setcolor(13);
      setfillstyle(1,13);
      fillellipse(x,y,2,2);
x=80;y=290;
     setcolor(3);
     setfillstyle(1,3);
     fillellipse(x,y,14,14);
     setcolor(5);
     setfillstyle(1,5);
    fillellipse(x,y,12,12);
     setcolor(7);
     setfillstyle(1,7);
    fillellipse(x,y,9,9);
     setcolor(9);
    setfillstyle(1,9);
    fillellipse(x,y,6,6);
     setcolor(13);
     setfillstyle(1,13);
     fillellipse(x,y,3,3);
x=80;y=330;
      setcolor(1);
      setfillstyle(1,1);
      fillellipse(x,y,14,14);
      setfillstyle(1,RED);
      pieslice(x,y,0,40,14);
x=80,y=370;
      for(int i=0;(!kbhit());i++)
     { setcolor(i%16);
      setfillstyle(1,i%16);
      fillellipse(x,y,abs(14-(i%28)),abs(14-(i%28)));
      delay(300);
      }

    }
void main()
{
  int gdriver = DETECT, gmode, errorcode;
  initgraph(&gdriver, &gmode, "");
  cleardevice();
  intro();
  getch();
  unsigned int level;
  char choice;
  int flag_pill;
up:cleardevice();
  setcolor(15);
  settextstyle(1,0,3);
  outtextxy(100,100,"SELECT LEVEL ");
  outtextxy(100,130,"1.BEGINNER ");
  outtextxy(100,160,"2.MASTER ");
  outtextxy(100,190,"3.EXPERT ");
  choice=getche();
  switch(choice)
  { case '1':
          level=25;
          flag_pill=10;
          break;
    case '2':
          level=15;
          flag_pill=6;
          break;
    case '3':
          level=5;
          flag_pill=4;
          break;
    case 27:
          closegraph();
          exit(0);
    default:
          goto up;
  }

  cleardevice();
  border(15,15,15,465);
  border(15,15,465,15);
  border(465,15,465,465);
  border(15,465,465,465);
  border(195,45,195,75);
  border(75,75,195,75);
  border(255,75,345,75);
  border(405,75,405,135);
  border(45,135,105,135);
  border(165,135,345,135);
  border(105,195,105,195);
  border(75,195,75,255);
  border(105,255,105,255);
  border(165,195,165,285);
  border(195,195,285,195);
  border(285,195,285,285);
  border(225,255,225,285);
  border(405,195,405,225);
  border(345,195,345,345);
  border(165,345,345,345);
  border(405,285,405,405);
  border(435,345,435,345);
  border(75,315,75,315);
  border(105,315,105,405);
  border(135,405,165,405);
  border(45,375,45,405);
  border(225,405,285,405);
  border(345,405,345,405);
  delay(5000);

  int number=8;
  opponent chor[8];
  void *ptr;int size;
  chaser police(75,375,4);
  monster devil;
//power pills
  power pill[6];
  randomize();
  for(int i=0;i<6;i++)
  {
up1:
    int tempxy=rand()% 465;
    tempxy=tempxy-(tempxy%15);
    pill[i].setx(tempxy);
    tempxy=rand()% 465;
    tempxy=tempxy-(tempxy%15);
    pill[i].sety(tempxy);
    if(getpixel(pill[i].getx(),pill[i].gety())==7) goto up1;
    pill[i].setinterval(rand()%20);
  }

  for(i=1;i<6;i++)
   pill[i].setinterval(pill[i].getinterval()+pill[i-1].getinterval());

  chor[0]=opponent(435,45,2);
  chor[1]=opponent(45,105,1);
  chor[2]=opponent(195,315,3);
  chor[3]=opponent(195,405,2);
  chor[4]=opponent(255,165,1);
  chor[5]=opponent(375,255,2);
  chor[6]=opponent(435,375,2);
  chor[7]=opponent(105,225,1);
  int dirn[8][4];
  int devil_dirn[4];
  int pol_dirn[4];
  int pol_temp1;
  int devil_temp1;
  char bullets[4];
  int temp1[8];
  int input=50;
  int range;
  int win=0;
  int pills=0;
  time_t first,second;
  chor[0].draw();
  size=imagesize(chor[0].getx()-14,chor[0].gety()-14,chor[0].getx()+14,chor[0].gety()+14);
  ptr=malloc(size);
  getimage(chor[0].getx()-14,chor[0].gety()-14,chor[0].getx()+14,chor[0].gety()+14,ptr);
//display all gunshots-coins
  bullet bt[122];
  int btemp=0,bti,btj;
  for( bti=45;bti<=465;bti+=30)
  { for( btj=45;btj<=465;btj+=30)
     {
       if(getpixel(btj,bti)!=7)
         { bt[btemp]=bullet(btj,bti);
           btemp++;

          }
      }
   }

  for(btemp=0;btemp<122;btemp++)
   bt[btemp].draw();
  randomize();
//start the timer
first=time(NULL);
//dispay text
setcolor(15);
settextstyle(1,0,3);
outtextxy(500,70,"SHOTS LEFT:");
outtextxy(500,130,"TIME LEFT:");
//begin the loop!
 while(input!=27)
  {
   while(!kbhit())
   {
//co-ords of chors
  for(int loop=0;loop<number;loop++)
  {
  if(chor[loop].gethit()==1) continue;
  dirn[loop][0]=dirn[loop][1]=dirn[loop][2]=dirn[loop][3]=0;
  if(getpixel((chor[loop].getx()+17),chor[loop].gety())!=7)dirn[loop][0]=1;
  if(getpixel(chor[loop].getx(),(chor[loop].gety()+17))!=7)dirn[loop][1]=1;
  if(getpixel((chor[loop].getx()-17),chor[loop].gety())!=7)dirn[loop][2]=1;
  if(getpixel(chor[loop].getx(),(chor[loop].gety()-17))!=7)dirn[loop][3]=1;
ret:
  temp1[loop]=rand() % 4;
  if(dirn[loop][temp1[loop]]==0) goto ret;
  chor[loop].changedir(temp1[loop]+1);
   }
//co-ords of devil chor

if((difftime(time(NULL),first)>50)&&(devil.gethit()!=1))
{ devil_dirn[0]=devil_dirn[1]=devil_dirn[2]=devil_dirn[3]=0;
  if(getpixel((devil.getx()+17),devil.gety())!=7)devil_dirn[0]=1;
  if(getpixel(devil.getx(),(devil.gety()+17))!=7)devil_dirn[1]=1;
  if(getpixel((devil.getx()-17),devil.gety())!=7)devil_dirn[2]=1;
  if(getpixel(devil.getx(),(devil.gety()-17))!=7)devil_dirn[3]=1;
ret1:
  devil_temp1=rand() % 4;
  if(devil_dirn[devil_temp1]==0) goto ret1;
  devil.changedir(devil_temp1+1);
 }


//co-ord of police
  pol_dirn[0]=pol_dirn[1]=pol_dirn[2]=pol_dirn[3]=0;
  if(getpixel((police.getx()+16),police.gety())!=7)pol_dirn[0]=1;
  if(getpixel(police.getx(),(police.gety()+16))!=7)pol_dirn[1]=1;
  if(getpixel((police.getx()-16),police.gety())!=7)pol_dirn[2]=1;
  if(getpixel(police.getx(),(police.gety()-16))!=7)pol_dirn[3]=1;


 if(pol_dirn[police.getdir()-1]==0) police.setmove(0);
// if(pol_dirn[pol_temp1]==0) {police.setmove(0);}
 if(pol_dirn[pol_temp1]==1)
 { police.changedir(pol_temp1+1);
   police.setmove(1)}


//display chors
 for(int temp=0;temp<10;temp++)
      {
      for(int loop=0;loop<number;loop++)
      {setcolor(0);
       setfillstyle(SOLID_FILL,BLACK);
       if(chor[loop].gethit()==1) continue;
      circle(chor[loop].getx(),chor[loop].gety(),14);
      floodfill(chor[loop].getx(),chor[loop].gety(),0);
      if(chor[loop].getdir()==1)chor[loop].setx(3);
      if(chor[loop].getdir()==2)chor[loop].sety(3);
      if(chor[loop].getdir()==3)chor[loop].setx(-3);
      if(chor[loop].getdir()==4)chor[loop].sety(-3);
      putimage(chor[loop].getx()-14,chor[loop].gety()-14,ptr,2);
       }
//display devil
    if((difftime(time(NULL),first)>50)&&(devil.gethit()!=1