• 简易的贪吃蛇小游戏(以后或许会更新)C++/C语言


    第一版:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #define WIDTH 20
    6. #define HEIGHT 20
    7. int gameOver;
    8. int score;
    9. int x, y; // 蛇头的坐标
    10. int fruitX, fruitY; // 食物的坐标
    11. int tailX[100], tailY[100]; // 蛇身的坐标
    12. int tailLength;
    13. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
    14. enum eDirection dir;
    15. void Setup()
    16. {
    17. gameOver = 0;
    18. score = 0;
    19. x = WIDTH / 2;
    20. y = HEIGHT / 2;
    21. fruitX = rand() % WIDTH;
    22. fruitY = rand() % HEIGHT;
    23. dir = STOP;
    24. }
    25. void Draw()
    26. {
    27. system("cls"); // 清屏
    28. for (int i = 0; i < WIDTH + 2; i++)
    29. printf("#");
    30. printf("\n");
    31. for (int i = 0; i < HEIGHT; i++)
    32. {
    33. for (int j = 0; j < WIDTH; j++)
    34. {
    35. if (j == 0)
    36. printf("#");
    37. if (i == y && j == x)
    38. printf("O");
    39. else if (i == fruitY && j == fruitX)
    40. printf("F");
    41. else
    42. {
    43. int printTail = 0;
    44. for (int k = 0; k < tailLength; k++)
    45. {
    46. if (tailX[k] == j && tailY[k] == i)
    47. {
    48. printf("o");
    49. printTail = 1;
    50. }
    51. }
    52. if (!printTail)
    53. printf(" ");
    54. }
    55. if (j == WIDTH - 1)
    56. printf("#");
    57. }
    58. printf("\n");
    59. }
    60. for (int i = 0; i < WIDTH + 2; i++)
    61. printf("#");
    62. printf("\n");
    63. printf("Score: %d\n", score);
    64. }
    65. void Input()
    66. {
    67. if (_kbhit())
    68. {
    69. switch (_getch())
    70. {
    71. case 'a':
    72. dir = LEFT;
    73. break;
    74. case 'd':
    75. dir = RIGHT;
    76. break;
    77. case 'w':
    78. dir = UP;
    79. break;
    80. case 's':
    81. dir = DOWN;
    82. break;
    83. case 'x':
    84. gameOver = 1;
    85. break;
    86. }
    87. }
    88. }
    89. void Logic()
    90. {
    91. int prevX = tailX[0];
    92. int prevY = tailY[0];
    93. int prev2X, prev2Y;
    94. tailX[0] = x;
    95. tailY[0] = y;
    96. for (int i = 1; i < tailLength; i++)
    97. {
    98. prev2X = tailX[i];
    99. prev2Y = tailY[i];
    100. tailX[i] = prevX;
    101. tailY[i] = prevY;
    102. prevX = prev2X;
    103. prevY = prev2Y;
    104. }
    105. switch (dir)
    106. {
    107. case LEFT:
    108. x--;
    109. break;
    110. case RIGHT:
    111. x++;
    112. break;
    113. case UP:
    114. y--;
    115. break;
    116. case DOWN:
    117. y++;
    118. break;
    119. }
    120. if (x >= WIDTH)
    121. x = 0;
    122. else if (x < 0)
    123. x = WIDTH - 1;
    124. if (y >= HEIGHT)
    125. y = 0;
    126. else if (y < 0)
    127. y = HEIGHT - 1;
    128. for (int i = 0; i < tailLength; i++)
    129. {
    130. if (tailX[i] == x && tailY[i] == y)
    131. gameOver = 1;
    132. }
    133. if (x == fruitX && y == fruitY)
    134. {
    135. score += 10;
    136. fruitX = rand() % WIDTH;
    137. fruitY = rand() % HEIGHT;
    138. tailLength++;
    139. }
    140. }
    141. int main()
    142. {
    143. Setup();
    144. while (!gameOver)
    145. {
    146. Draw();
    147. Input();
    148. Logic();
    149. Sleep(10); // 控制游戏速度
    150. }
    151. return 0;
    152. }

    第二版:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define WIDTH 30
    7. #define HEIGHT 20
    8. int gameOver;
    9. int score;
    10. int x, y; // 蛇头的坐标
    11. int fruitX, fruitY; // 食物的坐标
    12. int tailX[100], tailY[100]; // 蛇身的坐标
    13. int tailLength;
    14. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
    15. enum eDirection dir;
    16. int aiX, aiY; // AI蛇头的坐标
    17. int aiTailX[100], aiTailY[100]; // AI蛇身的坐标
    18. int aiTailLength;
    19. enum eDirection aiDir;
    20. void Setup()
    21. {
    22. gameOver = 0;
    23. score = 0;
    24. x = WIDTH / 2;
    25. y = HEIGHT / 2;
    26. fruitX = rand() % WIDTH;
    27. fruitY = rand() % HEIGHT;
    28. dir = STOP;
    29. aiX = WIDTH / 2 - 5;
    30. aiY = HEIGHT / 2;
    31. aiDir = STOP;
    32. aiTailLength = 0;
    33. }
    34. void Draw()
    35. {
    36. system("cls"); // 清屏
    37. for (int i = 0; i < WIDTH + 2; i++)
    38. printf("#");
    39. printf("\n");
    40. for (int i = 0; i < HEIGHT; i++)
    41. {
    42. for (int j = 0; j < WIDTH; j++)
    43. {
    44. if (j == 0)
    45. printf("#");
    46. if (i == y && j == x)
    47. printf("O");
    48. else if (i == fruitY && j == fruitX)
    49. printf("F");
    50. else if (i == aiY && j == aiX)
    51. printf("A");
    52. else
    53. {
    54. int printTail = 0;
    55. for (int k = 0; k < tailLength; k++)
    56. {
    57. if (tailX[k] == j && tailY[k] == i)
    58. {
    59. printf("o");
    60. printTail = 1;
    61. }
    62. }
    63. for (int k = 0; k < aiTailLength; k++)
    64. {
    65. if (aiTailX[k] == j && aiTailY[k] == i)
    66. {
    67. printf("a");
    68. printTail = 1;
    69. }
    70. }
    71. if (!printTail)
    72. printf(" ");
    73. }
    74. if (j == WIDTH - 1)
    75. printf("#");
    76. }
    77. printf("\n");
    78. }
    79. for (int i = 0; i < WIDTH + 2; i++)
    80. printf("#");
    81. printf("\n");
    82. printf("Score: %d\n", score);
    83. }
    84. void Input()
    85. {
    86. if (_kbhit())
    87. {
    88. switch (_getch())
    89. {
    90. case 'a':
    91. dir = LEFT;
    92. break;
    93. case 'd':
    94. dir = RIGHT;
    95. break;
    96. case 'w':
    97. dir = UP;
    98. break;
    99. case 's':
    100. dir = DOWN;
    101. break;
    102. case 'x':
    103. gameOver = 1;
    104. break;
    105. }
    106. }
    107. }
    108. void AI()
    109. {
    110. if (aiX < fruitX)
    111. aiDir = RIGHT;
    112. else if (aiX > fruitX)
    113. aiDir = LEFT;
    114. else if (aiY < fruitY)
    115. aiDir = DOWN;
    116. else if (aiY > fruitY)
    117. aiDir = UP;
    118. }
    119. void Logic()
    120. {
    121. int prevX = tailX[0];
    122. int prevY = tailY[0];
    123. int prev2X, prev2Y;
    124. tailX[0] = x;
    125. tailY[0] = y;
    126. for (int i = 1; i < tailLength; i++)
    127. {
    128. prev2X = tailX[i];
    129. prev2Y = tailY[i];
    130. tailX[i] = prevX;
    131. tailY[i] = prevY;
    132. prevX = prev2X;
    133. prevY = prev2Y;
    134. }
    135. int aiPrevX = aiTailX[0];
    136. int aiPrevY = aiTailY[0];
    137. int aiPrev2X, aiPrev2Y;
    138. aiTailX[0] = aiX;
    139. aiTailY[0] = aiY;
    140. for (int i = 1; i < aiTailLength; i++)
    141. {
    142. aiPrev2X = aiTailX[i];
    143. aiPrev2Y = aiTailY[i];
    144. aiTailX[i] = aiPrevX;
    145. aiTailY[i] = aiPrevY;
    146. aiPrevX = aiPrev2X;
    147. aiPrevY = aiPrev2Y;
    148. }
    149. switch (dir)
    150. {
    151. case LEFT:
    152. x--;
    153. break;
    154. case RIGHT:
    155. x++;
    156. break;
    157. case UP:
    158. y--;
    159. break;
    160. case DOWN:
    161. y++;
    162. break;
    163. }
    164. switch (aiDir)
    165. {
    166. case LEFT:
    167. aiX--;
    168. break;
    169. case RIGHT:
    170. aiX++;
    171. break;
    172. case UP:
    173. aiY--;
    174. break;
    175. case DOWN:
    176. aiY++;
    177. break;
    178. }
    179. if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)
    180. gameOver = 1;
    181. for (int i = 0; i < tailLength; i++)
    182. {
    183. if (tailX[i] == x && tailY[i] == y)
    184. gameOver = 1;
    185. }
    186. if (aiX >= WIDTH || aiX < 0 || aiY >= HEIGHT || aiY < 0)
    187. gameOver = 1;
    188. for (int i = 0; i < aiTailLength; i++)
    189. {
    190. if (aiTailX[i] == aiX && aiTailY[i] == aiY)
    191. gameOver = 1;
    192. }
    193. if (x == fruitX && y == fruitY)
    194. {
    195. score += 10;
    196. fruitX = rand() % WIDTH;
    197. fruitY = rand() % HEIGHT;
    198. tailLength++;
    199. }
    200. if (aiX == fruitX && aiY == fruitY)
    201. {
    202. score += 10;
    203. fruitX = rand() % WIDTH;
    204. fruitY = rand() % HEIGHT;
    205. aiTailLength++;
    206. }
    207. }
    208. int main()
    209. {
    210. srand(time(NULL)); // 随机数种子
    211. Setup();
    212. while (!gameOver)
    213. {
    214. Draw();
    215. Input();
    216. AI();
    217. Logic();
    218. Sleep(100); // 控制游戏速度
    219. }
    220. printf("GAME OVER\n");
    221. return 0;
    222. }

     制作不易,喜欢的话,给个赞吧。

  • 相关阅读:
    iTOP3399开发板Qt蜂鸣器和LED测试
    Java如何并行多个if语句呢?
    【译】向您介绍改版的 Visual Studio 资源管理器
    网络安全笔记-入侵检测系统
    “似水无形” 的小程序化
    Go学习第十章——文件操作,Json和测试
    使用JQ获取并渲染三级联动分类数据
    【吴恩达L1W2作业2】有关scipy.misc.imresize
    洞见云原生微服务及微服务架构浅析
    从静态链接到动态链接,彻底理解ELF
  • 原文地址:https://blog.csdn.net/m0_69824302/article/details/133690013