C++实现自由落体的小球

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float x, y, vx, vy, g;
x = 100; // 小球初始X位置
y = 200; // 小球初始Y位置
vx = 5; // 小球初始X速度
vy = 6; // 小球初始y速度
g = 0.1; // 重力
initgraph(600, 600); // 绘制600,600窗体
while (1)
{
cleardevice();
vy = vy + g; // 计算小球y速度
x = x + vx; // 计算小球x位置坐标
y = y + vy; // 计算小球y位置坐标
if (y >= 590) // 判断小球是否碰到或越过地面
vy = -0.95 * vy;
if (x >= 590) // 判断小球碰到右边墙壁
vx = -0.95 * vx;
if (x<=10) // 判断小球碰到左边墙壁
vx = -0.95 * vx;
fillcircle(x, y, 10); // 绘制直径为10的小球
Sleep(5);
}
_getch();
closegraph();
return 0;
}

浙公网安备 33010602011771号