Simulate a car in C

/*
* Control keys are:
* f-> forward
* b-> backward
* 0-> brake
* 1,2,3,4-> gears
*/


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gdriver=DETECT,gmode;
int xc=0,yc=285,r=15,a=0,i,x1,y1,x2,y2,mode=1,dtime=75;
int x3,x4,y3,y4;
int kb='f';
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
while(1)
{

if(kbhit())
{
kb=getch();
if(kb=='q')
{
cleardevice();
printf("\n You are about to quit...press any key...");
getch();
exit(0);
}
if(kb=='b')
{
mode=0;
}
if(kb=='f')
{
mode=1;
}
if(kb=='1')
{
dtime=80;
}
if(kb=='2')
{
dtime=60;
}
if(kb=='3')
{
dtime=40;
}
if(kb=='4')
{
dtime=20;
}
if(kb=='0')
{
mode=2;
continue;
}
}

if(mode!=2)
{
cleardevice();
}

line(0,300,640,300);

if(mode==0) //reverse
{
setcolor(9);

/*wheels*/
circle(xc,yc,r);
circle(xc+100,yc,r);
/*body*/
line(xc-35,yc,xc-15,yc);
line(xc+15,yc,xc+85,yc);
line(xc+115,yc,xc+135,yc);
line(xc-35,yc,xc-35,yc-25);
line(xc+135,yc,xc+135,yc-25);

line(xc-35,yc-25,xc,yc-30);
line(xc+135,yc-25,xc+100,yc-30);

line(xc,yc-30,xc+15,yc-50);
line(xc+100,yc-30,xc+85,yc-50);

line(xc+15,yc-50,xc+85,yc-50);
/*glasses*/
line(xc+6,yc-30,xc+18,yc-47);
line(xc+18,yc-47,xc+18,yc-30);
line(xc+6,yc-30,xc+18,yc-30);

line(xc+94,yc-30,xc+82,yc-47);
line(xc+82,yc-47,xc+82,yc-30);
line(xc+94,yc-30,xc+82,yc-30);

line(xc+21,yc-47,xc+21,yc-30);
line(xc+21,yc-47,xc+48,yc-47);
line(xc+21,yc-30,xc+48,yc-30);
line(xc+48,yc-30,xc+48,yc-47);


line(xc+79,yc-47,xc+79,yc-30);
line(xc+79,yc-47,xc+52,yc-47);
line(xc+79,yc-30,xc+52,yc-30);
line(xc+52,yc-30,xc+52,yc-47);

for(i=0;i<4;i++)
{
x1=xc+(r*cos(a));
y1=yc+(r*sin(a));
x2=xc-(r*cos(a));
y2=yc-(r*sin(a));
x3=(xc+100)+(r*cos(a));
y3=yc+(r*sin(a));
x4=(xc+100)-(r*cos(a));
y4=yc-(r*sin(a));
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
a+=90;

}
xc-=1;
delay(dtime);
}

/*forward*/
if(mode==1)
{
setcolor(9);
/*wheels*/
circle(xc,yc,r);
circle(xc+100,yc,r);
/*body*/
line(xc-35,yc,xc-15,yc);
line(xc+15,yc,xc+85,yc);
line(xc+115,yc,xc+135,yc);
line(xc-35,yc,xc-35,yc-25);
line(xc+135,yc,xc+135,yc-25);

line(xc-35,yc-25,xc,yc-30);
line(xc+135,yc-25,xc+100,yc-30);

line(xc,yc-30,xc+15,yc-50);
line(xc+100,yc-30,xc+85,yc-50);

line(xc+15,yc-50,xc+85,yc-50);
/*glasses*/
line(xc+6,yc-30,xc+18,yc-47);
line(xc+18,yc-47,xc+18,yc-30);
line(xc+6,yc-30,xc+18,yc-30);

line(xc+94,yc-30,xc+82,yc-47);
line(xc+82,yc-47,xc+82,yc-30);
line(xc+94,yc-30,xc+82,yc-30);

line(xc+21,yc-47,xc+21,yc-30);
line(xc+21,yc-47,xc+48,yc-47);
line(xc+21,yc-30,xc+48,yc-30);
line(xc+48,yc-30,xc+48,yc-47);


line(xc+79,yc-47,xc+79,yc-30);
line(xc+79,yc-47,xc+52,yc-47);
line(xc+79,yc-30,xc+52,yc-30);
line(xc+52,yc-30,xc+52,yc-47);

for(i=0;i<4;i++)
{
x1=xc+(r*cos(-a));
y1=yc+(r*sin(-a));
x2=xc-(r*cos(-a));
y2=yc-(r*sin(-a));
x3=(xc+100)+(r*cos(-a));
y3=yc+(r*sin(-a));
x4=(xc+100)-(r*cos(-a));
y4=yc-(r*sin(-a));
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
a+=90;
}
xc+=1;
delay(dtime);

}

}

}

Post a Comment

0 Comments