Tuesday, 20 December 2011

Car racing Game: Source code Step by Step - 0

I mentioned about developing car racing game earlier. So now i think i will supply the source code of it. Since it ain't small program, i'll make it several parts.
Here, i start with the main function, and next time i will add all necessary classes and its methods.
So, main function comes as follows



int main(void)
{ /* request auto detection */
 int gdriver = DETECT, gmode, errorcode;
  int i;
 /* initialize graphics and local variables */
 initgraph(&gdriver, &gmode, "..\\BGI");
 /* read result of initialization */
 errorcode = graphresult();
 if (errorcode != grOk) /* an error occurred */
 {
  printf("Graphics error: %s\n", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  getch();
  exit(1); /* terminate with an error code */
 }

 Ehel: //label, indicates that starting point of game

 char up=3; //max expectation of crush
 int color=start(); //user can choose his/her car color.
 setfillstyle(1,0);
 closegraph();
 initgraph(&gdriver, &gmode, "..\\BGI");//you may need to change location
 UMashin Mashin1(color);
 Mashin Mashin2[3]={14,14,14};
 Huree Huree1(13);
 /*clean up*/
 char bool1=0;
 char key=75;
 char songo1=0;
 char songo2=0;
 long int point=-100;
 i=0;

 chad: //label, that indicates when car crashes into another

 char level=1;
 int delayx=50;
 if(up==-1)
 {
  nahi: if(1==songoh()) //lable, that indicates restarting point
   goto Ehel;
  else goto End;
 }
 Mashin2[0].setY(550);
 Mashin2[1].setY(550);
 Mashin2[2].setY(550);
 closegraph();
 initgraph(&gdriver, &gmode, "..\\BGI");
 setfillstyle(0,0);
 settextstyle(0,0,2);
 outtextxy(460,210,"UP");
 outtextxy(400,60,"LEVEL");
 outtextxy(400,360,"POINT");
 outtextxy(400,400,strpoint(point));
 outtextxy(400,100,strpoint(level));
 bar(450,250,500,280);
 outtextxy(460,250,strpoint(up));
 Mashin1.put(126,375,0,0);
 while(key!=27)
 {
  if(i%8==0)
  {
   dahi: songo2=random(3); // replay
   if( songo1==songo2) goto dahi;
   Mashin2[songo2].setY(0);
   songo1=songo2;
   point+=100;
   bar(400,400,500,450);
   outtextxy(400,400,strpoint(point));
  }
  bool1=Huree1.put(bool1,3);
  Mashin2[0].put(126+0*76,30,1);
  Mashin2[1].put(126+1*76,30,1);
  Mashin2[2].put(126+2*76,30,1);
  if(kbhit())
  {
   key=getch();
   if(key==75||key=='a')
   {
    Mashin1.zalah(1,3);//navigate the car
   }
   else if(key==77||key=='d')
   {
    Mashin1.zalah(0,3);//navigate the car
   }
  }
/* takes care of crashing of car */

  if(!Mashin1.shalga(Mashin1.X(),Mashin2[0].X(),Mashin1.Y(),Mashin2[0].Y())||
  !Mashin1.shalga(Mashin1.X(),Mashin2[1].X(),Mashin1.Y(),Mashin2[1].Y())||
  !Mashin1.shalga(Mashin1.X(),Mashin2[2].X(),Mashin1.Y(),Mashin2[2].Y()))
  {
   up--;
   delay(1000);
   goto chad;
  }
  if(i%200==0&&delayx>5)
  {
   delayx-=5;
   bar(400,100,500,130);
   outtextxy(400,100,strpoint(level));
   level++;
  }
  if(delayx<=0)
  {
   delay(3000);
   closegraph();
   initgraph(&gdriver, &gmode, "..\\BGI");
   win();
   goto nahi;
  }
  delay(delayx);
  i++;
 }
 End: delay(1000); //label, that indicates the end of game
 closegraph();
 initgraph(&gdriver, &gmode, "..\\BGI");
 Point(point);
 end();
 /* clean up */
 closegraph();
 return 0;
}
Since it is only main function, many functions, of course unclear. But when its classes and methods supplied, every thing will become clear. So keep it in your mind please. This is not the game, but this is just the starting point of game.

No comments:

Post a Comment