当前位置:   article > 正文

EasyX图形随机移动实践_easyx实现平滑移动图像

easyx实现平滑移动图像

有一小球在一矩形框内的任意位置,初始时以随机角度,随机速度往下掉落,每次碰到边框时以随机角度,随机速度继续运行,按ESC键结束。编写程序模拟小球的运动过程。

  1. #include <conio.h>//要用到_kbhit()函数
  2. #include <time.h>//要用到随机数
  3. #include <graphics.h>
  4. int get_randx();//实现随机出现
  5. int get_randy();
  6. int get_kx();//1~6随机取一个
  7. int get_ky();
  8. int main()
  9. {
  10. initgraph(800, 800);
  11. setbkcolor(RGB(150, 150, 90));
  12. cleardevice();
  13. int x = get_randx();
  14. int y = get_randy();
  15. int kx = get_kx();
  16. int ky = get_ky();
  17. while (!(_kbhit() && _getch() == 27))
  18. {
  19. if ((x + kx + 20) >= 800)//四个边界检测,若达到边界,进行回弹并赋予随机速度
  20. {
  21. kx = -(get_kx());
  22. x += kx;
  23. }
  24. else if ((x + kx - 20) <= 0)
  25. {
  26. kx = get_kx();
  27. x += kx;
  28. }
  29. if (y + ky + 20 >= 800)
  30. {
  31. ky = -(get_ky());
  32. y += ky;
  33. }
  34. else if ((y + ky - 20) <= 0)
  35. {
  36. ky = get_ky();
  37. y += ky;
  38. }
  39. setcolor(RGB(50, 80, 80));//图像刷新
  40. setfillcolor(RGB(50, 80, 80));
  41. fillcircle(x, y, 20);
  42. Sleep(20);
  43. setcolor(RGB(150, 150, 90));
  44. setfillcolor(RGB(150, 150, 90));
  45. fillcircle(x, y, 20);
  46. x += kx;
  47. y += ky;
  48. }
  49. closegraph();
  50. return 0;
  51. }
  52. int get_randx()
  53. {
  54. srand((unsigned)time(NULL) * (unsigned)rand());
  55. return rand() % 800;
  56. }
  57. int get_randy()
  58. {
  59. srand((unsigned)time(NULL) + (unsigned)rand());
  60. return rand() % 800;
  61. }
  62. int get_kx()
  63. {
  64. srand((unsigned)time(NULL) + (unsigned)rand());
  65. int k = rand() % 6;
  66. while (k == 0)
  67. k = rand() % 6;
  68. return k;
  69. }
  70. int get_ky()
  71. {
  72. srand((unsigned)time(NULL) + (unsigned)rand());
  73. int k = rand() % 6;
  74. while (k == 0)
  75. k = rand() % 6;
  76. return k;
  77. }

 46ba91cbefc544eda97401e2e70c0c7a.png

 

 看懂的掌声?

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/94145
推荐阅读
相关标签
  

闽ICP备14008679号