Racing cars

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<dos.h>

void draw(int x,int y)
{
setcolor(6);
setfillstyle(SOLID_FILL,6);
if(x%2==0)
{
fillellipse(x,y,12,10);
fillellipse(x+60,y,12,10);
}
else
{
fillellipse(x,y,10,12);
fillellipse(x+60,y,10,12);
}
setcolor(RED);
line(x+14,y,x+46,y);
line(x-34,y,x-14,y);
line(x+74,y,x+92,y);
arc(x,y,360,180,14);
arc(x+60,y,360,180,14);
line(x-34,y,x-34,y-25);
line(x-34,y-25,x,y-25);
line(x,y-25,x,y-45);
line(x,y-45,x+60,y-45);
line(x+60,y-45,x+60,y-25);
line(x+60,y-25,x+100,y-25);
line(x+100,y-25,x+92,y);
setfillstyle(SOLID_FILL,RED);
floodfill(x+30,y-25,RED);
}
void erase(int x,int y)
{
setfillstyle(SOLID_FILL,BLACK);
bar(x-35,y-45,x+102,y+12);
bar(0,getmaxy()/2-20,getmaxx(),getmaxy()/2+20);
}

int main()
{
int gd=DETECT,gm,x,y,j,i=0,x1,y1,y2,y3,free=1,time,level=0,sublevel=0,lives=3;
char ch;
randomize();
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=40;
y=getmaxy()/2;
x1=getmaxx()-40;
y1=y-65;
y2=y;
y3=y+65;
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
for(j=0;j<15;j+=2)
{
rectangle(j*40,getmaxy()/2-20,50+j*40,getmaxy()/2-10);
floodfill(j*40+2,getmaxy()/2-20+2,WHITE);
}
setcolor(GREEN);
line(0,getmaxy()/2-125,getmaxx(),getmaxy()/2-125);
line(0,getmaxy()/2+90,getmaxx(),getmaxy()/2+90);
setfillstyle(SOLID_FILL,GREEN);
floodfill(1,1,GREEN);
floodfill(getmaxx(),getmaxy(),GREEN);
gotoxy(1,1);
cout<<"UP/DOWN key to move the car";
gotoxy(1,2);
cout<<"RIGHT key to use nitrous   ";
gotoxy(1,3);
cout<<"p to pause the game        ";
gotoxy(70,23);
cout<<"score:   ";
gotoxy(36,25);
cout<<"level:   ";
gotoxy(4,23);
cout<<"lives:   ";
gotoxy(36,27);
cout<<"Speed:   ";


while(1)
{
      //cleardevice();
gotoxy(77,23);
cout<<(sublevel*10)+(level*70);
gotoxy(43,27);
cout<<(level+1)*40;
gotoxy(43,25);
cout<<level;
gotoxy(11,23);
cout<<lives;
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
for(j=0;j<20;j+=2)
{
rectangle(j*40-(level+1)*10*i,getmaxy()/2-20,50+j*40-(level+1)*10*i,getmaxy()/2-10);
floodfill(j*40+10-(level+1)*10*i,getmaxy()/2-20+2,WHITE);
}
if(i==8)
{
i=0;
}


time=100-(level*20);
i++;


if(x1<=10)
{
free=random(3)+1;
x1=getmaxx()-40;
sublevel++;
if(sublevel==7)
{
sublevel=0;
level++;
if(level==6)
{
cleardevice();
gotoxy(38,16);
cout<<"YOU WON";
gotoxy(34,20);
cout<<"Your Score: "<<(sublevel*10)+(level*70);
getch();
closegraph();
return 0;
}
gotoxy(36,16);
cout<<"NEXT LEVEL";
delay(1500);
gotoxy(36,16);
cout<<"          ";
}
}
if((x+100)>=(x1-35))
{
if( (free==1&&y!=(getmaxy()/2-65)) || (free==2&&y!=(getmaxy()/2)) || (free==3&&y!=(getmaxy()/2+65)) )
{
if(lives==0)
{
cleardevice();
gotoxy(36,16);
cout<<"GAME OVER";
gotoxy(34,20);
cout<<"Your Score: "<<(sublevel*10)+(level*70);
getch();
closegraph();
return 0;
}
lives--;
gotoxy(34,16);
cout<<"You Lost a Life";
sublevel=0;
delay(1500);
gotoxy(34,16);
cout<<"               ";
x1=getmaxx()-40;
}
}


if(kbhit())
{
ch=getch();
switch (ch)
{

case 80://down
if(y<(getmaxy()/2)+60)
{
y=y+65;
}
break;

case 72://up
if(y>(getmaxy()/2)-60)
{
y=y-65;
}
break;

case 77://nitrous
x1=x1-60;
setfillstyle(SOLID_FILL,YELLOW);
bar(0,y-5,130,y+5);
break;

case 112:
gotoxy(36,16);
cout<<"Game Paused";
while(!kbhit())
{}
gotoxy(36,16);
cout<<"            ";

break;

case char(27):
gotoxy(35,16);
cout<<"Come again";
getch();
closegraph();
return 0;

}//end of switch
}//end of if
if(x!=40)
x=40;
else
x=x+1;
x1=x1-20;
draw(x,y);
if(free!=1)
draw(x1,y1);
if(free!=2)
draw(x1,y2);
if(free!=3)
draw(x1,y3);

delay(time);
erase(x,y);
if(free!=1)
erase(x1,y1);
if(free!=2)
erase(x1,y2);
if(free!=3)
erase(x1,y3);

}//end of while
}//end of main


Comments

Popular Posts