Monday, 30 January 2012

Car racing Game: Source code Step by Step - 2

OK, here is the last step of the game. You can copy this code and arrange these 3 steps as follows.
step - 1
step - 2
step - 0
I mean, 1st copy the step - 1 into some CPP file and then step - 2 and lastly step - 0.
Hope you'll enjoy the game.


//******************STRPOINT
char *strpoint(long int point)
{
   int i=0,j;
   char *str;
   str=(char*)malloc(10);
   if(point==0)
   {
     str[0]=48;
     str[1]='\0';
   }
   else
   {
      while(pow10(i)<=point) i++; str[i]='\0'; for(j=i-1;j>=0;j--)
      {
       str[j]=point%10+48;
       point=point/10;
      }
   }
   return str;
   }
//********************END
void end(void)
{
   setfillstyle(1,9);
   bar(0,0,650,500);
   char *a="GAME OVER";
   settextstyle(0,0,4);
   textheight(a);
   outtextxy(160,200,a);
   settextstyle(0,0,1);
   outtextxy(50,400," Created by Bayasgalantuguldur");
   outtextxy(50,410,"Computer science and managements school.");
   delay(4000);
   getch();
}
//*******************START
int Call(char zal,int too,int *x)
{
   if(zal==1&&*x>125)
   {
     *x-=26;
     setfillstyle(1, 4);
     bar(*x,375,*x+25,400);
     setfillstyle(1,9);
     bar(*x+26,375,*x+26*2,400);
     return -1;
     //*col--;
   }
   else if(zal==0&&(125+26*(too-1))>*x)
   {
     *x+=26;
     setfillstyle(1, 4);
     bar(*x,375,*x+25,400);
     setfillstyle(1, 9);
     bar(*x-26,375,*x-1,400);
     return 1;
     //*col++;
   }
   else return 0;
}

int Color(int too)
{
   char key=0;
   int color=0;
   int x=125;
   setfillstyle(1, 4);
   bar(x,375,x+25,400);
   settextstyle(0,0,1);
   outtextxy(100,330,"Select the color of your car");
   while(key!=13)
   {
     key=getch();
     if(key==75||key=='a'|key=='d')
       color+=Call(1,too,&x);
     else if(key==77||key=='a'||key=='d')
       color+=Call(0,too,&x);
   }
   return color;
}

int start(void)
{
   char ungu[5]={8,11,12,5,13};
   setfillstyle(1,9);
   bar(0,0,650,500);
   char *a="START GAME";
   settextstyle(0,0,4);
   textheight(a);
   outtextxy(160,200,a);
   int i;
   for(i=0;i<5;i++)
   {
     setfillstyle(1,ungu[i]);
     bar(125+26*i,350,150+26*i,375);
   }
   return ungu[Color(5)];
}
//******************SONGOH*************//
int UpDown(char ud,int x)
{
   if(ud==1&&x==-1)
   {
     x=1;
     setfillstyle(1,9);
     bar(220,190,430,230);
     outtextxy(230,200,"Play again");
     setfillstyle(1,0);
     bar(220,240,430,280);
     outtextxy(230,250,"EXIT");
   }
   else if(ud==0&&x==1)
   {
     x=-1;
     setfillstyle(1,0);
     bar(220,190,430,230);
     outtextxy(230,200,"Play again");
     setfillstyle(1,9);
     bar(220,240,430,280);
     outtextxy(230,250,"EXIT");
   }
   return x;
}

int songoh()
{
   int x=1;
   settextstyle(0,0,2);
   setfillstyle(1,9);
   bar(220,190,430,230);
   outtextxy(230,200,"Play again");
   setfillstyle(1,0);
   bar(220,240,430,280);
   outtextxy(230,250,"EXIT");
   int key=0;
   while(key!=13)
   {
     key=getch();
     if(key==72)
       x=UpDown(1,x);
     else if(key==80)
       x=UpDown(0,x);
   }
   return x;
}
//***************WIN**************//

void win()
{
   settextstyle(0,0,2);
   int i;
   for(i=0;i<50;i++)
   {
     setfillstyle(1,9);
     if(i%2==0)
     {
       setfillstyle(1,9);
       bar(220,190,400,230);
       outtextxy(230,200," You Won");
     }
     else
     {
       setfillstyle(1,4);
       bar(220,190,400,230);
       outtextxy(230,200," You Win");
     }
     delay(100);
   }
   delay(500);
}
//***********POINT***********//

void Point(long int point)
{
   settextstyle(0,0,2);
   outtextxy(190,200,"Your score:");
   settextstyle(0,0,3);
   outtextxy(400,230,strpoint(point));
   delay(3000);
}

1 comment:

  1. yostoi yanztai baahan header file nemeed l bolj bn daa. Saihan boljee. Ahiad enenees iluu togloom hiigeerei.

    ReplyDelete