How to Create Animation using line() Function in C Language

animation using the line() function in C or C++ programming language

Introduction :

Creating animations is an interesting work in programming world. Using C or C++ graphics programming language, you can create different types of animations. You can use line() function of graphics.h library to develop an animation program.

In this article, I shall show you how to create a simple animation using the line() function in C or C++ programming language. Here, I use the line() function of the graphics.h library in Turbo C++ IDE for this purpose.

About the program :

This is a simple program of drawing and erasing lines in different colors which create an animation effect on the screen. When you run the program, you see the lines are drawn and create rectangles in different sizes. After that, they will be erased. Then, further rectangles will be created using lines with different color. If you press any key, the program will be stopped.

Explanation of the program :

At the beginning of the program, you have to include the conio.h, graphics.h and dos.h libraries. Then, declare integer variables such as “xmax”, “ymax”, “i”, “j”, “x”, “y”, “xx”, “yy”, “x1”, “x2”, “y1”, “y2”, “xx1”, “xx2”, “yy1”, “yy2”, “graphic_driver” and “graphic_mode” in the main() function. Now, you can use initgraph() function to initialize the graphics mode.

Using getmaxx() and getmaxy() functions, you can get the maximum x and y coordinates of the graphics screen and store in “xmax” and “ymax” variables. In while loop, you have to use kbhit() function to check any key is pressed or not. Now, set the color for the lines using setcolor() function. You can draw lines using the line() function. At last, you can close the graphics mode using closegraph() function.

How run the program :

Now, install the Turbo C++ IDE and open it. Then, create a C or C++ file and paste the below code in it. You can follow the link to copy paste in Turbo C++ IDE. Do you know how can you use graphics.h in Turbo C++ IDE?

Source code of the program :

The following code is the source code of animation program in C or C++ programming language.

Output of the program :

When you run the program on your PC, you can see the output of the animation program like below image.

output of the animation program in C or C++ programming language

Conclusion :

At last, you have learned how to build animation program in C or C++ programming language. Now, you can create more complex animations using my code. Thank you for visiting my site.

Scroll to Top