#include
#include
#include
#include
#include
#include
#include
#define DEFDELAY 5
/* Global Variables */
// Symbolic Constants
const int xincr=1,yincr=1;
const int TRUE=1,FALSE=0;
const int FWD=0,BCK=1,UP=0,DWN=1;
const int LEFT=1,RIGHT=2,TOP=3,BOTTOM=4;
// Other Variables
int gd,gm,MAXX,MAXY;
int xstart,ystart,ballrad;
int xinst,yinst;
int xdir,ydir,cuttingbound;
int bounce=TRUE,linecol,fillcol;
/* Function Prototypes */
void initgraphics(void);
void testresolution(void);
void moveball(void);
int testboundary(void);
void rebound(void);
void drawball(void);
void showimpact(void);
void main()
{
testresolution();
printf("Please Enter XY Co-Ordinates of Starting Point : ");
scanf("%d%d",&xstart,&ystart);
printf("\n\nEnter the Size of Ball (Radius) : ");
scanf("%d",&ballrad);
printf("\n\n\nPress Any Key to Enter Graphics Mode ..... ");
getch();
moveball();
}
void initgraphics(void)
{
int error;
initgraph(&gd,&gm,"\\TC\\BGI");
error=graphresult();
if(error!=grOk)
{
printf("\n\n\aGRAPHICS ERROR! Error Code : %d",error);
getch();exit(1);
}
}
void testresolution(void)
{
clrscr();
printf("Testing System Resolution! Press Any Key to enter Graphics Mode ....");
getch();
initgraphics();
MAXX=getmaxx();
MAXY=getmaxy();
closegraph();
printf("System Resolution Detected!\n");
printf("\nHORIZONTAL PIXEL SPAN : %d (0 to %d)",MAXX+1,MAXX);
printf("\nVERTICAL PIXEL SPAN : %d (0 to %d)",MAXY+1,MAXY);
getch();
clrscr();
}
void moveball(void)
{
// Seeding Random Number Generator
randomize();
// Initializing the Graphics Mode
initgraphics();
// Initializing the Position of the Ball
xinst=xstart;
yinst=ystart;
// Iterating Till User Hits a Key
xdir=random(2);
ydir=random(2);
while(!kbhit())
{
if(testboundary())
{
// Drawing the Ball
cleardevice();
drawball();
delay(DEFDELAY);
// Making Changes
if(xdir==FWD)
xinst+=xincr;
else
xinst-=xincr;
if(ydir==UP)
yinst-=yincr;
else
yinst+=yincr;
}
else
rebound();
}
closegraph();
}
int testboundary(void)
{
if(bounce==TRUE)
return TRUE;
else
{
if((xinst+ballrad)>=MAXX || (xinst-ballrad)<=0 ||
(yinst+ballrad)>=MAXY || (yinst-ballrad)<=0)
{
if((xinst+ballrad)>=MAXX)
cuttingbound=RIGHT;
else if((xinst-ballrad)<=0)
cuttingbound=LEFT;
else if((yinst+ballrad)>=MAXY)
cuttingbound=BOTTOM;
else
cuttingbound=TOP;
return FALSE;
}
else
return TRUE;
}
}
void rebound(void)
{
bounce=TRUE;
switch(cuttingbound)
{
case LEFT:xdir=FWD;break;
case RIGHT:xdir=BCK;break;
case TOP:ydir=DWN;break;
case BOTTOM:ydir=UP;break;
}
}
void drawball(void)
{
if(bounce==TRUE)
{
bounce=FALSE;
do{
linecol=random(15);
fillcol=random(15);
}while(linecol==BLACK || fillcol==BLACK || linecol==fillcol);
}
setcolor(linecol);
circle(xinst,yinst,ballrad);
setfillstyle(1,fillcol);
floodfill(xinst,yinst,linecol);
}
1 ความคิดเห็น:
เอ่อ include อะไรค่ะ
แสดงความคิดเห็น