-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.2 多球反弹.cpp
118 lines (103 loc) · 2.71 KB
/
4.2 多球反弹.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define High 480 // 游戏画面尺寸
#define Width 640
#define BallNum 15 // 小球个数
int main()
{
float ball_x[BallNum],ball_y[BallNum]; // 小球的坐标
float ball_vx[BallNum],ball_vy[BallNum]; // 小球的速度
float radius; // 小球的半径
int i,j;
radius = 20;
for (i=0;i<BallNum;i++) // 随机小球的位置与速度
{
ball_x[i] = rand()%int(Width-4*radius) + 2*radius;
ball_y[i] = rand()%int(High-4*radius) + 2*radius;
ball_vx[i] = (rand()%2)*2 - 1;
ball_vy[i] = (rand()%2)*2 - 1;
}
initgraph(Width, High);
BeginBatchDraw();
while (1)
{
// 绘制黑线、黑色填充的圆
setcolor(BLACK);
setfillcolor(BLACK);
for (i=0;i<BallNum;i++)
fillcircle(ball_x[i], ball_y[i], radius);
// 更新小圆坐标
for (i=0;i<BallNum;i++)
{
ball_x[i] = ball_x[i] + ball_vx[i];
ball_y[i] = ball_y[i] + ball_vy[i];
// 把超出边界的小球拉回来
if (ball_x[i]<radius)
ball_x[i] = radius;
if (ball_y[i]<radius)
ball_y[i] = radius;
if (ball_x[i]>Width-radius)
ball_x[i] = Width-radius;
if (ball_y[i]>High-radius)
ball_y[i] = High-radius;
}
// 判断是否和墙壁碰撞
for (i=0;i<BallNum;i++)
{
if ((ball_x[i]<=radius)||(ball_x[i]>=Width-radius))
ball_vx[i] = -ball_vx[i];
if ((ball_y[i]<=radius)||(ball_y[i]>=High-radius))
ball_vy[i] = -ball_vy[i];
}
float minDistances2[BallNum][2]; // 记录某个小球,距离它最近的小球的距离,这个小球的下标
for (i=0;i<BallNum;i++)
{
minDistances2[i][0] = 9999999;
minDistances2[i][1] = -1;
}
// 求解所有小球两两之间的距离平方
for (i=0;i<BallNum;i++)
{
for (j=0;j<BallNum;j++)
{
if (i!=j) // 自己和自己不需要比
{
float dist2;
dist2 = (ball_x[i] - ball_x[j])*(ball_x[i] - ball_x[j])
+ (ball_y[i] - ball_y[j])*(ball_y[i] - ball_y[j]);
if (dist2<minDistances2[i][0])
{
minDistances2[i][0] = dist2;
minDistances2[i][1] = j;
}
}
}
}
// 判断球之间是否碰撞
for (i=0;i<BallNum;i++)
{
if (minDistances2[i][0]<=4*radius*radius) // 最小距离小于阈值,发生碰撞
{
j = minDistances2[i][1];
// 交换速度
int temp;
temp = ball_vx[i]; ball_vx[i] = ball_vx[j]; ball_vx[j] = temp;
temp = ball_vy[i]; ball_vy[i] = ball_vy[j]; ball_vy[j] = temp;
minDistances2[j][0] = 999999999; // 避免交换两次速度,又回去了
minDistances2[j][1] = -1;
}
}
// 绘制黄线、绿色填充的圆
setcolor(YELLOW);
setfillcolor(GREEN);
for (i=0;i<BallNum;i++)
fillcircle(ball_x[i], ball_y[i], radius);
FlushBatchDraw();
// 延时
Sleep(3);
}
EndBatchDraw();
closegraph();
return 0;
}