Sunday 1 July 2012

To Show Movement of Ball using c++



#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
#include <stdlib.h>
#define ESC_KEY     0x1b

int tx=0,ty=0;
void getx(int &ox)
{
    if (ox==50)
    {
        tx=0;
        ox=ox+1;
    }
    else if (ox==getmaxx()-50)
    {
        tx=-1;
        ox=ox-1;
    }
    else if(tx==0)
        ox=ox+1;
    else  if(tx==-1)
        ox=ox-1;
}

void gety(int &oy)
{
    if (oy==50)
    {
        ty=0;
        oy=oy+1;
    }
    else if (oy==getmaxy()-50)
    {
        ty=-1;
        oy=oy-1;
    }
    else if(ty==0)
        oy=oy+1;
    else if(ty==-1)
        oy=oy-1;
}

void main()
{
int i,j;
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "..\\bgi");
int x,y;
x=100;
y=100;

a:
cleardevice();
settextstyle(0, HORIZ_DIR, 1);
outtextxy((getmaxx()/2)-20,getmaxy()-10,"PRESS ESC FOR EXIT");
getx(x);
gety(y);
settextstyle(0, HORIZ_DIR, 1);
circle(x,y,50);
outtextxy(x-20,y,"");
while(!kbhit())
{

delay(15);
goto a;
}
if (getch()==ESC_KEY)
{
exit(1);
}
else
goto a;

}




No comments:

Post a Comment