hello guys,
the following code is taken from my senior. i am unable to under stand its logic .
because i am new in graphics can u guys kindly explain its logics
thanks.

#include<graphics.h>
#include<conio.h>
#define left 0
#define top 0
#define  right 639
#define  bottom 479
#define  radius  20
void main ()
{
int gdriver = DETECT, gmode, errorcode;
int x,y,dx,dy,oldx,oldy;
unsigned char ballbuff[2000];
initgraph(&gdriver, &gmode, "");
rectangle(left, top, right,bottom);
x=left+radius+10;
y= top+radius +10;
setbkcolor(BROWN);
setcolor(LIGHTGREEN);
setfillstyle(SOLID_FILL,YELLOW);
circle(x,y,radius);
floodfill(x,y,LIGHTGREEN);
getimage(x-radius,y-radius,x+radius,y+radius,ballbuff);
dx=2;dy=2;
while(!kbhit())
{
putimage(x-radius,y-radius,ballbuff,COPY_PUT);
oldx=x;oldy=y;
x+=dx;y+=dy;
  if(x==left+radius+2||x>=right-radius-2)
    dx=-dx;
  if(y==top+radius+2||y>=bottom-radius-2)
    dy=-dy;
putimage(oldx-radius,oldy-radius-2,ballbuff,XOR_PUT);
delay(1);
}
getch();
closegraph();
}

did you compile and run the program to see what it does? Requires Turbo C compiler.

Member Avatar for GreenDay2001

I find this dos.h missing. Whatever the logic is quite simple.
First he makes a whole screen, with background borders and all(quite obious).
Then he creates a circle and puts it into memory, using getimage. Later it is put into screen using putimage.
A loope is created which increments x,y by dx, dy. When the circle touches any of the screen border(detected using if), the dy and dx are changed from 1 to -1. So every time x,y is incremented by -1 or you could say decremented([..x+(-1)=x-1 only].

And yes, you need to Turbo C++ or borland C++ and must inlude graphics library. If it doesn't compile post here.

thankz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.