Implement Bouncing Ball Animation Using C Graphics


Implement Bouncing Ball Animation Using C Graphics



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

  void main()

 {

        /* request auto detection */

        int gdriver = DETECT, gmode, err;

        int i = 0, x = 0, y = 0, flag = 0;



        /* initialize graphic mode */

        initgraph(&gdriver, &gmode, "C:/TC/BGI");



        /* ball movement */

        while (x <= getmaxx() && !kbhit()) {



                /* set the current drawing color */

                setcolor(LIGHTRED);

                /* fill the ball with given pattern & color */

                setfillstyle(SOLID_FILL, LIGHTRED);



                /* ball with light red color */

                pieslice(x, y, 0, 360, 12);



                /* ball movement towards x-axis */

                if (i % 5 == 0) {

                        /* left to right */

                        x = x + 3;

                        i = 0;

                }





                /* ball movement towards y-axis */

                if (flag) {

                        /* bottom to top */

                        y = y - 10;

                } else {

                        /* top to bottom */

                        y = y + 10;

                }



                /* check whether ball reached y-axis maximum */

                if (y >= getmaxy()) {

                        flag = 1;

                } else if (y <= 0) {

                        flag = 0;

                }



                /* sleeps for 50 milliseconds */

                delay(50);

                /* clear the graphic screen */

                cleardevice();

                i++;

        }



        getch();



        /* deallocate memory allocated for graphic system */

        closegraph();

  }




OUTPUT:-


Comments

Popular posts from this blog

Walking Stickman -Using C graphics

Sine Wave - Using C Graphics