模拟一个球自由落体并反弹的全过程,假定每次反弹都是弹回原来的一半高度,
但是到了一定高度后就不再反弹了;
#include <graphics.h>
#include <conio.h>
#include <math.h>
int main()
{
initgraph(640, 480);
int h= 20;
int y;
int v;
int t = 0;
while (h < 459)//460时,小球最终不会停下来
{
v = 0;
t = sqrt((2 * 10 * h));
v = 10 * t;
//小球下降部分
for (y = h; y <= 460; y += 10)
{
setcolor(YELLOW);
setfillcolor(BLUE);
//绘制一个黄线,蓝色填充的球
fillcircle(320, y, 20);
Sleep(v--);//下降的速度均匀的增大
setcolor(BLACK);
setfillcolor(BLACK);
//绘制一个黑线,黑色填充的球
fillcircle(320, y, 20);
}
//取小球高度的一半
h = ((460 - h) / 2 + h);
t = sqrt((2 * 10 * h));
v = 10 * t;
//小球上升部分
for (y = 460; y >= h; y -= 10)
{
setcolor(YELLOW);
setfillcolor(BLUE);
//绘制一个黄线,蓝色填充的球
fillcircle(320, y, 20);
Sleep(v++);//上升的速度均匀的减小
setcolor(BLACK);
setfillcolor(BLACK);
//绘制一个黑线,黑色填充的球
fillcircle(320, y, 20);
}
}
//最后在地面留下一个黄线,蓝色填充的小球
setcolor(YELLOW);
setfillcolor(BLUE);
fillcircle(320, 460, 20);
//球心坐标
_getch();
closegraph();
return 0;
}