服务器之家

服务器之家 > 正文

C++控制台实现贪吃蛇游戏

时间:2021-09-03 14:49     来源/作者:XD灬

本文实例为大家分享了C++实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

刚学完了C语言,便尝试的写了贪吃蛇的代码,但是效果不佳,很多的bug,所以,这个学了C++,便重新的写了这个小游戏,用类来封装!

先是头文件:

  1. struct Snake
  2. {
  3. int x, y;
  4. };
  5. class snake
  6. {
  7. public:
  8. snake() //构造函数
  9. {
  10. length = 3;
  11. s[2].x = 10;
  12. s[2].y = 10;
  13. s[1].x = 9;
  14. s[1].y = 10;
  15. s[0].x = 8;
  16. s[0].y = 10;
  17. up = right = left = down = 0;
  18. }
  19. ~snake(){}
  20. void display(); //显示蛇身函数
  21. void Rightmove(); //右移函数
  22. void Leftmove(); //左移函数
  23. void Upmove(); //上移函数
  24. void Downmove(); //下移函数
  25. int cheak(); //检查是否撞墙或撞到自身
  26. void creat_food(); //产生食物
  27. int eat_food(); //吃食物
  28. private:
  29. struct Snake s[100]; //先定义蛇身最长100
  30. int length; //当前蛇长度
  31. int x3, y3; //食物坐标
  32. int up, down, right, left; //蛇的状态,是上移还是下移或...
  33. };
  34.  
  35. void make_frame(); //打印框架的函数
  36. void show(); //游戏开始倒计时函数
  37. void gameover(); //游戏结束函数

下面是各个函数的实现的cpp文件:

  1. # include <iostream>
  2. # include <Windows.h>
  3. # include <time.h>
  4. # include "snake.h"
  5. # define MaxLen 20
  6. # define MaxWen 30
  7. using namespace std;
  8. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄
  9. void gotoxy(HANDLE hOut, int x, int y) //输出位置的函数
  10. {
  11. COORD pos;
  12. pos.X = x;
  13. pos.Y = y;
  14. SetConsoleCursorPosition(hOut, pos);
  15. }
  16.  
  17. void snake::display() //打印蛇身
  18. {
  19. for (int i = length - 1; i >= 0; i--)
  20. {
  21. if (i == length - 1) //打印蛇头
  22. {
  23. gotoxy(hOut, s[i].x, s[i].y);
  24. cout << char(15);
  25. }
  26. else //打印蛇身
  27. {
  28. gotoxy(hOut, s[i].x, s[i].y);
  29. cout << '*';
  30. }
  31. }
  32. gotoxy(hOut, 0, 22);
  33. }
  34. void snake::Rightmove() //右移
  35. {
  36. right = 1; up = down = left = 0;
  37. int x1, x2, y1, y2;
  38. x1 = x2 = s[length - 1].x;
  39. y1 = y2 = s[length - 1].y;
  40. s[length - 1].x++; //蛇头x坐标自增
  41. for (int i = length - 2; i >= 0; i--) //除了蛇头,其他的结点都等于它的上一个结点的坐标
  42. {
  43. x2 = s[i].x; y2 = s[i].y;
  44. s[i].x = x1; s[i].y = y1;
  45. x1 = x2; y1 = y2;
  46. }
  47. gotoxy(hOut, x2, y2); //消除蛇移动遗留的 ‘*'
  48. cout << ' ';
  49. }
  50. void snake::Leftmove() //左移
  51. {
  52. left = 1; right = up = down = 0;
  53. int x1, x2, y1, y2;
  54. x1 = x2 = s[length - 1].x;
  55. y1 = y2 = s[length - 1].y;
  56. s[length - 1].x--; //同上
  57. for (int i = length - 2; i >= 0; i--)
  58. {
  59. x2 = s[i].x; y2 = s[i].y;
  60. s[i].x = x1; s[i].y = y1;
  61. x1 = x2; y1 = y2;
  62. }
  63. gotoxy(hOut, x2, y2); //同上
  64. cout << ' ';
  65. }
  66. void snake::Downmove() //下移
  67. {
  68. down = 1; right = up = left = 0;
  69. int x1, x2, y1, y2;
  70. x1 = x2 = s[length - 1].x;
  71. y1 = y2 = s[length - 1].y;
  72. s[length - 1].y++; //同上
  73. for (int i = length - 2; i >= 0; i--)
  74. {
  75. x2 = s[i].x; y2 = s[i].y;
  76. s[i].x = x1; s[i].y = y1;
  77. x1 = x2; y1 = y2;
  78. }
  79. gotoxy(hOut, x2, y2); //同上
  80. cout << ' ';
  81. }
  82. void snake::Upmove() //上移
  83. {
  84. up = 1; down = right = left = 0;
  85. int x1, x2, y1, y2;
  86. x1 = x2 = s[length - 1].x;
  87. y1 = y2 = s[length - 1].y;
  88. s[length - 1].y--; //同上
  89. for (int i = length - 2; i >= 0; i--)
  90. {
  91. x2 = s[i].x; y2 = s[i].y;
  92. s[i].x = x1; s[i].y = y1;
  93. x1 = x2; y1 = y2;
  94. }
  95. gotoxy(hOut, x2, y2); //同上
  96. cout << ' ';
  97. }
  98. int snake::cheak()
  99. {
  100. int flag = 0;
  101. for (int i = length - 2; i >= 0; i--) //是否撞到自身
  102. {
  103. if (s[i].x == s[length - 1].x && s[i].y == s[length - 1].y)
  104. {
  105. flag = 1; //是,标识符为1
  106. break;
  107. }
  108. }
  109. if (flag == 1 || (s[length - 1].x >= 30 + 1 || s[length - 1].x < 4) || (s[length - 1].y <= 1 || s[length - 1].y >= 20))
  110. {
  111. return 0; //检测是否撞自身,或者撞墙
  112. }
  113. else
  114. {
  115. return 1;
  116. }
  117. }
  118. void snake::creat_food() //产生食物坐标
  119. {
  120. xy: x3 = (rand() % (25)) + 3;
  121. y3 = (rand() % (17)) + 2;
  122. for (int i = length - 1; i >= 0; i--) //检查食物是否在蛇身上
  123. {
  124. if (s[i].x == x3 && s[i].y == y3) //是就重新产生食物坐标
  125. goto xy;
  126. }
  127. gotoxy(hOut, x3, y3); //显示食物
  128. cout << '*';
  129. }
  130. int snake::eat_food()
  131. {
  132. if (s[length - 1].x == x3 && s[length - 1].y == y3) //蛇头碰到食物
  133. {
  134. if (up == 1) //如果蛇是在上移,增加一个结点,为蛇头的上一个结点
  135. {
  136. s[length].x = x3;
  137. s[length].y = y3 - 1;
  138. }
  139. else if (down == 1) //同上
  140. {
  141. s[length].x = x3;
  142. s[length].y = y3 + 1;
  143. }
  144. else if (right == 1) //同上
  145. {
  146. s[length].x = x3 + 1;
  147. s[length].y = y3;
  148. }
  149. else if (left == 1) //同上
  150. {
  151. s[length].x = x3 - 1;
  152. s[length].y = y3;
  153. }
  154. length++; //蛇长加1
  155. return 1;
  156. }
  157. else
  158. return 0;
  159. }
  160. void make_frame() //打印框架函数
  161. {
  162. cout << " 贪吃蛇游戏" << endl;
  163. gotoxy(hOut, 2, 1);
  164. cout << "╔";
  165. for (int i = 4; i < 2 + MaxWen; i++)
  166. {
  167. gotoxy(hOut, i, 1);
  168. printf("=");
  169. }
  170. for (int i = 2; i < MaxLen; i++)
  171. {
  172. gotoxy(hOut, 2, i);
  173. printf("║");
  174. }
  175. gotoxy(hOut, 2 + MaxWen, 1);
  176. printf("╗");
  177. for (int i = 2; i < MaxLen; i++)
  178. {
  179. gotoxy(hOut, 2 + MaxWen, i);
  180. printf("║");
  181. }
  182. gotoxy(hOut, 2, MaxLen);
  183. printf("╚");
  184. gotoxy(hOut, 2 + MaxWen, MaxLen);
  185. printf("╝");
  186. for (int i = 4; i < 2 + MaxWen; i++)
  187. {
  188. gotoxy(hOut, i, MaxLen);
  189. printf("=");
  190. }
  191. }
  192. void show() //显示操作方法和游戏开始倒计时
  193. {
  194. gotoxy(hOut, 35, 5);
  195. cout << "↑:" << 'w';
  196. gotoxy(hOut, 35, 6);
  197. cout << "←:" << 'a';
  198. gotoxy(hOut, 35, 7);
  199. cout << "↓:" << 's';
  200. gotoxy(hOut, 35, 8);
  201. cout << "→:" << 'd';
  202. gotoxy(hOut, 16, 5);
  203. cout << '3';
  204. Sleep(1000);
  205. gotoxy(hOut, 16, 5);
  206. cout << '2';
  207. Sleep(1000);
  208. gotoxy(hOut, 16, 5);
  209. cout << '1';
  210. Sleep(1000);
  211. gotoxy(hOut, 16, 5);
  212. cout << ' ';
  213. }
  214. void gameover() //游戏结束函数
  215. {
  216. system("cls");
  217. system("color 3B");
  218. gotoxy(hOut, 14, 5);
  219. cout << " GAME OVER!";
  220. gotoxy(hOut, 14, 6);
  221. cout << "PLAY AGAIN ? Y(yes) \ N(no)";
  222. }

主函数的cpp文件:

  1. # include <iostream>
  2. # include <Windows.h>
  3. # include <conio.h>
  4. # include "snake.h"
  5. using namespace std;
  6. char ch;
  7. int main()
  8. {
  9. while (1)
  10. {
  11. snake sn; //声明对象
  12. system("cls"); //清屏
  13. system("color 3B"); //背景和字体颜色调整
  14. make_frame(); //打印框架
  15. sn.display(); //显示蛇
  16. show(); //游戏开始
  17. sn.creat_food(); //产生食物
  18. while (sn.cheak()) //检查是否死亡
  19. {
  20. sn.Rightmove(); //右移
  21. sn.display(); //显示蛇身
  22. if (sn.eat_food()) //检查是否吃到食物
  23. {
  24. sn.creat_food(); //重新产生食物
  25. sn.display();
  26. }
  27. Sleep(500); //等待500Ms
  28. p: if (_kbhit()) //是否有按键
  29. {
  30. ch = _getch();
  31. if (ch == 97 || ch == 100)
  32. goto p;
  33. if (ch == 115 || ch == 119)
  34. break;
  35. }
  36. }
  37. pp: switch (ch) //有按键
  38. {
  39. case 119: //上移的情况
  40. {
  41. while (sn.cheak())
  42. {
  43. sn.Upmove();
  44. sn.display();
  45. if (sn.eat_food())
  46. {
  47. sn.creat_food();
  48. sn.display();
  49. Sleep(300);
  50. }
  51. Sleep(500);
  52. pw: if (_kbhit())
  53. {
  54. ch = _getch();
  55. if (ch == 119 || ch == 115)
  56. goto pw;
  57. if (ch == 97 || ch == 100)
  58. goto pp;
  59. }
  60. }
  61. }break;
  62. case 97: //左移的情况
  63. {
  64. while (sn.cheak())
  65. {
  66. sn.Leftmove();
  67. sn.display();
  68. if (sn.eat_food())
  69. {
  70. sn.creat_food();
  71. sn.display();
  72. }
  73. Sleep(500);
  74. pa: if (_kbhit())
  75. {
  76. ch = _getch();
  77. if (ch == 97 || ch == 100)
  78. goto pa;
  79. if (ch == 115 || ch == 119)
  80. goto pp;
  81. }
  82. }
  83. }break;
  84. case 115: //下移的情况
  85. {
  86. while (sn.cheak())
  87. {
  88. sn.Downmove();
  89. sn.display();
  90. if (sn.eat_food())
  91. {
  92. sn.creat_food();
  93. sn.display();
  94. Sleep(300);
  95. }
  96. Sleep(500);
  97. ps: if (_kbhit())
  98. {
  99. ch = _getch();
  100. if (ch == 115 || ch == 119)
  101. goto ps;
  102. if (ch == 97 || ch == 100)
  103. goto pp;
  104. }
  105. }
  106. }break;
  107. case 100: //右移的情况
  108. {
  109. while (sn.cheak())
  110. {
  111. sn.Rightmove();
  112. sn.display();
  113. if (sn.eat_food())
  114. {
  115. sn.creat_food();
  116. sn.display();
  117. }
  118. Sleep(500);
  119. pd: if (_kbhit())
  120. {
  121. ch = _getch();
  122. if (ch == 100 || ch == 97)
  123. goto pd;
  124. if (ch == 119 || ch == 115)
  125. goto pp;
  126. }
  127. }
  128. }break;
  129. default:
  130. break;
  131. }
  132. gameover(); //显示游戏结束,是否重玩
  133. py: ch = _getch();
  134. if (ch == 110) //否
  135. {
  136. system("cls");
  137. break;
  138. }
  139. else if (ch == 121) //是
  140. continue;
  141. else
  142. goto py;
  143. }
  144. return 0;
  145. }

下面是游戏的截图:

C++控制台实现贪吃蛇游戏

C++控制台实现贪吃蛇游戏

C++控制台实现贪吃蛇游戏

C++控制台实现贪吃蛇游戏

控制台的实现,不是很美观,主要是由于上下和左右的间隙不一样大,所以看起来不是很好看,但总体还是实现了贪吃蛇!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_25425023/article/details/44088109

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部