当前位置:   article > 正文

贪吃蛇(关卡设计)_贪吃蛇关卡设计

贪吃蛇关卡设计

SnakeGame.h文件

  1. #pragma once
  2. #include<iostream>
  3. #include<list>
  4. #include<conio.h>
  5. #include<Windows.h>
  6. #include<time.h>
  7. #include<string>
  8. using namespace std;
  9. class SnakeGame
  10. {
  11. public:
  12. SnakeGame();
  13. virtual ~SnakeGame();
  14. private:
  15. int dir;//方向
  16. string q[4] = { "你真厉害! ",
  17. "666 (⊙o⊙) ",
  18. "通关有奖励哦 QWQ",
  19. "加油 (*>.<*) " };
  20. int grades;//分数
  21. COORD coord;//保存下一次前进的点
  22. list<COORD>Snake;//保存蛇身
  23. int mp[26][26];//保存地图
  24. public:
  25. void CreateMp();//初始化地图
  26. void SetCOORD(int x, int y);//移动光标
  27. void Move();//蛇移动
  28. bool GameOver(int x, int y);//判断蛇是否死亡
  29. void CreateFood();//创建一个食物
  30. int ChangeDir(char ch);//将字符转换成数字w:1 a:2 s:3 d:4
  31. void Mood();//打印表情QWQ
  32. void SetColor();//设置颜色
  33. // void Create();
  34. void CreateBarrier();//第二关
  35. void SetCustom2();//第三关
  36. private:
  37. int Custom;//记录关卡
  38. public:
  39. void init();//初始化
  40. void StartGame();//开始游戏
  41. };

SnakeGame.cpp文件


  1. #include "SnakeGame.h"
  2. SnakeGame::SnakeGame()
  3. {
  4. }
  5. SnakeGame::~SnakeGame()
  6. {
  7. }
  8. void SnakeGame::CreateMp()
  9. {
  10. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  11. SetConsoleTextAttribute(hCon, (12) | (0));//地图设置为红色
  12. for (int i = 0; i < 21; i++)
  13. {
  14. mp[i][0] = 1;
  15. mp[0][i] = 1;
  16. SetCOORD(0, i);
  17. cout << "■";
  18. Sleep(10);
  19. SetCOORD(i * 2, 0);
  20. cout << "■";
  21. Sleep(10);
  22. }
  23. for (int i = 0; i < 21; i++)
  24. {
  25. mp[20][i] = 1;
  26. mp[i][20] = 1;
  27. SetCOORD(20*2, i);
  28. cout << "■";
  29. Sleep(10);
  30. SetCOORD(i * 2, 20);
  31. cout << "■";
  32. Sleep(10);
  33. }
  34. //CreateBarrier();
  35. SetCOORD(10 * 2, 10);
  36. cout << " ";
  37. SetCOORD(6 * 2, 7);
  38. cout << "第" << Custom + 1 << "关";
  39. SetCOORD(6 * 2, 10);
  40. cout << "请按任意键开始游戏";
  41. SetCOORD(2, 23);
  42. cout << "方向控制:";
  43. SetCOORD(2, 24);
  44. cout << "上:W 下:S 左:A 右:D";
  45. SetCOORD(2, 25);
  46. cout << "鼠标点击屏幕暂停游戏";
  47. SetCOORD(24*2, 2);
  48. cout << "你的得分为:" << grades;
  49. SetCOORD(2, 26);
  50. cout << "制作:ZZK" << endl;
  51. SetCOORD(24 * 2, 4);
  52. cout << "游戏图标说明:";
  53. SetCOORD(24 * 2, 5);
  54. cout << "障碍:■";
  55. SetCOORD(24 * 2, 6);
  56. cout << "食物:★";
  57. SetCOORD(24 * 2, 11);
  58. cout << "第" << Custom + 1 << "关";
  59. SetCOORD(24 * 2, 12);
  60. cout << "吃到40个五角星进入下一关";
  61. }
  62. void SnakeGame::SetCOORD(int x, int y)//设置光标
  63. {
  64. COORD xx;//windows.h里的一个结构体,有两个参数 X 和 Y 分别代表列和行
  65. xx.X = x;
  66. xx.Y = y;
  67. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xx);//windows.h里的函数--把光标移到xx处
  68. }
  69. void SnakeGame::Move()//移动
  70. {
  71. char ch = 'w';
  72. while (true)
  73. {
  74. ch = _getch();//立即从键盘的到一个字符
  75. while (!_kbhit())//_kbhit()函数没有字符时输入返回0
  76. {
  77. int kk = ChangeDir(ch);
  78. if (kk != dir&&abs(kk - dir) != 2)//判断是否能改变方向
  79. {
  80. dir = kk;
  81. }
  82. coord = Snake.front();
  83. switch (dir)//1上2左3下4右
  84. {
  85. case 1:coord.Y--; break;
  86. case 2:coord.X--; break;
  87. case 3:coord.Y++; break;
  88. case 4:coord.X++; break;
  89. }
  90. if (GameOver(coord.X, coord.Y))//判读蛇是否死掉
  91. {
  92. system("cls");
  93. COORD s;
  94. s.X = 10 * 2;
  95. s.Y = 10;
  96. SetCOORD(s.X, s.Y);
  97. cout << "游戏结束,你挂了QWQ";
  98. SetCOORD(s.X, s.Y + 1);
  99. cout << "你的分数为:" << grades;
  100. SetCOORD(s.X, s.Y + 2);
  101. cout << "要重新开始咩?";
  102. SetCOORD(s.X, s.Y + 3);
  103. cout << "1:重开就重开 2:算了吧";
  104. SetCOORD(s.X, s.Y + 4);
  105. cout << "你的选择:";
  106. int kkk;
  107. cin >> kkk;
  108. if (kkk == 1)
  109. {
  110. system("cls");
  111. init();
  112. StartGame();
  113. return;
  114. }
  115. return;
  116. }
  117. if (mp[coord.Y][coord.X] == 2)//吃到食物
  118. {
  119. SetColor();
  120. grades+=1;
  121. if (grades == 40)//达到40分进入下一关
  122. {
  123. Custom++;
  124. SetCOORD(24 * 2, 12);
  125. cout << "恭喜你进入下一关";
  126. SetCOORD(24 * 2, 13);
  127. cout << "进入下一关吗? 1:肯定进入啊 2:算了吧,我认怂QWQ";
  128. SetCOORD(24 * 2, 14);
  129. cout << "你的选择是:";
  130. int mm;
  131. cin >> mm;
  132. if (mm != 2)
  133. {
  134. system("cls");
  135. SetCOORD(10 * 2, 10);
  136. cout << "离进入下一关还有3秒";
  137. Sleep(3000);
  138. //判断下一关是第几关
  139. if (Custom == 1)
  140. {
  141. init();
  142. Custom = 1;
  143. CreateBarrier();
  144. }
  145. else if (Custom == 2)
  146. {
  147. init();
  148. Custom = 2;
  149. SetCustom2();
  150. }
  151. else
  152. {
  153. system("cls");
  154. SetCOORD(10 * 2, 10);
  155. cout << "恭喜你,通关了,你真6";
  156. return;
  157. }
  158. StartGame();
  159. return;
  160. }
  161. else
  162. {
  163. SetCOORD(24 * 2, 15);
  164. cout << "你怎么能这么怂";
  165. SetCOORD(24 * 2, 16);
  166. cout << "唉继续游戏吧!";
  167. }
  168. }
  169. SetCOORD(24 * 2, 2);
  170. cout << "你的得分为:" << grades;
  171. SetCOORD(coord.X * 2, coord.Y);
  172. cout << "★";
  173. mp[coord.Y][coord.X] = 3;
  174. CreateFood();
  175. COORD ss;
  176. ss = Snake.front();
  177. SetCOORD(ss.X * 2, ss.Y);
  178. cout << "●";
  179. Snake.push_front(coord);
  180. Mood();
  181. }
  182. else//没吃到食物
  183. {
  184. SetColor();
  185. SetCOORD(coord.X * 2, coord.Y);
  186. cout << "★";
  187. mp[coord.Y][coord.X] = 3;
  188. COORD ss;
  189. ss = Snake.front();
  190. SetCOORD(ss.X * 2, ss.Y);
  191. cout << "●";
  192. Snake.push_front(coord);
  193. ss = Snake.back();
  194. Snake.pop_back();
  195. mp[ss.Y][ss.X] = 0;
  196. SetCOORD(ss.X * 2, ss.Y);
  197. cout << " ";
  198. }
  199. if (grades * 10 == 250)
  200. {
  201. Sleep(250);
  202. }
  203. else
  204. Sleep(500-grades*10);
  205. }
  206. }
  207. }
  208. bool SnakeGame::GameOver(int x, int y)
  209. {
  210. if (mp[y][x] == 1||mp[y][x]==3)
  211. return true;
  212. else
  213. return false;
  214. }
  215. void SnakeGame::CreateFood()
  216. {
  217. int x, y;
  218. do
  219. {
  220. x = rand() % 20;
  221. y = rand() % 20;
  222. if (mp[y][x] == 0)
  223. {
  224. break;
  225. }
  226. } while (1);
  227. mp[y][x] = 2;//食物标记为2
  228. SetCOORD(x*2 , y);
  229. SetColor();
  230. cout << "★";
  231. }
  232. int SnakeGame::ChangeDir(char ch)
  233. {
  234. switch (ch)
  235. {
  236. case 'w':
  237. case 'W':return 1;
  238. case 'a':
  239. case 'A':return 2;
  240. case 's':
  241. case 'S':return 3;
  242. case 'd':
  243. case 'D':return 4;
  244. default:return dir;
  245. }
  246. return 0;
  247. }
  248. void SnakeGame::Mood()
  249. {
  250. int x = 25, y = 7;
  251. SetCOORD(x * 2, y);
  252. int k;
  253. k = rand() % 4;
  254. cout << q[k];
  255. }
  256. void SnakeGame::SetColor()
  257. {
  258. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  259. unsigned short x;
  260. x = rand() % 15+1;
  261. SetConsoleTextAttribute(hCon, (x) | (0));
  262. }
  263. //void SnakeGame::Create()
  264. //{
  265. //}
  266. void SnakeGame::CreateBarrier()
  267. {
  268. for (int i = 3; i <= 16; i++)
  269. {
  270. SetCOORD(i * 2, 4);
  271. mp[4][i] = 1;
  272. cout << "■";
  273. }
  274. for (int i = 3; i <= 16; i++)
  275. {
  276. SetCOORD(i * 2, 16);
  277. cout << "■";
  278. mp[16][i] = 1;
  279. }
  280. }
  281. void SnakeGame::SetCustom2()
  282. {
  283. for (int i = 3; i <= 6; i++)
  284. {
  285. SetCOORD(i * 2, 3);
  286. cout << "■";
  287. mp[3][i] = 1;
  288. SetCOORD(i * 2, 17);
  289. cout << "■";
  290. mp[17][i] = 1;
  291. SetCOORD(3*2, i);
  292. cout << "■";
  293. mp[i][3] = 1;
  294. SetCOORD(17 * 2, i);
  295. cout << "■";
  296. mp[i][17] = 1;
  297. }
  298. for (int i = 14; i <= 17; i++)
  299. {
  300. SetCOORD(3 * 2, i);
  301. cout << "■";
  302. mp[i][3] = 1;
  303. SetCOORD(17 * 2, i);
  304. cout << "■";
  305. mp[i][17] = 1;
  306. SetCOORD(i * 2, 17);
  307. cout << "■";
  308. mp[17][i] = 1;
  309. SetCOORD(i * 2, 3);
  310. cout << "■";
  311. mp[3][i] = 1;
  312. }
  313. for (int i = 9; i <= 10; i++)
  314. {
  315. SetCOORD(i * 2, 9);
  316. cout << "■";
  317. mp[9][i] = 1;
  318. SetCOORD(i * 2, 11);
  319. cout << "■";
  320. mp[11][i] = 1;
  321. }
  322. }
  323. void SnakeGame::init()
  324. {
  325. dir = 1;
  326. grades = 0;
  327. Custom = 0;
  328. Snake.clear();
  329. memset(mp, 0, sizeof(mp));
  330. return;
  331. }
  332. void SnakeGame::StartGame()
  333. {
  334. grades = 0;
  335. dir = 1;
  336. unsigned int t = clock();
  337. srand(t);
  338. CreateMp();
  339. CreateFood();
  340. dir = 1;//先向上走
  341. coord.X = 3;
  342. coord.Y = 7;
  343. mp[7][3] = 3;
  344. SetCOORD(coord.X * 2, coord.Y);
  345. printf("●");
  346. //system("pause");
  347. Snake.push_front(coord);
  348. while (!_kbhit())
  349. {
  350. }
  351. SetCOORD(6 * 2, 7);
  352. cout << " ";
  353. SetCOORD(6 * 2, 10);
  354. cout << " ";
  355. Move();
  356. }


主函数

  1. #include<stdio.h>
  2. #include"SnakeGame.h"
  3. int main()
  4. {
  5. SnakeGame a;//创建一个贪吃蛇游戏
  6. a.init();
  7. a.StartGame();
  8. system("pause");//系统暂停
  9. return 0;
  10. }



程序运行起来是这样子QWQ:







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

闽ICP备14008679号