Here is a C graphics program for moving car animation using graphics.h header file. In this program, we will first draw a car and color it. In every iteration of for loop we keep on increasing the x coordinates of every point of car to make it look like this car is moving from left to right.
Graphics programming in C can be both fun and educational. In this tutorial, we'll explore how to develop a simple moving car animation using Turbo C graphics. Turbo C graphics functions provide a way to draw and manipulate graphics on the screen, and with some creativity, we can bring our animations to life.
We will use below mentioned graphics functions in this program.
| Function | Description |
|---|---|
| initgraph | It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. |
| getmaxx | It returns the maximum X coordinate in current graphics mode and driver. |
| getmaxy | It returns the maximum X coordinate in current graphics mode and driver. |
| setcolor | It changes the current drawing colour. Default colour is white. Each color is assigned a number, like BLACK is 0 and RED is 4. Here we are using colour constants defined inside graphics.h header file. |
| setfillstyle | It sets the current fill pattern and fill color. |
| circle | It draws a circle with radius r and centre at (x, y). |
| line | It draws a straight line between two points on screen. |
| arc | It draws a circular arc from start angle till end angle. |
| floodfill | It is used to fill a closed area with current fill pattern and fill color. It takes any point inside closed area and color of the boundary as input. |
| cleardevice | It clears the screen, and sets current position to (0, 0). |
| delay | It is used to suspend execution of a program for a M milliseconds. |
| closegraph | It unloads the graphics drivers and sets the screen back to text mode. |
Setting Up Turbo C Environment
Before we dive into the animation, make sure you have Turbo C installed and configured. Turbo C was popular in the MS-DOS era, so consider using an emulator or an environment that supports DOS applications.
Understanding Turbo C Graphics Basics
Turbo C graphics functions provide a set of tools for drawing shapes, lines, and colors on the screen. The graphics.h header file and functions like initgraph(), line(), circle(), and getch() are essential for our animation.
Turbo C provides functions for drawing lines, circles, rectangles, and other shapes. Here's an example:
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Draw a line
line(100, 100, 200, 200);
// Draw a rectangle
rectangle(250, 150, 350, 250);
// Draw a circle
circle(500, 200, 50);
getch();
closegraph();
return 0;
}
C program for moving car graphics animation
In this program, we first draw a red color car on left side of the screen (x,y) and then erases it using cleardevice function. We again draw this car at(x + 5, y). This will look like a moving car from left to right direction. We will repeat above steps until car reaches the right side of screen.
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
int main() {
int gd = DETECT, gm;
int i, maxx, midy;
/* initialize graphic mode */
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* maximum pixel in horizontal axis */
maxx = getmaxx();
/* mid pixel in vertical axis */
midy = getmaxy()/2;
for (i=0; i < maxx-150; i=i+5) {
/* clears screen */
cleardevice();
/* draw a white road */
setcolor(WHITE);
line(0, midy + 37, maxx, midy + 37);
/* Draw Car */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);
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, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
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, 120 + i, midy + 23);
line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy - 15);
line(57 + i, midy - 15, 57 + i, midy);
line(57 + i, midy, 28 + i, midy);
line(62 + i, midy - 15, 77 + i, midy - 15);
line(77 + i, midy - 15, 92 + i, midy);
line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
/* Draw Wheels */
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
/* Add delay of 0.1 milli seconds */
delay(100);
}
closegraph();
return 0;
}
Output
Here is the screenshot of moving car.
Conclusion
Congratulations! You've successfully created a simple moving car animation using Turbo C graphics. This tutorial covers the basics, but graphics programming offers endless possibilities for creativity. Experiment with different shapes, colors, and movements to enhance your animation further. Happy coding!
Related Topics