Home / C / Bouncing Ball Program in C Graphics

Bouncing Ball Program in C Graphics

Here is the C Program for Bouncing ball animation in computer Graphics. Download the source code given here for the bouncing ball problem. The colored bouncing ball moves along x and y axis using a loop and decreasing and increasing the value of x and y in order to move the ball.

Bouncing Ball Program C

  •  

      #include <stdio.h>
      #include <graphics.h>
      #include <dos.h>
      #include <conio.h>
    
      int main() {
            /* to auto detect graphics drivers*/
            int gdriver = DETECT, gmode, g_error;
            int j = 0, x = 0, y = 0, temp = 0;
    
            /* initialize graphic mode */
            initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
            g_error = graphresult();
    
            if (g_error != grOk) {
                    /* error happened */
                    printf("Graphics error: %sn",
                                    grapherrormsg(g_error));
                    return 0;
            }
    
            /* bouncing ball movement */
            while (x <= max_x() && !kbhit()) {
    
                    /* which color to draw the movement */
                    setcolor(LIGHTRED);
                    /* coloring the bouncing ball with given */
                    setfillstyle(SOLID_FILL, LIGHTRED);
    
                    /* bounce ball color is red */
                    pieslice(x, y, 0, 360, 12);
    
                    /* Bouncing ball towards x-axis */
                    if (j % 5 == 0) {
                            /* left to right */
                            x = x + 3;
                            j = 0;
                    }
    
    
                    /* ball bouncing towards y-axis */
                    if (temp) {
                            /* bottom to top */
                            y = y - 10;
                    } else {
                            /* top to bottom */
                            y = y + 10;
                    }
    
                    /* To check if ball reached max y-axis*/
                    if (y >= max_y()) {
                            temp = 1;
                    } else if (y <= 0) {
                            temp = 0;
                    }
    
                    /* sleeps for 50 milliseconds for better graphics */
                    delay(50);
                    /* clear the graphic screen */
                    cleardevice();
                    j++;
            }
    
            getch();
    
            /* deallocating allocated memory for graphics */
            closegraph();
    
            return 0;
      }
    

     

  • This is another short approach for the Bouncing ball program. This program is made using for loop and ternary operator within it. Since the program is not difficult to understand there isn’t any explanation. Just go line by line to understand it. The ball movement along x and y axis can easily be understood.

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h> //  including the graphics libraries
    #include<dos.h> 
    void main() {
    /*auto detect the graphcis drivers*/
    	int gd = DETECT, gm = DETECT;
    	int x, y = 0, j, temp = 400, k = 1;
    	initgraph(&gd, &gm, ""); //initialize the graphics
    	setcolor(RED);
    	setfillstyle(SOLID_FILL, RED);
    	for (x = 40; x < 600; x++) {
    		cleardevice(); // to clear the screen previously used for graphics
    		circle(x, y, 30);
    		floodfill(x, y, RED); 
    		delay(40);
    		if (y >= 400) {
    			k = 0;
    			temp -= 20;
    		}
    		if (y <= (400 - t))
    			    k = 1;
    		y = y + (c ? 15 : -15);
    	}
    	getch();
    }

     

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.