Search This Blog

Keyboard Controlled Car Animation in C - Computer Graphics Lab Program

This is a computer graphics lab program. This is an animation program in which a car can be moved using the arrow keys on keyboard. The car should move forward when right arrow key is pressed and backward when left arrow key is pressed. The car accelerates as the right arrow key is pressed. When it is released the car decelerates. The program uses the graphics library graphics.h. This program works well in the turbo c compiler with Borland graphics interface (BGI). The output of the program is as follows:

The program is as follows:


#include<stdio.h>
#include<math.h>
#include<graphics.h>
int gd=DETECT,gm;
int x,y,dir,deg=0;
float rad1,rad2;


void car(int x, int y,int dir)
{
int i;
int p1[8]={75,0,160,0,177,75,50,75};
int p2[8]={80,10,115,10,115,60,70,60};
int p3[8]={120,10,150,10,165,60,120,60};
int p4[8]={20,75,225,75,240,140,0,140};
int p5[8]={15,90,30,90,25,115,8,115};
int p6[8]={215,90,230,90,235,115,220,115};
int wavex=215,wavey=40,phase=0;
float rad;
wavex+=x;
for(i=0;i<8;i+=2)
{
p1[i]+=x;
p1[i+1]+=y;
p2[i]+=x;
p2[i+1]+=y;
p3[i]+=x;
p3[i+1]+=y;
p4[i]+=x;
p4[i+1]+=y;
p5[i]+=x;
p5[i+1]+=y;
p6[i]+=x;
p6[i+1]+=y;
}

setcolor(DARKGRAY);
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,p1);
fillpoly(4,p4);
setfillstyle(SOLID_FILL,LIGHTGRAY);
fillpoly(4,p2);
fillpoly(4,p3);
setfillstyle(SOLID_FILL,LIGHTGREEN);
fillpoly(4,p5);
fillpoly(4,p6);
setcolor(BROWN);
//tyres
setfillstyle(SOLID_FILL,DARKGRAY);
fillellipse(x+60,y+150,25,25);
fillellipse(x+185,y+150,25,25);
setfillstyle(SOLID_FILL,BROWN);
fillellipse(x+60,y+150,10,10);
fillellipse(x+185,y+150,10,10);
setcolor(YELLOW);
rad1=3.14f*deg/180;
rad2=3.14f*(deg+90)/180;
line(x+60,y+150,x+60+10*cos(rad1),y+150+10*sin(rad1));
line(x+60,y+150,x+60+10*cos(rad2),y+150+10*sin(rad2));
line(x+60,y+150,x+60-10*cos(rad1),y+150-10*sin(rad1));
line(x+60,y+150,x+60-10*cos(rad2),y+150-10*sin(rad2));
line(x+185,y+150,x+185+10*cos(rad1),y+150+10*sin(rad1));
line(x+185,y+150,x+185+10*cos(rad2),y+150+10*sin(rad2));
line(x+185,y+150,x+185-10*cos(rad1),y+150-10*sin(rad1));
line(x+185,y+150,x+185-10*cos(rad2),y+150-10*sin(rad2));
//flag
setcolor(LIGHTGRAY);
line(x+215,y+75,x+215,y+40);
setcolor(RED);
wavey=66;
phase=(phase+90)%360;
for(i=0;i<720;i+=10)
 {
 rad=(wavex+phase+i)*3.14f/180;
 putpixel(wavex-dir*(i)/30,wavey+3*sin(rad),RED);
 putpixel(wavex-dir*(i)/30,wavey+6+3*sin(rad),RED);
 putpixel(wavex-dir*(i)/30,wavey+12+3*sin(rad),RED);
 }
}

void main()
{
int c1=10,incr=0,xmax;
float despeed=0.5f,speed=3;;
initgraph(&gd,&gm,"");
cleardevice();
xmax=getmaxx();
dir=0;
x=20,y=30;
while(c1!='q')
{
if(kbhit())
 {
 c1=getch();
 if(c1==0)
  {
  c1=getch();
  if(c1==75)
   {
   x-=3;
   deg=(deg+dir*20)%360;
   if(speed>3)
    speed=3;
   dir=-1;
   }
  else if(c1==77)
   {
   x+=speed;
   deg=(deg+dir*20)%360;
   incr=(incr+1)%5;
   speed+=incr/4;
   dir=1;
   }
  }
 }
else if(speed>0)
 {
 x+=speed;
 deg=(deg+dir*20)%360;
 speed-=despeed;
 if(speed<0)
  speed=0;
 }
delay(60);
cleardevice();
if(x>=xmax)
 x=-235;
if(x<-235)
 x=xmax;
car(x,y,dir);
}

closegraph();
}



No comments: