1.0版本

1.实现了小球在窗口中的碰撞
2.随机生成小球的相关属性
3.使用数组生成多个小球
`
//include
include <graphics.h>
include <conio.h>
include <stdio.h>
//using namespace std;
define screen_x 1000
define screen_y 700
define n 2

int get_rand(int min, int max) {
int r = rand() % (max - min + 1) + min;
return r;
}

int main() {
int i, ball_x[100],ball_y[100],ball_r[100],x_speed[100],y_speed[100];
for (i = 1; i <= n; i++) {
ball_r[i] = get_rand(8,20);
ball_x[i] = get_rand(ball_r[i],screen_x-ball_r[i]);
ball_y[i] = get_rand(ball_r[i],screen_y-ball_r[i]);
x_speed[i] = get_rand(5,15);
y_speed[i] = get_rand(3,10);
}

initgraph(screen_x,screen_y);
while(1){
	for (i = 1; i <= n; i++) {
		ball_x[i] += x_speed[i];
		ball_y[i] += y_speed[i];
		if (ball_x[i] > screen_x - ball_r[i] || ball_x[i] < ball_r[i]) {
			x_speed[i] = - x_speed[i];
		}
		if (ball_y[i] > screen_y - ball_r[i]||ball_y[i]<ball_r[i]) {
			y_speed[i] = - y_speed[i];
		}
	}
	
	cleardevice();//清屏
	for (i = 1; i <= n; i++) {
		fillcircle(ball_x[i],ball_y[i],ball_r[i]);
	}
	Sleep(100);
}
return 0;

}

`

2.0版本:

1.把代码提取出来放到不同函数里
`
include <graphics.h>
include <conio.h>
include <stdio.h>
include <time.h>
define screen_x 1000
define screen_y 700
define n 5
int ball_x[n],ball_y[n],ball_r[n],x_speed[n],y_speed[n];

int get_rand(int min, int max) {
int r = rand() % (max - min + 1) + min;
return r;
}

void start() {
int i;
initgraph(screen_x,screen_y);
BeginBatchDraw();//开始批量绘制
srand(time(0));//设定随机数的产生与系统时钟相关联(随机种子函数)
for (i = 0; i < n; i++) {
ball_r[i] = get_rand(8,20);
ball_x[i] = get_rand(ball_r[i],screen_x-ball_r[i]);
ball_y[i] = get_rand(ball_r[i],screen_y-ball_r[i]);
x_speed[i] = get_rand(5,15);
y_speed[i] = get_rand(3,10);
}
}

void show() {
int i;
cleardevice();//清屏
for (i = 0; i <n; i++) {
fillcircle(ball_x[i],ball_y[i],ball_r[i]);
}
FlushBatchDraw();//批量绘制
Sleep(100);
}

void update() {
int i;
for (i =0; i < n; i++){
ball_x[i] += x_speed[i];
ball_y[i] += y_speed[i];
if (ball_x[i] > screen_x - ball_r[i] || ball_x[i] < ball_r[i]) {
x_speed[i] = - x_speed[i];
}
if (ball_y[i] > screen_y - ball_r[i]||ball_y[i]<ball_r[i]) {
y_speed[i] = - y_speed[i];
}
}
}

int main() {
start();
while(1){
show();
update();
}
return 0;
}
`

3.0版本

1.把小球封装为结构体
2.小球碰撞后反弹
`
struct Ball {
int x;
int y;
int r;
int vx;
int vy;
};
Ball b[n];//定义小球结构体变量

int get_rand(int min, int max) {
int r = rand() % (max - min + 1) + min;
return r;
}

void start() {
int i;
initgraph(screen_x,screen_y);
BeginBatchDraw();//开始批量绘制
srand(time(0));//设定随机数的产生与系统时钟相关联(随机种子函数)
for (i = 0; i < n; i++) {
b[i].r = get_rand(8,20);
b[i].x = get_rand(b[i].r,screen_x - b[i].r);
b[i].y = get_rand(b[i].r,screen_y - b[i].r);
b[i].vx = get_rand(5,15);
b[i].vy = get_rand(3,10);
}
}

void show() {
int i;
cleardevice();//清屏
for (i = 0; i <n; i++) {
fillcircle(b[i].x,b[i].y,b[i].r);
}
FlushBatchDraw();//批量绘制
Sleep(100);
}

void update() {
int i,j;
for (i =0; i < n; i++){
b[i].x += b[i].vx;
b[i].y += b[i].vy;
if (b[i].x > screen_x - b[i].r || b[i].x < b[i].r) {
b[i].vx = - b[i].vx;
}
if (b[i].y > screen_y - b[i].r || b[i].y<b[i].r) {
b[i].vy = - b[i].vy;
}
for (j = 0; j < n; j++) {
if (i != j) {
if ((b[i].x-b[j].x)* (b[i].x-b[j].x)+(b[i].y-b[j].y)(b[i].y-b[j].y)<=(b[i].r+b[j].r) (b[i].r + b[j].r)) {
b[i].vx = -b[i].vx;
b[i].vy = -b[i].vy;
b[j].vx = -b[j].vx;
b[j].vy = -b[j].vy;
}
}
}
}
}

int main() {
start();
while(1){
show();
update();
}
return 0;
}

`

include <graphics.h>

include <conio.h>

include <stdio.h>

include <time.h>

define screen_x 1000

define screen_y 700

define n 5