Moving Car Program in C
Here you will learn to create a moving car program in c using computer graphics. Here we will be using line, circle, and arc functions of graphics.h library to complete this program. This program can be run by using Turbo C++ and Dev C++ compiler. Here we will be using Turbo c++ compiler.
Car Animation in Computer Graphics
Animation is the moving effects in an image. This can be done using different methods like hand-drawn animation, stop-motion animation, computer animation, and clay animation.
Here, we are creating a c program for Moving Car Animation using computer graphics. We are making this car by using the various designing functions like line, circle, and arc. For animation we are using following:-
- for loop( reprinting car with new coordinates)
- delay function( for time delay)
- cleardevice function (to clear screen)
Program Code of Moving Car Program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | //Car Animation in Computer Graphics #include<stdio.h> #include<graphics.h> #include<conio.h> #include<dos.h> int main() { int gd = DETECT, gm; int i, maxx, midy; //initialize graphics mode initgraph(&gd, &gm, "C:\TURBOC3\BGI"); //get maximum pixel of horizontal axis maxx = getmaxx(); //get mid pixel of vertical axis midy = getmaxy()/2; for (i=0;i<maxx-250;i=i+5) { // clears screen cleardevice(); outtextxy(150,120, "Moving Car Animation in Graphics"); setcolor(RED); //Draw Road line(0, midy+36,maxx-100,midy+36); setcolor(WHITE); //Draw Car line(i, midy + 23, i, midy); line(i, midy, 40 + i, midy - 20); line(40 + i, midy - 20, 80 + i, midy - 20); line(80 + i, midy - 20, 100 + i, midy); line(100 + i, midy, 135 + i, midy); line(135 + i, midy, 135 + i, midy + 23); //120 line(0 + i, midy + 23, 18 + i, midy + 23); arc(30 + i, midy + 23, 0, 180, 12); line(42 + i, midy + 23, 78 + i, midy + 23); arc(90 + i, midy + 23, 0, 180, 12); line(102 + i, midy + 23, 135 + i, midy + 23); //120 circle(30 + i, midy + 25, 9); circle(90 + i, midy + 25, 9); //car speed delay(100); } getch(); closegraph(); return 0; } |
Output