终于完成了!!!累啊
//目前设置了360个随机方向,可以更改M置产生更多随机方向#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<time.h>
#define SCREEN_X 640
#define SCREEN_Y 480//屏幕大小
#define M 360 //1<=方向数目<=360
#define PI 3.141592653589793
#define N 10
#define V 10 //控制运动速度
int R=10;//圆心坐标和半径
struct yuanxin
{
int A;
int B;
};
struct yuanxin *move(struct yuanxin *p,int R,int n);//让球运动的函数
int main(void)
{
//绘图窗口相关设置
initgraph(SCREEN_X , SCREEN_Y);
setbkcolor(WHITE);
cleardevice();
srand((unsigned)time(NULL));//设置随机数种子
int n;//控制方向数目
struct yuanxin O,*p;
p=&O;//指向圆心的指针
p->A = rand()%241+200;
p->B = rand()%81+200;
n = rand()%9; //产生随机数
while(!kbhit())
{
//判断遇到了哪种边界
if( SCREEN_X -(p->A)<=R) n=rand()%(M/2+1)+(M/2);
else if((p->A)<=R) n=rand()%(M/2+1);
else if(SCREEN_Y -(p->B)<=R) n=rand()%(M/2+1)%+(3*M/4);
else if((p->B)<=R) n=rand()%(M/2+1)+(M/4);
else n=rand()%(M+1);
p = move(p,R,n);//遇到边界后任意弹回
}
getch();
closegraph();
return 0;
}
struct yuanxin *move(struct yuanxin *p,int R,int n)
{
struct yuanxin *p2;
p2=p;
do
{
setfillcolor(LIGHTGREEN);
fillcircle(p2->A,p2->B,R);
Sleep(V);
setfillcolor(GREEN);
fillcircle(p2->A,p2->B,R);
//Sleep(V);
p2->A += N*sin(n* 2*PI/M),p2->B -= N*cos(n* 2*PI/M);
}
while( !(( SCREEN_X -(p2->A)<=R) || ((p2->A)<=R) || (SCREEN_Y -(p2->B)<=R) || ((p2->B)<=R)) );//没有到达边界
return(p2);//到达边界后返回主函数,带回指向圆心的指针
}
[ 本帖最后由 haiboself 于 2013-11-22 14:23 编辑 ]