This blog is under construction

Sunday 25 August 2013

Implement Bouncing Ball Animation Using C Graphics



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

  int 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:/TURBOC3/BGI");
        err = graphresult();

        if (err != grOk) {
                /* error happened */
                printf("Graphics Error: %s\n",
                                grapherrormsg(err));
                return 0;
        }

        /* 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();

        return 0;
  }





OUTPUT: (Bouncing Ball - C Graphics)


12 comments:

  1. the graphics header file has an error

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I really appreciate your efforts for this blog. This is so informative. Keep it up and keep sharing such posts.
    Sometimes it is difficult for students to manage their studies with job. In academic life they have to make lengthy assignments and assessments due to which they don’t get enough time to focus on studies. Therefore many students seek assignment help Melbourne. Many different subjects where students mostly seek online assignment help are chcece018 assessment answers Help, Management Assignment Help, bsbdiv501 assessment answers, etc.

    ReplyDelete
  9. Great! This is the way to help people i really like it Carry on, thanks to the admin for sharing this informative stuff with us. I like to visit here again for further beneficial posts.
    Checkout our post and give your precious suggestions!
    How to unlock pattern without data lose
    How cell phone repair business profitable

    ReplyDelete