C Program for Bouncing Ball Animation Using C Graphics

Here is a C graphics program for bouncing ball animation using graphics.h header file. In this program, we will draw a red color ball move it vertically up and down like a bouncing ball. We will use below mentioned 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.
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).
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).
kbhit It is used to determine whether a key is pressed or not. It returns a non-zero value if a key is pressed otherwise zero.
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.

C program for bouncing ball graphics animation

In this program, we first draw a red color ball on screen having center at (x, y) and then erases it using cleardevice function. We again draw this ball at center (x, y + 5), or (x, y - 5) depending upon whether ball is moving down or up. This will look like a bouncing ball. We will repeat above steps until user press any key on keyboard.

#include <stdio.h>
#include <graphics.h>
#include <dos.h>

int main() {
 int gd = DETECT, gm;
 int i, x, y, flag=0;
 initgraph(&gd, &gm, "C:\\TC\\BGI");

 /* get mid positions in x and y-axis */
 x = getmaxx()/2;
 y = 30;


 while (!kbhit()) {
  if(y >= getmaxy()-30 || y <= 30)
     flag = !flag;
     /* draws the gray board */
     setcolor(RED);
     setfillstyle(SOLID_FILL, RED);
     circle(x, y, 30);
     floodfill(x, y, RED);

 /* delay for 50 milli seconds */
 delay(50);

 /* clears screen */
 cleardevice();
 if(flag){
     y = y + 5;
 } else {
     y = y - 5;
 }
    }

    closegraph();
    return 0;
}
Output Here is the screenshot of bouncing ball. C graphics program for moving ball animation

Conclusion

This tutorial has guided you through the exhilarating process of creating a bouncing ball animation using Turbo C graphics. By exploring fundamental graphics functions and animation principles, you've gained insights into the dynamic world of visual programming.

As you witnessed, graphics programming allows for creative expression and the development of visually appealing applications. The animation of a bouncing ball serves as a starting point for more intricate and engaging projects. Consider experimenting with additional elements, such as multiple bouncing balls, diverse trajectories, or even integrating user interactions.

Understanding the mechanics of animation, including redraw loops, variable control, and collision detection, is crucial for advancing your graphics programming skills. Turbo C, although a classic platform, provides a solid environment for honing these skills, and the principles you've learned can be applied to more modern graphics libraries and frameworks.

As you continue your journey into graphics programming, explore advanced concepts like sprite animations, user-driven interactions, and real-time rendering. Additionally, consider transitioning to contemporary programming environments and languages to harness the latest graphical capabilities and industry trends.

Remember, the beauty of graphics programming lies in its limitless possibilities. Whether you're crafting entertaining games, dynamic simulations, or interactive user interfaces, the skills acquired from this tutorial lay the groundwork for your exploration of the captivating world of visual programming. Embrace your newfound knowledge, and let your creativity flourish in the realm of animated graphics!


Related Topics
C graphics program for moving car animation
C program to draw pie chart using graphics
C program to draw sine wave graph on screen
C program to draw 3D bar graph on screen
C graphics program to draw digital clock
C Program to draw concentric circles screen
C Program to draw a hut using graphics
List of all C Graphics Programs