C Program to Move a Car

In this article, we’ll be implementing moving car program in computer graphics. Using translation transformation, we can draw and move an object to another coordinate. The below code is a moving car program written in C.

C Program to move a car in computer graphics

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gm,gd=DETECT;
int i= 0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(i<=800)
{
line(0,300,800,300); // Path
//Body of the car
line(50+i,220,100+i,220);
line(50+i,220,30+i,250);
line(100+i,220,120+i,250);
rectangle(0+i,250,160+i,270);
// Tyres of the car
circle(30+i,285,12);
circle(130+i,285,12);
if(i>=800)
{
break;
}
i=i+2;
clearviewport(); // clearing image which would make illusion of moving car
}
getch();
closegraph();

}

Explanation: 

In the above code, we have drawn a car and a path on which it would be running. We used, a line method to create a path, 1 rectangle and 3 line methods to build the upper body of a car and two circle method for its tyres.

Using translation transformation, we were able to make an illusion of a moving car. And lastly, clearviewport() method used to clear a previously generated view.

Output:

movingcar

Leave a Reply