How can I make a multiple bouncing balls program in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackwld
    New Member
    • Jan 2018
    • 2

    How can I make a multiple bouncing balls program in C?

    Hello,

    I am trying to make a program in C with multiple bouncing balls. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library.

    Thank you in advamce.

    Here is my code:

    #include <stdio.h>
    #include <graphics.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>

    int main()
    {
    FILE *input;
    int n, *num, i, total, j;
    if((input=fopen ("data.txt", "r"))!=NULL )
    {
    fscanf(input, "%d", &n);
    total=n*7;
    num=(int *) malloc(total*si zeof(int));
    if(num!=NULL)
    {
    for(i=0; i<total; i++)
    {
    fscanf(input, "%d", &num[i]);
    }
    fclose(input);

    clock_t start,finish, previous;
    double step;
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdri ver, &gmode, "");
    errorcode = graphresult();
    if (errorcode != grOk)
    {
    printf("Graphic s error: %s\n", grapherrormsg(e rrorcode));
    system ("pause");
    exit(1);
    }
    start=clock();
    previous=start;
    do
    {
    j=0;
    for(i=0; i<=n; i++)
    {
    finish = clock();
    step = (finish-previous)*1.0/CLOCKS_PER_SEC;
    if (step >= 0.03)
    {
    previous = finish;
    setfillstyle(SO LID_FILL,BLACK) ;
    setcolor(BLACK) ;
    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
    num[j]+= num[j+5]*step;
    num[j+1]+= num[j+6]*step;
    if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
    {
    num[j+5] *= -1;
    }
    if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
    {
    num[j+6] *= -1;
    }
    setfillstyle(SO LID_FILL,RED);
    setcolor(RED);
    fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
    j=j+7;
    }
    }
    }
    while (!kbhit());
    closegraph();
    }
    else
    {
    printf("Could not allocate memory\n\n");
    }
    }
    else
    {
    printf("Could not open file\n\n");
    }


    system("pause") ;
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Debugging is a necessary part of coding.

    In this case I would execute the program step-by-step using your debugger. This will let you see ofter each line of code what the contents of your variables is.

    If you have not used your debugger, this is an excellent opportunity to learn how.

    Let me know what happens.

    Comment

    • jackwld
      New Member
      • Jan 2018
      • 2

      #3
      Well, I used it and it showed me this:

      MEGA provides free cloud storage with convenient and powerful always-on privacy. Claim your free 20GB now

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I am not sure what that's about. MEGA is a New Zealand based cloud service. I have no idea why it's part of a debugger service.

        What compiler are you using?

        Comment

        Working...