Translate

Car Racing Game made in C/C++

#include<stdio.h>
#include<windows.h>
#include "graphics.h"
#include<math.h>
#include<dos.h>
#include<time.h>
#include<process.h>
#pragma comment(lib, "Winmm.lib")
#include<string.h>

#include<windows.h>
int stopcar=1 ,stoproot=1,stoprotatetrack=1,stopscore=1;

void sound(    void *param)
{
    while(kbhit()){
    for(int a=0;;a++){       
        PlaySound("123.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC);
    delay(1);
    }
    }
    _endthread();
}
int score=0;
void scoref(void *param){
   
   
    char p[1000];

        while(stopscore){
            score=score+1;
    sprintf(p,"%d",score);
    setcolor(14);
    settextstyle(1,0,2);
    outtextxy(290,20,p);
        delay(100);
    //    readimagefile("z.bmp",453,550,906,600);
    readimagefile("h.bmp",470,380,510,400);
        }
_endthread();
        }
void scoreboard(void){
    char p[1000];
    readimagefile("scb.bmp",0,0,1350,700);


    sprintf(p,"%d",score);
    setcolor(14);
    settextstyle(1,0,5);
    outtextxy(getmaxx()/2-150,getmaxy()/2-200,"YOUR SCORE");
    outtextxy(getmaxx()/2,getmaxy()/2-125,p);
    if(score<=2000){
    outtextxy(getmaxx()/2-150,getmaxy()/2-30,"KEEP IT UP");
    }
    if(score>=2000){
    outtextxy(getmaxx()/2-150,getmaxy()/2-30,"GOOD ONE...");
    }
}
struct arccoordstype a, b;
void finalroad(void *param){
   
readimagefile("strip.bmp",453,0,470,100);
readimagefile("strip.bmp",890,0,906,100);

        while(kbhit){
    delay(10);       
       
        }
_endthread();
        }
void collisiontest(int xh,int xe,int yh,int ye)
{
    if((yh==ye+35)){int l=xe;
        for(;l<=xe+30;l++)
        {
            for(int a=0;a<50;a++){
        if(l==xh+a)
        {
            delay(1000);
        outtextxy(500,275,"GAME OVER");
        stopcar=0 ,stoproot=0,stoprotatetrack=0,stopscore=0;
        delay(2000);
        cleardevice();
        scoreboard();
        getch();
        cleardevice();
   
        exit(0);
        }
       
            }
        }
   
   
    }
   
        }
int lastx=650,lasty=600;
int hh,ll,ii,kk,bb;
void cr(void){
        circle(getmaxx()/2,getmaxy()/2,100);


}
void load(void){
     settextstyle(1,0,5);
    outtextxy(500,550," LOADING.... ");
}
void r(void){
    rectangle(480,248,880,450);
}
void poly(void){

            int points[]={580,350,  680,250,  780,350,  680,450, 580,350, };         
   
    drawpoly(5, points);


}
void gotoxy(short x , short y) 
{
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position ={x, y};
    SetConsoleCursorPosition(handle,position);
}
void root1(void){
    readimagefile("strip.bmp",660,0,680,100);
    readimagefile("strip.bmp",660,200,680,300);
    readimagefile("strip.bmp",660,400,680,500);
   
    readimagefile("strip.bmp",454,0,460,100);
    readimagefile("strip.bmp",454,200,460,300);
    readimagefile("strip.bmp",454,400,460,500);
   
    readimagefile("strip.bmp",900,0,906,100);
    readimagefile("strip.bmp",900,200,906,300);
    readimagefile("strip.bmp",900,400,906,500);
   
    readimagefile("strip.bmp",454,600,460,700);
    readimagefile("strip.bmp",900,600,906,700);
    delay(150);
    readimagefile("blackstrip.bmp",660,0,680,100);
    readimagefile("blackstrip.bmp",660,200,680,300);
    readimagefile("blackstrip.bmp",660,400,680,500);
   
    readimagefile("blackstrip.bmp",454,0,460,100);
    readimagefile("blackstrip.bmp",454,200,460,300);
    readimagefile("blackstrip.bmp",454,400,460,500);
   
    readimagefile("blackstrip.bmp",900,0,906,100);
    readimagefile("blackstrip.bmp",900,200,906,300);
    readimagefile("blackstrip.bmp",900,400,906,500);
    readimagefile("blackstrip.bmp",454,600,460,700);
    readimagefile("blackstrip.bmp",900,600,906,700);

    readimagefile("strip.bmp",660,100,680,200);
    readimagefile("strip.bmp",660,300,680,400);
   
   

    readimagefile("strip.bmp",454,100,460,200);
    readimagefile("strip.bmp",454,300,460,400);
    readimagefile("strip.bmp",900,100,906,200);
    readimagefile("strip.bmp",900,300,906,400);
    readimagefile("strip.bmp",454,600,460,650);
    readimagefile("strip.bmp",900,600,906,650);
   
    delay(150);
   
    readimagefile("blackstrip.bmp",660,100,680,200);
    readimagefile("blackstrip.bmp",660,300,680,400);

    readimagefile("blackstrip.bmp",454,100,460,200);
    readimagefile("blackstrip.bmp",454,300,460,400);
    readimagefile("blackstrip.bmp",900,100,906,200);
    readimagefile("blackstrip.bmp",900,300,906,400);
    readimagefile("blackstrip.bmp",454,600,460,650);
    readimagefile("blackstrip.bmp",900,600,906,650);
   

}

void root(void *param){
   


        while(stoproot){
            root1();
            delay(1);
        }
_endthread();
        }
void traffic(int x,int b,int c)
{
    setfillstyle(1,c);
    setcolor(BLACK);
    bar3d(x,b,x+30,b+30,0,0);
    setfillstyle(1,BLUE);
    setcolor(BLACK);
    bar3d(x+5,b+30,x+25,b+40,0,0);
    setfillstyle(1,8);
    setcolor(BLACK);
    bar3d(x+1,b+32,x+5,b+38,0,0);
    setfillstyle(1,8);
    setcolor(BLACK);
    bar3d(x+26,b+32,x+30,b+38,0,0);
    setcolor(8);
    setfillstyle(1,8);
    setcolor(BLACK);
}
int l(void){
    int c;
    clock_t time;
    time=clock();
    srand(time);
    c=rand()/10*5;
    return c;

}   
void n(void *param){
   
la :int cc;cc=1+ rand()%13;
    bb=0;
    //cleardevice();
    while(stopcar==1){
    bb++;
   


         if(bb==2000){
    p: clock_t time;
    time=clock();
    srand(time);
   
        ii=200-((rand()%2)*10);
        kk=250+((rand()%3)*20);
        hh=150+((rand()%3)*30);
   
        goto la;
   
   
    }

    //delay(-1);
    clock_t time;
    time=clock();
    srand(time);
   

    traffic(kk+300,bb,cc);
    traffic(ii+600,bb,cc+1);
    traffic(hh+500,bb,cc+2);
    traffic(kk+300,bb-400,cc);
    traffic(ii+260,bb-700,cc+1);
    traffic(kk+270,bb-1000,cc);
    traffic(ii+600,bb-1300,cc+1);
    traffic(hh+500,bb-1300,cc+2);
    traffic(hh+650,bb-120,cc+2);

   
         collisiontest(lastx,kk+300,lasty,bb);
             collisiontest(lastx,ii+600,lasty,bb);
             collisiontest(lastx,hh+500,lasty,bb);
                      collisiontest(lastx,kk+300,lasty,bb-400);
                      collisiontest(lastx,ii+260,lasty,bb-700);
                              collisiontest(lastx,kk+270,lasty,bb-1000);
                                  collisiontest(lastx,ii+600,lasty,bb-1300);
        collisiontest(lastx,hh+500,lasty,bb-1300);
         collisiontest(lastx,hh+650,lasty,bb-120);
   
    }
    _endthread();
}

void flame (void)
{
    readimagefile("flame.bmp",150,90+20,555,215);
    readimagefile("fcar.bmp",500,80,950,200);
}
void countdown(void)
{
  int i=0;
char c[10];
   
   
    for (i = 3; i > 0; i--)
   {

     
     
       setcolor(WHITE);
    cr();
     load();
     r();poly();
     setcolor(RED);
   flame();
 
   settextstyle(1,0,9);

      sprintf(c,"%d",i);
      outtextxy(getmaxx()/2-35, getmaxy()/2-45, c);
      delay(2);
        
      for(int k=0;k<=100;k++){
   
     
          line(680+k,250+k, 680, 350);
         
       
     delay(2);
     
   
      }

      for(int k=0;k<=100;k++){
      line(780-k,350+k, 680, 350);
     delay(2); 
     
      }

      for(int k=0;k<=100;k++){
      line(680-k,450-k, 680, 350);
       delay(2);
     
     
      }

      for(int k=0;k<=100;k++){
      line(580+k,350-k, 680, 350);
      delay(2);
     
            }
      cleardevice();
    }

    cleardevice();
}
void screen(void)
{int g,angle;
   
    setcolor(WHITE);   
    settextstyle(1,0,5); 
    outtextxy(480,50,"CAR GAME");
   
     
   setcolor(LIGHTBLUE);
    settextstyle(1,0,3);
    outtextxy(598,300,"ARSLAN");       
    outtextxy(598,330,"MUNEEB");settextstyle(1,0,3);
    outtextxy(598,360 ,"Zain");
       while (!kbhit()) {

g = rand() % 15 + 1;
g=g-3;//increment to g  
setcolor(g);
   delay(200);
   int k=0;
         
       int points[]={30+k,180+100,   10+k,180+100,   10+k,150+100,   40+k,150+100,   40+k,117+100,   120+k,117+100,   120+k,150+100,   150+k,150+100,
   
                                150+k,180+100,   120+k,180+100, };         
   
    drawpoly(10, points);
    line(55+k, 180+100, 98+k, 180+100);

    rectangle(49+k,124+100,74+k,146+100);
    rectangle(82+k,124+100,110+k,146+100);
    line(55+k,156+100, 62+k, 156+100);
    line(88+k,156+100, 95+k, 156+100);
    circle(110+k, 180+100, 12);
    circle(43+k, 180+100, 12);
   
   
    g=g+2;
    setcolor(g);
   settextstyle(1,0,4);
    delay(200);
    outtextxy(265,600,"PRESS ANY KEY TO CONTINUE.... ");
   
     while(angle<=360)
   {
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,200);
      setcolor(RED);
      getarccoords(&a);
        circle(a.xstart,a.ystart,angle);
      setcolor(1);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
      getarccoords(&a);
      setcolor(YELLOW);
      circle(a.xstart,a.ystart,30);
      angle = angle+5;
      delay(50);
   }
   delay(100);


   
       }

 
   
       }
void truckmenu(void)
{
    readimagefile("c1.bmp",80,100,155,225);
    settextstyle(1,0,2);
    outtextxy(75,232,"FEATURES");settextstyle(6,0,2);
    outtextxy(80,250,"SPEED...");
    outtextxy(80,270,"RACE...");
   
    readimagefile("c2.bmp",555,90,650,215);
    readimagefile("c3.bmp",950,90,1200,170);
    readimagefile("c4.bmp",90,450,250,535);
    while(!kbhit()){
   
    delay(200);
     
   
}
    getch();
}
void trk(void)
{while(stoprotatetrack!=0){
    int a;
    for( a=0;a<=700;a++){
    //readimagefile("tr.bmp",395,0+a,453,700+a);
        readimagefile("g2.bmp",395,-700+a,453,0+a);
       
    }
    a=0;
}

}
void rotatetrack(void *param)
{
        trk();
        delay(1);
        trk();
        delay(1);
   
_endthread();
}
void game(void)
{    int f,s;
char key;
 char p[10];
   
void *plane;
    HANDLE handle2;
    HANDLE handle1;
    HANDLE handle5;
    HANDLE handle8;





    HANDLE handle10;
int fx,fy=582;
readimagefile("g.bmp",0,0,453,700);
readimagefile("gg.bmp",906,0,1360,700);
readimagefile("car.bmp",650,600,700,700);

plane=malloc(imagesize(650,600,700,700));        //creating a pointer to store image drawn on screen
getimage(650,600,700,700,plane);                //storing the plane image in memory        
putimage(650,600,plane,1);                        //drawing the image on the same place with xor mod, this will erase the image

putimage(700,700,plane,0);
lastx=650;
lasty=600;                    
     if(kbhit){
         handle1 =  (HANDLE) _beginthread(n,0,0);
         handle8 =  (HANDLE) _beginthread(scoref,0,0);
         handle5 =  (HANDLE) _beginthread(root,0,0);   
       
         collisiontest(lastx,kk,lasty,bb);
for(int s=0;;s++){
key=getch();   
handle2 =  (HANDLE) _beginthread(sound,0,0);   
   

if(key==72){
        if(lasty>0){
        putimage(lastx,lasty,plane,1);
        lasty=lasty-20;
        putimage(lastx,lasty,plane,0);
   
        PlaySound("123.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC);

        }
}
if(key==80){
        if(lasty<600){
        putimage(lastx,lasty,plane,1);
        lasty=lasty+20;
        putimage(lastx,lasty,plane,0);
   
        }
}
    if(key==75){
        if(lastx>=471){
        putimage(lastx,lasty,plane,1);
        lastx=lastx-20;
        putimage(lastx,lasty,plane,0);
   
        }}
    if(key==77){
        if(lastx<=830){
        putimage(lastx,lasty,plane,1);
        lastx=lastx+20;
        putimage(lastx,lasty,plane,0);
   
        }
       
       

   
    }
}

}
}
 void main(){
    int x=220 ,y=675;

    initwindow(1360,700,"Multithreaded");
   
        delay(10000);
   
countdown();
cleardevice();
screen();
getch();
cleardevice();
truckmenu();
       
        while(!kbhit()){
        delay(1000);

}
getch();
   
cleardevice();

HANDLE handle9;

   
    //handle9 =  (HANDLE) _beginthread(rotatetrack,0,0);
   

    game();
    scoreboard();
   

    getch();

}
Previous
Next Post »

Contact Form

Name

Email *

Message *