#include <graphics.h>
#include <conio.h>
#include <math.h>
#define LINE 850
#define ROW 460
#define RNUM 50
void movecircle(int x,int y)
{BeginBatchDraw();setfillcolor(YELLOW);solidcircle(x,y,RNUM);FlushBatchDraw();Sleep(5);setfillcolor(BLACK);solidcircle(x,y,RNUM);
}
void chancemove(int *x,int *y,int *movex,int *movey)
{if (((*x)+RNUM) >= LINE){*movex = -*movex;}else if (*x < RNUM){*movex = -*movex;}
if (((*y)+RNUM) >= ROW){*movey = -*movey;}else if (*y < RNUM){*movey = -*movey;}
}
int pengzhuang(int *x1,int *y1,int *x2,int *y2)
{float tempx;float tempy;
tempx = *x1-*x2;
tempy = *y1-*y2;
tempx *= tempx;
tempy *= tempy;
if (sqrt(tempx + tempy)/2-RNUM < 1 && sqrt(tempx + tempy)/2-RNUM > -1)
{return 1;}
elsereturn 0;
}
void main(void)
{int x1=RNUM,y1 =RNUM;int x2=LINE - RNUM,y2 =ROW - RNUM;
int movex1 = 1,movey1 = 1;int movex2 = 1,movey2 = 1;
initgraph(LINE,ROW);
while (1)
{
chancemove(&x1,&y1,&movex1,&movey1);
chancemove(&x2,&y2,&movex2,&movey2);
movecircle(x1,y1);
movecircle(x2,y2);
if (pengzhuang(&x1,&y1,&x2,&y2))
{movex1 = -movex1;movey1 = -movey1;movex2 = -movex2;movey2 = -movey2;}
x1 = x1+movex1;y1 = y1+movey1;
x2 = x2+movex2;y2 = y2+movey2;
}
getch();
closegraph();
}
#include <conio.h>
#include <math.h>
#define LINE 850
#define ROW 460
#define RNUM 50
void movecircle(int x,int y)
{BeginBatchDraw();setfillcolor(YELLOW);solidcircle(x,y,RNUM);FlushBatchDraw();Sleep(5);setfillcolor(BLACK);solidcircle(x,y,RNUM);
}
void chancemove(int *x,int *y,int *movex,int *movey)
{if (((*x)+RNUM) >= LINE){*movex = -*movex;}else if (*x < RNUM){*movex = -*movex;}
if (((*y)+RNUM) >= ROW){*movey = -*movey;}else if (*y < RNUM){*movey = -*movey;}
}
int pengzhuang(int *x1,int *y1,int *x2,int *y2)
{float tempx;float tempy;
tempx = *x1-*x2;
tempy = *y1-*y2;
tempx *= tempx;
tempy *= tempy;
if (sqrt(tempx + tempy)/2-RNUM < 1 && sqrt(tempx + tempy)/2-RNUM > -1)
{return 1;}
elsereturn 0;
}
void main(void)
{int x1=RNUM,y1 =RNUM;int x2=LINE - RNUM,y2 =ROW - RNUM;
int movex1 = 1,movey1 = 1;int movex2 = 1,movey2 = 1;
initgraph(LINE,ROW);
while (1)
{
chancemove(&x1,&y1,&movex1,&movey1);
chancemove(&x2,&y2,&movex2,&movey2);
movecircle(x1,y1);
movecircle(x2,y2);
if (pengzhuang(&x1,&y1,&x2,&y2))
{movex1 = -movex1;movey1 = -movey1;movex2 = -movex2;movey2 = -movey2;}
x1 = x1+movex1;y1 = y1+movey1;
x2 = x2+movex2;y2 = y2+movey2;
}
getch();
closegraph();
}