• SimpleCG动画示例--汉诺塔动画演示


    前言

            SimpleCG的使用方法在前面已经介绍了许多,有兴趣的同学如果有去动手,制作一些简单动画应该没多大问题的。所以这次我们来演示一下简单动画。我们刚学习C语言的递归函数时,有一个经典例子相信很多同学都写过,那就是汉诺塔。那么我们今天就来写一个汉诺塔的直观动画演示。

    运行程序下载bin/AnimateHannuo.zip · master · b2b160 / SimpleCG_Demo · GitCode

    一、全部源码

    1. #include "../import/include/CGBoard.h"
    2. #include "math.h"
    3. #ifdef _DEBUG
    4. #pragma comment(lib,"../import/lib/SimpleCG_MDd.lib")
    5. #else
    6. #pragma comment(lib,"../import/lib/SimpleCG_MT.lib")
    7. #endif
    8. #define C_FLOOR_CNT 7
    9. #define C_FLOOR_BOTTOM 380
    10. #define C_HAN_HEIGHT 10
    11. #define C_HAN_SPEED 30
    12. int g_nWidth = 640; //画面宽度
    13. int g_nHeight= 400; //画面高度
    14. enum ENUM_DIRECTION
    15. {
    16. enumDIR_NULL,
    17. enumDIR_UP,
    18. enumDIR_DOWN
    19. };
    20. struct tagHannuo
    21. {
    22. int nNumber;
    23. COLORREF nColor;
    24. int nWidth;
    25. int nPosHan;
    26. int nDir;
    27. POINT ptPos;
    28. };
    29. tagHannuo g_pHannuo[C_FLOOR_CNT];
    30. int g_nMoving = -1;
    31. void DrawHan()
    32. {
    33. int i;
    34. int j=0;
    35. setlinewidth(2);
    36. for(i=0;i<3; i++ )
    37. {
    38. _line( 100 + 200 * i, 50, 100 + 200 * i,C_FLOOR_BOTTOM );
    39. _line( 20 + 200 * i, C_FLOOR_BOTTOM, 180 + 200 * i,C_FLOOR_BOTTOM );
    40. }
    41. int nIndex = 0;
    42. for(j=0;j<3;++j)
    43. {
    44. nIndex = 0;
    45. for(i=C_FLOOR_CNT-1;i>=0; i-- )
    46. {
    47. if(g_pHannuo[i].nPosHan == j && i != g_nMoving)
    48. {
    49. setfillcolor(g_pHannuo[i].nColor);
    50. _solidrectangle( 100 +200 * g_pHannuo[i].nPosHan - g_pHannuo[i].nWidth/2, C_FLOOR_BOTTOM - nIndex * C_HAN_HEIGHT - C_HAN_HEIGHT, 100 +200 * g_pHannuo[i].nPosHan + g_pHannuo[i].nWidth/2, C_FLOOR_BOTTOM - nIndex * C_HAN_HEIGHT);
    51. ++nIndex;
    52. }
    53. }
    54. }
    55. }
    56. void DrawMoving()
    57. {
    58. if(g_nMoving>=0)
    59. {
    60. setfillcolor(g_pHannuo[g_nMoving].nColor);
    61. _solidrectangle( g_pHannuo[g_nMoving].ptPos.x, g_pHannuo[g_nMoving].ptPos.y, g_pHannuo[g_nMoving].ptPos.x + g_pHannuo[g_nMoving].nWidth, g_pHannuo[g_nMoving].ptPos.y+ C_HAN_HEIGHT);
    62. }
    63. }
    64. void DrawAll()
    65. {
    66. ClearDevice();
    67. DrawHan();
    68. DrawMoving();
    69. ReflushWindow();
    70. }
    71. void Moving( int nItem, int nFrom, int nTo )
    72. {
    73. g_nMoving = nItem;
    74. g_pHannuo[nItem].ptPos.x = 100 +200 * nFrom - g_pHannuo[nItem].nWidth/2;
    75. for( g_pHannuo[nItem].ptPos.y = C_FLOOR_BOTTOM - C_FLOOR_CNT * C_HAN_HEIGHT; IsShowingWindow()&&g_pHannuo[nItem].ptPos.y>40; g_pHannuo[nItem].ptPos.y-=10 )
    76. {
    77. DrawAll();
    78. Sleep(C_HAN_SPEED);
    79. }
    80. int nXStep = (nTo - nFrom) * 5;
    81. int nDest = 100 +200 * nTo - g_pHannuo[nItem].nWidth/2;
    82. for( g_pHannuo[nItem].ptPos.x = 100 +200 * nFrom - g_pHannuo[nItem].nWidth/2; IsShowingWindow()&&abs(g_pHannuo[nItem].ptPos.x-nDest)>5; g_pHannuo[nItem].ptPos.x+=nXStep )
    83. {
    84. DrawAll();
    85. Sleep(C_HAN_SPEED);
    86. }
    87. g_pHannuo[nItem].ptPos.x = 100 +200 * nTo - g_pHannuo[nItem].nWidth/2;
    88. for( g_pHannuo[nItem].ptPos.y = 40; IsShowingWindow()&&g_pHannuo[nItem].ptPos.y10 )
    89. {
    90. DrawAll();
    91. Sleep(C_HAN_SPEED);
    92. }
    93. g_nMoving = -1;
    94. g_pHannuo[nItem].nPosHan = nTo;
    95. }
    96. void MoveHan( int nFloor, int nFrom, int nTo, int nMiddle )
    97. {
    98. if( nFloor == 1 )
    99. {
    100. Moving( nFloor-1, nFrom-1, nTo-1);
    101. return;
    102. }
    103. MoveHan( nFloor-1, nFrom, nMiddle, nTo );
    104. Moving( nFloor-1, nFrom-1, nTo-1);
    105. MoveHan( nFloor-1, nMiddle, nTo, nFrom );
    106. }
    107. void DrawProcess()
    108. {
    109. bool bIsRunning = true;
    110. int i;
    111. srand(GetTickCount());
    112. for(i=0;i
    113. {
    114. g_pHannuo[i].nNumber=i+1;
    115. g_pHannuo[i].nColor = RGB(rand()%200,rand()%200,rand()%200);
    116. g_pHannuo[i].nDir = enumDIR_NULL;
    117. g_pHannuo[i].nPosHan = 0;
    118. g_pHannuo[i].nWidth = 20*(i+1);
    119. }
    120. MoveHan(C_FLOOR_CNT,1,2,3);
    121. DrawAll();
    122. }
    123. int _tmain(int argc, _TCHAR* argv[])
    124. {
    125. //初始化
    126. if( !ShowingBoard(g_nWidth,g_nHeight, DrawProcess))
    127. return 1;
    128. //关闭图库
    129. CloseBoard();
    130. return 0;
    131. }

    对于写过汉诺塔的同学来说,程序逻辑应该没什么难度,就是在递归程序上增加了动画过程。

    二、演示效果

    对于5层来说是不难的,但递归对于层数增加所带来的时间消耗是呈指数增加的,所以通过动画来观察层数增加带来的时间消耗非常直观。在原始的汉诺塔里是64层,要移完所有的层数将会世界末日,因为即便到世界的尽头也无法完成。有兴趣的同学可以把代码输入并把层数加大看看。

    三、代码下载

    汉诺塔演示源代码

    AnimateHannuo · master · b2b160 / SimpleCG_Demo · GitCode

    库安装方法如下

    SimpleCG库安装使用_b2b160的博客-CSDN博客

  • 相关阅读:
    DBCO-PEG-Lectins|二苯并环辛炔-聚乙二醇-凝集素|DBCO-PEG-凝集素
    【TSP问题】基于Hopfield神经网络求解旅行商问题附Matlab代码
    金仓数据库 KingbaseES PL/SQL 过程语言参考手册(9.PL/SQL子程序)
    函数—描述现实世界现象的基本工具
    这 13 个可能是你一直未使用过的超级棒的 Python 用法
    泰凌微BLE(2):ATT自定义UUID并实现Notification数据传输
    如何在mac上安装多版本python并配置PATH
    ts快速上手笔记01
    PRD文档
    【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
  • 原文地址:https://blog.csdn.net/b2b160/article/details/133470592