小球碰撞弹回操作

 1 #include <graphics.h>
 2 #include <conio.h>
 3 #include <time.h>
 4 
 5 int main(){
 6     srand((unsigned)time(NULL));
 7     initgraph(640, 480);
 8 
 9     //int x = 21, y = 21;
10     int x = rand() % 598 + 21;
11     int y = rand() % 438 + 21;
12     
13     //int a = 1, b = 1;
14     int a = rand() % 5 + 1;
15     int b = rand() % 5 + 1;
16     while (1){
17 
18         cleardevice();
19         BeginBatchDraw();
20         setcolor(GREEN);
21         setfillcolor(GREEN);
22         fillcircle(x, y, 20);
23         // 边界判定
24         if (x < 20 || x > 620){   
25             a *= -1;
26         }
27         if (y < 20 || y > 460){
28             b *= -1;
29         }
30 
31         x += a;
32         y += b;
33         Sleep(10);
34         EndBatchDraw();
35     }
36 
37     closegraph();
38     return 0;
39 }

// 在640 * 480 内随机一个位置产生一个小球 ,

碰到边框弹回

posted @ 2018-10-19 18:43  浪上了天  阅读(182)  评论(0编辑  收藏  举报