Monday, 26 December 2011

Car racing Game: Source code Step by Step - 1

I know that looking at plain code is not desirable, so just keep this code with you and wait for few days. The game, car racing, will be completed in few days. Just as i said, it is pain in the ass looking at the code that doesn't have an explanation but if you pay attention and need any help, i would gladly support you in any way.
I'm ready to explain every single line if you require. But first complete the game, and execute the game.
First thing is first.
So here code comes.


/******** Car class *******/
class Mashin // mashin => machine
{
 protected:
  void *mash1;
  int x,y;
 public:
  Mashin(int color); //constructor of machine
  ~Mashin() // destroyer
  {
   delete mash1;
  };
 int shalga(int x1,int x2,int y1,int y2)
 {
  if(x1==x2 && abs(y1-y2)<100) return 0;
   return 1;
 };
 void setX(int X)
 {
  x=X;
 }
 void setY(int Y)
 {
  y=Y;
 }
 void put(int x1,int i,int bool)
 {
  x=x1;
  if(y < 550)
   y+=i;
  putimage(x,y,mash1,COPY_PUT);
  if(bool==1)
   bar(x,y-i,x+75,y);
 };
 int X()
 {
  return x;
 };
 int Y()
 {
  return y;
 };
};Mashin::Mashin(int color)//if you recall, user can choose his/her car color. it will color the car according to chosen color. In short, it draws your car
{
 char m1[20][15]={
 {0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0},//1
 {0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},//2
 {0, 0, 0, 0, 0, 0, color, color, color, 0, 0, 0, 0, 0, 0},//3
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//4
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//5
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//6
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//7
 {8, 8, 8, 8, 9, color, color, color, color, color, 9, 8, 8, 8, 8},//8
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//10
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//11
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//12
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//13
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//14
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//15
 {0, 0, 0, 0, 0, color, color, color, color, color, 0, 0, 0, 0, 0},//16
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//18
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//17
 {8, 8, 8, 8, 9, color, color, color, color, color, 9, 8, 8, 8, 8},//18
 {8, 8, 8, 8, 0, color, color, color, color, color, 0, 8, 8, 8, 8},//19
 {8, 8, 8, 8, 0, 0, color, color, color, 0, 0, 8, 8, 8, 8} //20
};

int i;
int j;
for ( i=0; i<20; i++ )
{
 for (j=0; j<15; j++)
 {
  setfillstyle(1,m1[i][j]);
  /* draw the bar */
  bar(5*j,5*i, 5*j+5,5*i+5);
 }
}

 mash1=malloc(imagesize(0,0,75,100));
 getimage(0,0,75,100,mash1);
}
/*************main car**********/
class UMashin:public Mashin // mashin => machine.
{
 public:
  void zalah(char,int); //zalah=> navigation function
  void put(int x1,int y1,int i,int bool)
  {
   x=x1; y=y1;
   putimage(x,y,mash1,COPY_PUT);
   if(bool==1)
    bar(x,y-i,x+75,y);
   if(y>400)
    y=0;
  };
  UMashin(int color):Mashin(color)
  {
   x=126; y=375;
  };
};

void UMashin::zalah(char zal,int too)//definition of navigation function
{
 if(zal==1&&x>200)
 {
  x-=76;
  putimage(x,y,mash1,COPY_PUT);
  bar(x+76,375,x+76*2,475);
 }
 else if(zal==0&&(125+76*(too-1))>x)
 {
  x+=76;
  putimage(x,y,mash1,COPY_PUT);
  bar(x-76,375,x-1,475);
 }
}

/*************Border class*********/
class Huree // Huree => border
{
 void *huree;
 public:
  Huree(int color);
  ~Huree()
  {
   delete huree;
  };
  int put(int bool,int too)
  {
   if(bool==0)
   {
    putimage(100,0,huree,COPY_PUT);
    putimage(100+25+76*too,25,huree,COPY_PUT);
    setfillstyle(1,0);
    bar(100+25+76*too,0,100+50+76*too,25);
    bool=1;
   }
   else
   {
    putimage(100,25,huree,COPY_PUT);
    setfillstyle(1,0);
    bar(100,0,125,25);
    putimage(100+25+76*too,0,huree,COPY_PUT);
    bool=0;
   }
   return bool;
  };
};

Huree::Huree(int color)
{
 int i;
 for (i=0; i<(getmaxy()/25); i++)
 {
  setfillstyle(1,color);
  bar(100,50*i,125,
  50*i+25);
 }
 huree=malloc(imagesize(100,0,125,500));
 getimage(100,0,125,500,huree);
};

I apologize for that i used inappropriate names for functions and classes.

No comments:

Post a Comment