当前位置:   article > 正文

C++ graphics.h 贪吃蛇_c++windows.graphics.h怎样使用

c++windows.graphics.h怎样使用

C++  graphics.h 贪吃蛇

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <ctime>
  4. #define SIZE 20
  5. struct SnakeNode
  6. {
  7. int x;
  8. int y;
  9. struct SnakeNode* next;
  10. };
  11. SnakeNode* head;
  12. SnakeNode* food;
  13. int direction;
  14. int score;
  15. void InitGame()
  16. {
  17. initgraph(640, 480); // 初始化图形界面
  18. setbkcolor(WHITE);
  19. cleardevice();
  20. score = 0;
  21. direction = VK_RIGHT;
  22. SnakeNode* node = new SnakeNode;
  23. node->x = 0;
  24. node->y = 0;
  25. node->next = nullptr;
  26. head = node;
  27. food = new SnakeNode;
  28. srand(static_cast<unsigned>(time(nullptr)));
  29. food->x = rand() % (640 / SIZE) * SIZE;
  30. food->y = rand() % (480 / SIZE) * SIZE;
  31. food->next = nullptr;
  32. }
  33. void DrawSnake()
  34. {
  35. SnakeNode* node = head;
  36. while (node != nullptr)
  37. {
  38. setfillcolor(BLACK);
  39. solidrectangle(node->x, node->y, node->x + SIZE, node->y + SIZE);
  40. node = node->next;
  41. }
  42. }
  43. void DrawFood()
  44. {
  45. setfillcolor(RED);
  46. solidrectangle(food->x, food->y, food->x + SIZE, food->y + SIZE);
  47. }
  48. void UpdateSnake()
  49. {
  50. SnakeNode* node = new SnakeNode;
  51. node->x = head->x;
  52. node->y = head->y;
  53. node->next = head->next;
  54. head->next = node;
  55. if (direction == VK_RIGHT && head->x < 640 - SIZE)
  56. head->x += SIZE;
  57. else if (direction == VK_LEFT && head->x > 0)
  58. head->x -= SIZE;
  59. else if (direction == VK_DOWN && head->y < 480 - SIZE)
  60. head->y += SIZE;
  61. else if (direction == VK_UP && head->y > 0)
  62. head->y -= SIZE;
  63. if (head->x >= 640)
  64. head->x = 0;
  65. else if (head->x < 0)
  66. head->x = 640 - SIZE;
  67. if (head->y >= 480)
  68. head->y = 0;
  69. else if (head->y < 0)
  70. head->y = 480 - SIZE;
  71. if (head->x == food->x && head->y == food->y)
  72. {
  73. score++;
  74. delete food;
  75. food = new SnakeNode;
  76. food->x = rand() % (640 / SIZE) * SIZE;
  77. food->y = rand() % (480 / SIZE) * SIZE;
  78. food->next = nullptr;
  79. }
  80. else
  81. {
  82. SnakeNode* tail = head;
  83. while (tail->next->next != nullptr)
  84. tail = tail->next;
  85. delete tail->next;
  86. tail->next = nullptr;
  87. }
  88. }
  89. bool IsGameOver()
  90. {
  91. SnakeNode* node = head->next;
  92. while (node != nullptr)
  93. {
  94. if (head->x == node->x && head->y == node->y)
  95. return true;
  96. node = node->next;
  97. }
  98. return false;
  99. }
  100. int main()
  101. {
  102. InitGame();
  103. while (true)
  104. {
  105. if (_kbhit())
  106. {
  107. int key = _getch();
  108. if (key == 'D' || key == 'd' || key == VK_RIGHT)
  109. direction = VK_RIGHT;
  110. else if (key == 'A' || key == 'a' || key == VK_LEFT)
  111. direction = VK_LEFT;
  112. else if (key == 'S' || key == 's' || key == VK_DOWN)
  113. direction = VK_DOWN;
  114. else if (key == 'W' || key == 'w' || key == VK_UP)
  115. direction = VK_UP;
  116. }
  117. cleardevice();
  118. DrawSnake();
  119. DrawFood();
  120. UpdateSnake();
  121. if (IsGameOver())
  122. {
  123. MessageBox(nullptr, "Game Over!", "提示", MB_OK);
  124. break;
  125. }
  126. Sleep(200); // 控制游戏速度
  127. }
  128. closegraph();
  129. return 0;
  130. }

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

闽ICP备14008679号