3D balls using c and graphics

Hey guys, here is the simple graphics program. I have created three balls and given them random motion. When the balls hit the walls it is bounced back. Here is the screen shot of the output. Hope you guys like it.


/*
    Programmer: Ashok Kumar Shrestha (ak007)
    Program Details: 3d balls moving randaomly inside the
             rectanglular box
    Language: C and graphics
*/


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()        //3 moving balls
{

    int gd = DETECT, gm;
    initgraph(&gd,&gm,"..\\BGI");
    int r=20;
    int x1,y1,x2=150,y2=80, x3=500,y3=400;
    int x=x3-25,y=y3-25;
    int dx,dy;
    int a[2],b[2],a1[2],b1[2],dx1[2],dy1[2];
    a[1]=(x3+x2)/2,b[1]=(y3+y2)/2,dx1[1]=3,dy1[1]=4;
    a[0]=x2+(x3-x2)/3,b[0]=y2+40,dx1[0]=3,dy1[0]=4;

    dx=3;
    dy=4;
    rectangle(x2-2,y2-3,x3+5,y3+5);
    delay(100);
    while(!kbhit())    //while any key is not hit its true
    {
        x1=x+dx;
        y1=y-dy;

        a1[1]=a[1]-dx1[1];
        b1[1]=b[1]+dy1[1];

        a1[0]=a[0]+dx1[0];
        b1[0]=b[0]+dy1[0];

        setcolor(2);
        setfillstyle(1,2);
        circle(x,y,r);
        floodfill(x,y,2);
        setcolor(15);
        setfillstyle(1,15);
        circle(x+8,y-6,5);
        floodfill(x+8,y-6,15);

        setcolor(1);
        setfillstyle(1,1);
        circle(a[1],b[1],r);
        floodfill(a[1],b[1],1);
        setcolor(15);
        setfillstyle(1,15);
        circle(a[1]+8,b[1]-6,5);
        floodfill(a[1]+8,b[1]-6,15);

        setcolor(4);
        setfillstyle(1,4);
        circle(a[0],b[0],r);
        floodfill(a[0],b[0],4);
        setcolor(15);
        setfillstyle(1,15);
        circle(a[0]+8,b[0]-6,5);
        floodfill(a[0]+8,b[0]-6,15);

        if(x1<x2+r+1||x1>x3-r)
            dx=-dx;
        if(y1<y2+r+1||y1>y3-r)
            dy=-dy;

        if(a1[1]<x2+r+1||a1[1]>x3-r)
            dx1[1]=-dx1[1];
        if(b1[1]<y2+r+1||b1[1]>y3-r)
            dy1[1]=-dy1[1];

        if(a1[0]<x2+r+1||a1[0]>x3-r)
            dx1[0]=-dx1[0];
        if(b1[0]<y2+r+1||b1[0]>y3-r)
            dy1[0]=-dy1[0];
        delay(10);
        delay(15);
           //    delay(20);

        setcolor(0);
        setfillstyle(1,0);
        circle(x,y,r);
        floodfill(x,y,0);

        circle(a[1],b[1],r);
        floodfill(a[1],b[1],0);

        circle(a[0],b[0],r);
        floodfill(a[0],b[0],0);

        x=x1;    y=y1;
        a[1]=a1[1];    b[1]=b1[1];
        a[0]=a1[0];    b[0]=b1[0];
    }

    getch();
}

//End of the program


Comments

Popular posts from this blog

mTranslator

Circular Pattern Using C/C++ and Graphics

Sorting Hat: Privacy Policy