C++贪吃蛇

动画链接

GitHub链接:https://github.com/yanpeng1314/Snake

  1 #include "Snake.h"
  2 
  3 int iScore = 0;
  4 int iGrade = 1;
  5 
  6 //蛇头蛇尾初始位置
  7 int x_head = 1, y_head = 3;
  8 int x_tail = 1, y_tail = 1;
  9 
 10 //地图坐标
 11 int i_Map = 1, j_Map = 1;
 12 
 13 //第二节初始位置
 14 int x_second = 1, y_second = 2;
 15 
 16 //初始化移动方向
 17 int towards1 = 2; //原来方向
 18 int towards2; //按键按下后的方向
 19 
 20 const int SIDE = 20;
 21 int Snake_Map[SIDE][SIDE] = { 0 };
 22 
 23 int Speed = 300;
 24 
 25 
 26 
 27 
 28 list<Snake_Position> LIST;
 29 Snake_Position snake;
 30 
 31 
 32 
 33 int main()
 34 {
 35     Init();
 36     srand((unsigned)time(NULL));
 37     char getKeyboard = 'd';        //从键盘读取的键值
 38     char getKeyboard_Vice = 'd';    //副本: 如果读取其他值,则保持原来键值
 39     while (1)
 40     {
 41         int isSendFood = 1; //1--不发送食物  0--发送食物  
 42         int iFood;
 43         int x = rand() % 18 + 1;
 44         int y = rand() % 18 + 1;
 45         setColor(6);
 46         HiddenCursor();
 47         GOTO(y, x);
 48         cout << "";
 49         if (Snake_Map[x][y] != 2)
 50             Snake_Map[x][y] = 3;
 51         else
 52             continue;
 53 
 54         while (isSendFood)
 55         {
 56             if (_kbhit())
 57                 getKeyboard = _getch();
 58             if (getKeyboard != 's' && getKeyboard != 'S' && getKeyboard != 'a' && getKeyboard != 'A' && getKeyboard != 'w' && getKeyboard != 'W' && getKeyboard != 'd' && getKeyboard != 'D')
 59                 getKeyboard = getKeyboard_Vice;
 60             switch (getKeyboard)
 61             {
 62             case 'W':
 63             case 'w':
 64                 towards2 = 4;
 65                 if ((towards1 + towards2) == 5)
 66                 {
 67                     getKeyboard = getKeyboard_Vice;
 68                     break;
 69                 }
 70                 towards1 = towards2; //如果现在方向合理,则保存方向到towards1
 71                 x_head -= 1;
 72 
 73                 isDead();
 74                 if (Snake_Map[x_head][y_head] == 3)  //吃到食物
 75                 {
 76                     isSendFood = 0;
 77                     iScore += 1;
 78                     snake.x_snake = x_head;
 79                     snake.y_snake = y_head;
 80                     LIST.push_front(snake);
 81                     Snake_Map[x_head][y_head] = 2;
 82 
 83                     setColor(7);
 84                     HiddenCursor();
 85                     GOTO(y_head, x_head);
 86                     cout << "";
 87 
 88                 }
 89                 else
 90                 {
 91                     snake.x_snake = x_head;
 92                     snake.y_snake = y_head;
 93                     LIST.push_front(snake);
 94                     Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
 95 
 96                     setColor(7);
 97                     HiddenCursor();
 98                     GOTO(y_head, x_head);
 99                     cout << "";
100                     setColor(7);
101                     HiddenCursor();
102                     GOTO(LIST.back().y_snake, LIST.back().x_snake);
103                     cout << " ";
104 
105                     LIST.pop_back();
106                 }
107                 Snake_Map[x_head][y_head] = 2;
108                 Show_Score();
109                 Sleep(Speed);
110                 //Show_Snake();
111 
112                 getKeyboard_Vice = 'w';
113                 break;
114 
115             case 'S':
116             case 's':
117                 towards2 = 1;
118                 if ((towards1 + towards2) == 5)
119                 {
120                     getKeyboard = getKeyboard_Vice;
121                     break;
122                 }
123                 towards1 = towards2;
124                 x_head += 1;
125 
126                 isDead();
127                 if (Snake_Map[x_head][y_head] == 3)  //吃到食物
128                 {
129                     isSendFood = 0;
130                     iScore += 1;
131                     snake.x_snake = x_head;
132                     snake.y_snake = y_head;
133                     LIST.push_front(snake);
134                     Snake_Map[x_head][y_head] = 2;
135 
136 
137                     setColor(7);
138                     HiddenCursor();
139                     GOTO(y_head, x_head);
140                     cout << "";
141 
142 
143                 }
144                 else
145                 {
146                     snake.x_snake = x_head;
147                     snake.y_snake = y_head;
148                     LIST.push_front(snake);
149                     Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
150                     setColor(7);
151                     HiddenCursor();
152                     GOTO(y_head, x_head);
153                     cout << "";
154                     setColor(7);
155                     HiddenCursor();
156                     GOTO(LIST.back().y_snake, LIST.back().x_snake);
157                     cout << " ";
158                     LIST.pop_back();
159 
160                 }
161                 Snake_Map[x_head][y_head] = 2;
162                 Show_Score();
163                 Sleep(Speed);
164                 //Show_Snake();
165 
166                 getKeyboard_Vice = 's';
167                 break;
168 
169             case 'A':
170             case 'a':
171                 towards2 = 3;
172                 if ((towards1 + towards2) == 5)
173                 {
174                     getKeyboard = getKeyboard_Vice;
175                     break;
176                 }
177                 towards1 = towards2;
178 
179                 y_head -= 1;
180 
181                 isDead();
182                 if (Snake_Map[x_head][y_head] == 3)  //吃到食物
183                 {
184                     isSendFood = 0;
185                     iScore += 1;
186                     snake.x_snake = x_head;
187                     snake.y_snake = y_head;
188                     LIST.push_front(snake);
189                     Snake_Map[x_head][y_head] = 2;
190                     setColor(7);
191                     HiddenCursor();
192                     GOTO(y_head, x_head);
193                     cout << "";
194 
195                 }
196                 else
197                 {
198                     snake.x_snake = x_head;
199                     snake.y_snake = y_head;
200                     LIST.push_front(snake);
201                     Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
202                     setColor(7);
203                     HiddenCursor();
204                     GOTO(y_head, x_head);
205                     cout << "";
206                     setColor(7);
207                     HiddenCursor();
208                     GOTO(LIST.back().y_snake, LIST.back().x_snake);
209                     cout << " ";
210                     LIST.pop_back();
211 
212 
213                 }
214                 Snake_Map[x_head][y_head] = 2;
215                 Show_Score();
216                 Sleep(Speed);
217                 //Show_Snake();
218 
219                 getKeyboard_Vice = 'a';
220                 break;
221 
222             case 'D':
223             case 'd':
224                 towards2 = 2;
225                 if ((towards1 + towards2) == 5)
226                 {
227                     getKeyboard = getKeyboard_Vice;
228                     break;
229                 }
230                 towards1 = towards2;
231                 y_head += 1;
232 
233                 isDead();
234                 if (Snake_Map[x_head][y_head] == 3)  //吃到食物
235                 {
236                     isSendFood = 0;
237                     iScore += 1;
238                     snake.x_snake = x_head;
239                     snake.y_snake = y_head;
240                     LIST.push_front(snake);
241                     Snake_Map[x_head][y_head] = 2;
242                     setColor(7);
243                     HiddenCursor();
244                     GOTO(y_head, x_head);
245                     cout << "";
246 
247                 }
248                 else
249                 {
250                     snake.x_snake = x_head;
251                     snake.y_snake = y_head;
252                     LIST.push_front(snake);
253                     Snake_Map[LIST.back().x_snake][LIST.back().y_snake] = 0;
254                     setColor(7);
255                     HiddenCursor();
256                     GOTO(y_head, x_head);
257                     cout << "";
258                     setColor(7);
259                     HiddenCursor();
260                     GOTO(LIST.back().y_snake, LIST.back().x_snake);
261                     cout << " ";
262                     LIST.pop_back();
263 
264 
265                 }
266                 Snake_Map[x_head][y_head] = 2;
267                 Show_Score();
268                 Sleep(Speed);
269                 //Show_Snake();
270 
271                 getKeyboard_Vice = 'd';
272                 break;
273 
274             default:
275                 break;
276             }
277 
278         }
279     }
280 
281 
282     system("pause");
283     return 0;
284 }
285 
286 
287 void Init_Map()
288 {
289     for (int i = 0; i < SIDE; i++)
290     {
291         for (int j = 0; j < SIDE; j++)
292         {
293             if (i == 0 || i == SIDE - 1 || j == 0 || j == SIDE - 1)
294             {
295                 GOTO(i, j);
296                 cout << "";
297             }
298         }
299     }
300 }
301 
302 void Show_Snake()
303 {
304     for (int i = 1; i < SIDE - 1; i++)
305     {
306         for (int j = 1; j < SIDE - 1; j++)
307         {
308             if (Snake_Map[i][j] == 3)
309             {
310                 GOTO(j, i);
311                 cout << "";
312             }
313             if (Snake_Map[i][j] == 2)
314             {
315                 GOTO(j, i);
316                 cout << "";
317             }
318             if (Snake_Map[i][j] == 0)
319             {
320                 GOTO(j, i);
321                 cout << " ";
322             }
323         }
324     }
325 }
326 
327 void Init()
328 {
329     Init_Map();    //初始化显示地图
330     for (int i = 0; i < SIDE; i++)
331     {
332         for (int j = 0; j < SIDE; j++)
333         {
334             if (i == 0 || i == SIDE - 1 || j == 0 || j == SIDE - 1)
335                 Snake_Map[i][j] = 9;
336         }
337     }
338     //将蛇的初始三节坐标依次保存到LIST中
339     Snake_Map[1][1] = 2;
340     snake.x_snake = 1;
341     snake.y_snake = 1;
342     LIST.push_front(snake);
343 
344     Snake_Map[1][2] = 2;
345     snake.x_snake = 1;
346     snake.y_snake = 2;
347     LIST.push_front(snake);
348 
349     Snake_Map[1][3] = 2;
350     snake.x_snake = 1;
351     snake.y_snake = 3;
352     LIST.push_front(snake);
353 
354     Show_Snake();
355 }
356 
357 void isDead()
358 {
359     if (Snake_Map[x_head][y_head] == 9 || Snake_Map[x_head][y_head] == 2)  //死亡条件
360     {
361         system("cls");
362         cout << "你已经挂了, 游戏结束!" << endl;
363         Sleep(2000);
364         exit(-1);
365     }
366 }
367 
368 void Show_Score()
369 {
370     if (iScore == 5)
371     {
372         iGrade = 2;
373         Speed = 250;
374         //Sleep(2000);
375     }
376     if (iScore == 10)
377     {
378 
379         iGrade = 3;
380         Speed = 200;
381         //Sleep(2000);
382     }
383     if (iScore == 15)
384     {
385         setColor(4);
386         HiddenCursor();
387         GOTO(5, 5);
388         cout << "您已达到王者级别";
389         Sleep(5000);
390         exit(0);
391     }
392     setColor(4);
393     HiddenCursor();
394     GOTO(30, 8);
395     cout << "级别: " << iGrade;
396     GOTO(30, 12);
397     cout << "得分: " << iScore;
398 }
Main.cpp
 1 #include "Snake.h"
 2 using namespace std;
 3 
 4 
 5 void GOTO(int x, int y)
 6 {
 7     COORD wall{ 2 * x, y };
 8     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), wall);
 9 }
10 
11 void setColor(int color)
12 {
13     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
14 }
15 
16 
17 void HiddenCursor()
18 {
19     CONSOLE_CURSOR_INFO info;
20     info.dwSize = 1;
21     info.bVisible = FALSE;
22     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
23 }
Snake.cpp
 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <Windows.h>
 5 #include <time.h>
 6 #include <list>
 7 #include <conio.h>
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 #include <stack>
11 
12 using namespace std;
13 
14 class Snake_Position
15 {
16 public:
17     int x_snake;
18     int y_snake;
19 };
20 
21 //系统函数
22 void setColor(int color);
23 void GOTO(int x, int y);
24 void HiddenCursor();
25 
26 //自定义函数
27 void Init_Map();
28 void Show_Snake();
29 void Init();
30 void isDead();
31 void Show_Score();
Snake.h

 

转载于:https://www.cnblogs.com/yan1314/articles/8454276.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390196.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

远程办公招聘_招聘远程人才时要寻找的5种技能

远程办公招聘Remote work is a fast emerging segment of the labor market. How to embrace this shift as an employer - and find, recruit, and empower remote staff - is a question many companies and hiring managers are grappling with.远程工作是劳动力市场中快速崛…

10分钟腾讯云配置免费https

腾讯云免费证书申请地址&#xff1a; https://console.cloud.tencent... 填写相关信息 域名身份验证 文件验证 将fileauth.text 创建在网站访问根目录的 .well-known/pki-validation/目录使得 www.**.com/.well-known/pki-validation/fileauth.text 能够访问详情 等待5分钟左右…

1588. 所有奇数长度子数组的和

1588. 所有奇数长度子数组的和 给你一个正整数数组 arr &#xff0c;请你计算所有可能的奇数长度子数组的和。 子数组 定义为原数组中的一个连续子序列。 请你返回 arr 中 所有奇数长度子数组的和 。 示例 1&#xff1a; 输入&#xff1a;arr [1,4,2,5,3] 输出&#xff1…

洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)

题目描述 P教授要去看奥运&#xff0c;但是他舍不下他的玩具&#xff0c;于是他决定把所有的玩具运到北京。他使用自己的压缩器进行压缩&#xff0c;其可以将任意物品变成一堆&#xff0c;再放到一种特殊的一维容器中。P教授有编号为1...N的N件玩具&#xff0c;第i件玩具经过压…

680. 验证回文字符串 Ⅱ

680. 验证回文字符串 Ⅱ 给定一个非空字符串 s&#xff0c;最多删除一个字符。判断是否能成为回文字符串。 示例 1: 输入: s “aba” 输出: true 示例 2: 输入: s “abca” 输出: true 解释: 你可以删除c字符。 示例 3: 输入: s “abc” 输出: false 解题思路 使用…

Android--RxJava2更新体验

截止日前最新版2017-3-15: RxJava compile ‘io.reactivex:rxjava:1.2.7’ compile ‘io.reactivex:rxandroid:1.2.1’ RxJava2 compile “io.reactivex.rxjava2:rxjava:2.0.7” compile “io.reactivex.rxjava2:rxandroid:2.0.1” 1:create操作改变 Rxjava CompositeSubscri…

kotlin和java语言_Kotlin VS Java – 2020年您应该学习哪种编程语言?

kotlin和java语言It has been several years since Kotlin came out, and it has been doing well. Since it was created specifically to replace Java, Kotlin has naturally been compared with Java in many respects.自Kotlin问世以来已经有好几年了&#xff0c;而且一切…

oracle部署--安装oracle软件与部署单实例数据库

一、安装oracle数据库软件 1.创建相应的用户组及用户 groupadd oinstall groupadd oper groupadd dba useradd -g oinstall -G oper,dba oracle 2.创建oracle software安装路径 mkdir -p /u01/app/oracle/product/11.2.0/db_1 3.修改安装路径权限 chown -R oracle:oinstall …

web前端【第十一篇】jQuery属性相关操作

知识点总结 1、属性 属性&#xff08;如果你的选择器选出了多个对象&#xff0c;那么默认只会返回出第一个属性&#xff09;、 attr(属性名|属性值) - 一个参数是获取属性的值&#xff0c;两个参数是设置属性值 - 点击加载图片示例 re…

528. 按权重随机选择

528. 按权重随机选择 给定一个正整数数组 w &#xff0c;其中 w[i] 代表下标 i 的权重&#xff08;下标从 0 开始&#xff09;&#xff0c;请写一个函数 pickIndex &#xff0c;它可以随机地获取下标 i&#xff0c;选取下标 i 的概率与 w[i] 成正比。 例如&#xff0c;对于 w…

sql语句语法多表关联_SQL创建表语句-带有示例语法

sql语句语法多表关联SQL is one of the most reliable and straightforward querying languages around. It provides clear cut syntax that reads easily without abstracting away too much of the functionalitys meaning.SQL是最可靠&#xff0c;最直接的查询语言之一。 它…

分布式改造剧集三:Ehcache分布式改造

第三集&#xff1a;分布式Ehcache缓存改造 前言 ​ 好久没有写博客了&#xff0c;大有半途而废的趋势。忙不是借口&#xff0c;这个好习惯还是要继续坚持。前面我承诺的第一期的DIY分布式&#xff0c;是时候上终篇了---DIY分布式缓存。 探索之路 ​ 在前面的文章中&#xff0c;…

85. 最大矩形

85. 最大矩形 给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵&#xff0c;找出只包含 1 的最大矩形&#xff0c;并返回其面积。 示例 1&#xff1a; 输入&#xff1a;matrix [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”…

TP单字母函数

A方法 A方法用于在内部实例化控制器 调用格式&#xff1a;A(‘[项目://][分组/]模块’,’控制器层名称’) 最简单的用法&#xff1a; $User A(User); 表示实例化当前项目的UserAction控制器&#xff08;这个控制器对应的文件位于Lib/Action/UserAction.class.php&#xff09;…

Angular问题03 @angular/material版本问题

1 问题描述 应用使用 angular4在使用angular/material时&#xff0c;若果在导入模块时使用mat开头&#xff0c;就会报错。 2 问题原因 angular/material版本出现问题&#xff0c;angular/material 从版本5开始就必须要angular5的核心依赖&#xff1b;想要在angular5之前版本中的…

onclick判断组件调用_从子组件Onclick更新状态

onclick判断组件调用How to update the state of a parent component from a child component is one of the most commonly asked React questions.如何从子组件更新父组件的状态是最常见的React问题之一。 Imagine youre trying to write a simple recipe box application, …

Python 列表List的定义及操作

# 列表概念&#xff1a;有序的可变的元素集合# 定义 # 直接定义 nums [1,2,3,4,5]# 通过range函数构造&#xff0c;python2 和python3 版本之间的差异&#xff1b; # python3 用的时候才会去构造 nums range(1,101)# 列表嵌套 # 注意和C语言中数组的区别,是否可…

递归分解因数

题目总时间限制: 1000ms 内存限制: 65536kB描述给出一个正整数a&#xff0c;要求分解成若干个正整数的乘积&#xff0c;即a a1 * a2 * a3 * ... * an&#xff0c;并且1 < a1 < a2 < a3 < ... < an&#xff0c;问这样的分解的种数有多少。注意到a a也是一种分解…

剑指 Offer 51. 数组中的逆序对

剑指 Offer 51. 数组中的逆序对 在数组中的两个数字&#xff0c;如果前面一个数字大于后面的数字&#xff0c;则这两个数字组成一个逆序对。输入一个数组&#xff0c;求出这个数组中的逆序对的总数。 示例 1: 输入: [7,5,6,4] 输出: 5 限制&#xff1a; 0 < 数组长度 &…

react 图像识别_无法在React中基于URL查找图像

react 图像识别If youre new to React and are having trouble accessing images stored locally, youre not alone.如果您不熟悉React&#xff0c;并且无法访问本地存储的图像&#xff0c;那么您并不孤单。 Imagine you have your images stored in a directory next to a co…