Introduction :
Using C or C++ language, you can create simple animation programs. Developing a basic animated face is one of them. You can draw and animate facial expressions such as blinking eyes and a moving mouth using C or C++ graphics programming language. In this article, I shall show you how to create animated face in C or C++ graphics programming language. Here, I build the animated face in Turbo C++ IDE with the help of graphics.h library.
About the program :
This is a simple animated face program developed by C or C++ language. When you run the program, you see an animated face on the graphics screen. You can also see blinking eyes and moving mouth on the program. To stop the program, press any key.
Explanation of the program :
You have to include graphics.h, conio.h and dos.h in the program. In the main() function, declare integer variables such as “x”, “y”, “i”, “j”, “graphic_driver” and “graphic_mode”. Then, initialize the graphics mode using initgraph() function. After that, calculate the center of the screen using getmaxx() and getmaxy() functions and store in “x” and “y” variables. In the while loop, use kbhit() function to check any key press or not.
Using setbkcolor() function, set the background color of the graphics screen. To set the drawing color you can use setcolor() function. The setfillstyle() function is used to set the fill style of shapes. You can draw the face, nose, eyes and mouth using fillellipse() function. To control the animation speed, you have to use delay() function. Finally, use closegraph() function to close the graphics mode.
How run the program :
First, you have to install Turbo C++ IDE on your PC. Then, create a C or C++ file in Turbo C++ IDE. Now, copy and paste the following code in your C or C++ file. You have to know how to copy paste in the Turbo C++ IDE. Do you know how to use graphics.h in Turbo C++ IDE?
Source code of the program :
The following code is the source code of animated face program in C or C++ programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int x, y, i = 0, j = 0, graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
x = getmaxx() / 2;
y = getmaxy() / 2;
setbkcolor(4);
setcolor(14);
setfillstyle(SOLID_FILL, 14);
fillellipse(x, y, 170, 200);
setfillstyle(SOLID_FILL, 10);
fillellipse(x, y, 30, 80);
while (!kbhit())
{
setfillstyle(SOLID_FILL, 0);
fillellipse(x - 70, y - 60, 40, 30 - j);
setfillstyle(SOLID_FILL, 0);
fillellipse(x + 70, y - 60, 40, 30 - j);
setfillstyle(SOLID_FILL, 15);
fillellipse(x, y + 110, 70, 20 - i);
delay(20);
if (i == 20)
{
i = 0;
}
i++;
if (j == 30)
{
j = 0;
}
j++;
}
closegraph();
return 0;
}
Output of the program :
When you run the program, you can see the output of the program on your PC. You can also see the output in my YouTube channel.
Conclusion :
At last, you have learned how to create animated face program in C or C++ programming language. You can use my code in your project. Thank you for visiting my site.