EasyX—模拟小球自由落体
1 #include <stdio.h> 2 #include <graphics.h> 3 4 #define SCREEN_WIDTH 640 5 #define SCREEN_HEIGHT 480 6 #define R 20 7 #define T 0.01 8 #define G 9.8 9 #define EPSILON 5E-3 10 11 int main() { 12 initgraph( SCREEN_WIDTH, SCREEN_HEIGHT ); 13 circle( SCREEN_WIDTH / 2, R, R ); 14 15 double h = R; //小球当前的位置 16 double v = 0.0; //初速度 17 18 BeginBatchDraw(); 19 20 while( h <= SCREEN_HEIGHT - R ) { 21 circle( SCREEN_WIDTH / 2, h, R ); //重新绘制小球 22 FlushBatchDraw(); 23 24 Sleep( 1 ); 25 h = h + v * T + 0.5 * G * T * T; 26 v = v + G * T; 27 if( h > SCREEN_HEIGHT - R ) { 28 h = SCREEN_HEIGHT - R; 29 v = 0.7 * v; 30 v = -v; 31 } 32 33 if( abs( v ) < EPSILON && h >= SCREEN_HEIGHT - R ) { 34 break; 35 } 36 cleardevice(); 37 } 38 EndBatchDraw(); 39 40 closegraph(); 41 return 0; 42 }