Car Race
//*************************************************************************////*************************************************************************//
// -*- "Program For Game of Car Racing." -*- //
//-------------------------------------------------------------------------//
// -*- Language :- C-Graphics & C++. -*- //
//-------------------------------------------------------------------------//
// -*- Compilier :- Turbo C & C++. -*- //
//-------------------------------------------------------------------------//
// -*- By- Vikrantsingh Bisen. -*- //
//*************************************************************************//
//*************************************************************************//
//*************************************************//
// -*- Including Header File -*- //
//*************************************************//
#include <iostream.h>
#include <graphics.h>
#include <process.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <dos.h>
//*************************************************//
// -*- Class For Attractive look -*- //
//*************************************************//
class Display
{
private:
int i,j,k,l,a;
public:
void border();
void name();
void draw_car(int,int=300,int=11);
void erase_car(int,int=300,int=0);
void draw_track(int);
void erase_track(int);
void obstacle(int,int);
};
/*---------- -*- Function to Display Name of Programer -*- --------------*/
void Display::name()
{
cleardevice();
setbkcolor(BLACK);
setcolor(RED);
for(i=0;i<500;i++)
{
line(0,i,700,i);
delay(10);
}
setbkcolor(RED);
cleardevice(); int k,l;
rectangle(220,115,520,250);
setcolor(WHITE);
rectangle(208,127,418,178);rectangle(206,125,420,180);
setcolor(YELLOW);
settextstyle(4, HORIZ_DIR,6);
outtextxy(225,117,"Car.Race");
for(i=400,l=0,k=638,j=0;l<120;j++,k--,l++)
{ if(i>320)i--;
if(j>12)j=0;
//if(i!=320)
setcolor(YELLOW);
settextstyle(1, HORIZ_DIR,3);
outtextxy(220,i-80," Application of");
outtextxy(200,i-40,"C-Graphics & C++");
outtextxy(220,i," Designed By"); //Name of Designer.
settextstyle(1, HORIZ_DIR,4);
outtextxy(160,i+30,"Vikrantsingh Bisen");
//delay(5);
if(i>321){
setcolor(BLACK);
settextstyle(1, HORIZ_DIR,3);
outtextxy(220,i-80," Application of");
outtextxy(200,i-40,"C-Graphics & C++");
outtextxy(220,i," Designed By");
settextstyle(1, HORIZ_DIR,4);
outtextxy(160,i+30,"Vikrantsingh Bisen");
}
setcolor(CYAN);
line(l,0,l,500);
line(k,0,k,500);
}
setcolor(DARKGRAY);
for(i=0,j=0, k=638,l=490;i<320;i+=5,j+=5,l-=5,k-=5)
{
line(i,0,i,490);
line(k,0,k,490);
delay(50);
}
setcolor(BLACK);
for(i=320,j=250, k=318,l=250;i>0;i-=5,j-=5,l+=5,k+=5)
{
line(i,0,i,490);
line(k,0,k,490);
delay(50);
}
delay(200);
cleardevice();
setbkcolor(BLACK);
}
/*------------- -*- Function to Display Boreder -*- --------------*/
void Display::border()
{
setcolor(BROWN);
rectangle(1,1,638,478);
int poly[8];
poly[0]=2;poly[1]=2;poly[2]=100;poly[3]=2;
poly[4]=100;poly[5]=477;poly[6]=2;poly[7]=477;
//rectangle(2,2,100,477);
//rectangle(537,2,637,477);
setfillstyle(6,7);
fillpoly(4,poly);
poly[0]=537;poly[1]=2;poly[2]=637;poly[3]=2;
poly[4]=637;poly[5]=477;poly[6]=537;poly[7]=477;
setfillstyle(6,7);
fillpoly(4,poly);
setcolor(LIGHTGREEN);
settextstyle(7,HORIZ_DIR,2);
for(i=0;i<500;i+=15){outtextxy(105,i,"*");outtextxy(525,i,"*");}
}
/*------------- -*- Function to Draw Track -*- --------------------*/
void Display::draw_track(int p)
{
i=0;j=70; k=-100; //car (160);car (260,50);//car (360);car (460);
l=0;
setcolor(WHITE);
for(j=p;j<500;j+=100) // 5 - vert
{ l=0;
for(i=119;i<=519;i+=100) // 4 - horz
{
if(l==0)
{
setcolor(YELLOW);
l=1;
}
else
{ setcolor(WHITE);
l=0;
}
line(i,j,i,j+70);
}
}
}
/*------------- -*- Function to Erase Track -*- --------------------*/
void Display::erase_track(int p)
{
// cleardevice();
delay(1);
setcolor(BLACK);
for(j=p;j<500;j+=100) // 5 - vert
{
for(i=119;i<=519;i+=100) // 4 - horz
{
line(i,j,i,j+70);
}
}
}
/*------------- -*- Function to Draw Car -*- --------------------*/
void Display::draw_car(int m,int n,int p)
{
setcolor(p);
int poly[10];
poly[0]=m;
poly[1]=n;
poly[2]=m+10;
poly[3]=n;
poly[4]=m+20;
poly[5]=n+100;
poly[6]=m-10;
poly[7]=n+100;
// poly[8]=poly[0];
// poly[9]=poly[1];
setfillstyle(2,p-8);
fillpoly(4,poly);
//Tyres
rectangle(m-10,n+15,m-5,n+25); //LT
rectangle(m+15,n+15,m+20,n+25); //RT
rectangle(m-15,n+75,m-10,n+85); //LB
rectangle(m+20,n+75,m+25,n+85); //RB
}
/*------------- -*- Function to Erase Car -*- --------------------*/
void Display::erase_car(int m,int n,int p)
{
p=0;
setcolor(p);
for(i=n;i<n+120;i++)
line(m-20,i,m+25,i);
}
/*------------- -*- Function to Draw obstacle -*- --------------------*/
void Display::obstacle(int xc,int yc)
{
randomize();
int r=random(12);
if(r==0||r==7||r==8)r=12;
erase_car(xc,yc-20,0);
draw_car(xc,yc,r);
}
//*************************************************//
// -*- Class To Puttext on Screen -*- //
//*************************************************//
class Text
{
private:
int i;
public:
void puttext();
void showtime();
void show_info(int,int,char[],int,int,int);
void accedent();
void winner();
void losser();
};
void Text::puttext()
{
settextstyle(7,HORIZ_DIR,2);
setcolor(YELLOW);
outtextxy(4,80," DATE");
outtextxy(4,160," TIME");
outtextxy(4,240," SPEED");
outtextxy(4,320," KM");
outtextxy(530,80," NAME");
outtextxy(530,160," LIFE");
outtextxy(530,240," TARGET");
outtextxy(530,320," SCORE");
}
void Text::show_info(int speed,int km,char name[],int life,int target,int score)
{
showtime();
gotoxy(2,19);
cout<<setw(8)<<speed;
gotoxy(2,24);
cout<<setw(8)<<km;
gotoxy(70,9);
cout<<name;
gotoxy(70,14);
cout<<setw(8)<<life;
gotoxy(70,19);
cout<<setw(8)<<target;
gotoxy(70,24);
cout<<setw(8)<<score;
}
void Text::showtime()
{
char date[9],time[9],i;
_strdate(date);
_strtime(time);
gotoxy(2,9);
cout<<date;
gotoxy(2,14);
cout<<time;
}
void Text::accedent()
{
setcolor(CYAN);
for(i=0;i<320;i+=5)
{
delay(20);
line(i,0,i,500);
line(638-i,0,638-i,500);
}
setcolor(MAGENTA);
settextstyle(0,HORIZ_DIR,3);
outtextxy(150,200," Alert !");
outtextxy(150,250," Life Loss ");
outtextxy(150,300," Press Enter ");
while(1)
{
char ch=getch();
if(ch==13||ch==27) return;
}
}
void Text::winner()
{
setcolor(CYAN);
for(i=0;i<320;i+=5)
{
delay(20);
line(i,0,i,500);
line(638-i,0,638-i,500);
}
cleardevice();
setbkcolor(CYAN);
setcolor(MAGENTA);
settextstyle(0,HORIZ_DIR,3);
outtextxy(130,200,"Congratulations!");
outtextxy(65,250,"You have Won the Game");
while(1)
{
char ch=getch();
if(ch==13||ch==27)exit(0);
}
}
void Text::losser()
{
setcolor(CYAN);
for(i=0;i<320;i+=5)
{
delay(20);
line(i,0,i,500);
line(638-i,0,638-i,500);
}
cleardevice();
setbkcolor(CYAN);
setcolor(MAGENTA);
settextstyle(0,HORIZ_DIR,3);
outtextxy(130,200," Sorry!");
outtextxy(130,250," Game Over");
while(1)
{
char ch=getch();
if(ch==13||ch==27)exit(0);
}
}
//*************************************************//
// -*- Main Function -*- //
//*************************************************//
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
Display x;
Text tt;
char choice,ch;
int i,j=0,lane=160,k=0,l,xc=0,yc=0,check=1,s=0;
int speed=0,km=0,life=5,target=10000,score=0;
char name[15];
x.name();
setbkcolor(RED);
setcolor(GREEN);
for(i=0,k=638;i<100;i++,k--)
{
line(i,0,i,490);
line(k,0,k,490);
delay(20);
}
setcolor(YELLOW);
settextstyle(0,HORIZ_DIR,3);
outtextxy(130,200,"Enter Your Name");
rectangle(249,249,351,281);rectangle(250,250,350,280);
gotoxy(34,17);
cin>>name;
setcolor(LIGHTCYAN);
for(i=0,k=638;i<320;i+=5,k-=5)
{
line(i,0,i,490);
line(k,0,k,490);
delay(50);
}
cleardevice();
k=0;
setbkcolor(DARKGRAY);
x.draw_car(lane);
x.draw_track(j);
x.border();
tt.puttext();
do
{
while(!kbhit()){tt.showtime();}
tt.show_info(speed,km,name,life,target,score);
km++,target--,score++ ;
ch=getch();
switch(ch)
{
case 72:
speed++;
x.erase_track(j);
j+=10;
if(j>40)j=0;
x.draw_track(j);
break;
case 77:
speed=0;
x.erase_track(j);
j+=10;
if(j>40)j=0;
x.draw_track(j);
x.erase_car(lane);
lane+=100; check++;
if(lane>460){lane=460 ;check=4;}
x.draw_car(lane);
break;
case 75:
speed=0;
x.erase_track(j);
j+=10;
if(j>40)j=0;
x.draw_track(j);
x.erase_car(lane);
lane-=100; check--;
if(lane<160){lane=160;check=1; }
x.draw_car(lane);
break;
case 80:
x.erase_track(j);
j-=10;
if(j<-40)j=0;
x.draw_track(j);
break;
case 27:
exit(0);
}
if(k==0)
{
randomize();
s=random(500);
s=s%4;
xc=160+100*s;
}
if(k<55)
{
//x.erase_car(xc,yc-20,0);
//x.draw_car(xc,yc,12);
x.obstacle(xc,yc);
yc+=10;
k++;
if(k==54){yc=0,k=0;}
if(xc==lane)
{
if( yc>=220&&yc<=410)
{
tt.accedent();
cleardevice();
k=0;yc=0;life--;
speed=0;
score-=100;
x.border();
tt.puttext();
x.draw_car(lane);
}
}
}
if(life==0)
tt.losser();
if(target==0)
tt.winner();
}while(life);
}
/*--------------------- -*- End of Program -*- ---------------------------*/
//************************************************************************//
//------------------------------------------------------------------------//
//************************************************************************//
// In This Progam we have used //
// - Two Clases //
// - 1] Display:- //
// Display All kinds of border //
// e.g.,draw and erase car & track, etc., //
// - 2] Text:- //
// Put all kinds of text //
// e.g.,date,time,speed,name,etc., //
//************************************************************************//
//------------------------------------------------------------------------//
//************************************************************************//
No comments:
Post a Comment