C++ 简单的控制台贪吃蛇小游戏

  由于比较懒,所以不怎么写,觉得这样不应该。我应该对自己学的做出整理,不管是高端低端,写出来是自己的。

  1 // 贪吃蛇.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include
  6 #include"Windows.h"
  7 #include
  8 
  9 
 10 nData g_Snake[20];
 11 nData g_Food;
 12 
 13 using namespace std;
 14 int _tmain(int argc, _TCHAR* argv[])                                                            
 15 {
 16     int nKeyState = 0;
 17     int nSnakeMove = 5;
 18     int nSnakeLeng = 1;
 19     bool bSnake = false;
 20     
 21 
 22     //初始化食物
 23     srand((unsigned)time(NULL));
 24     g_Food.X = rand() % 18 + 1;
 25     g_Food.Y = rand() % 18 + 1;
 26     
 27     //初始化蛇头
 28 
 29     g_Snake[0].X = 5;
 30     g_Snake[0].Y = 6;
 31 
 32     while (true)
 33     {
 34         system("CLS");
 35         //开始菜单键盘输入
 36         if (nKeyState < 3)
 37         {
 38             if (KEY_DOWN(VK_DOWN))
 39             {
 40                 nKeyState++;
 41                 if (nKeyState > 2)
 42                 {
 43                     nKeyState = 0;
 44                 }
 45             }
 46             else if (KEY_DOWN(VK_UP))
 47             {
 48                 nKeyState--;
 49                 if (nKeyState < 0)
 50                 {
 51                     nKeyState = 2;
 52                 }
 53             }
 54             else if (KEY_DOWN(VK_RETURN))
 55             {
 56                 if (nKeyState == e_Menu_StartGame)
 57                 {
 58                     nKeyState = e_GO_StartGame;
 59                 }
 60             }
 61         }
 62         //蛇动作
 63         else if (nKeyState == e_GO_StartGame)
 64         {
 65             //备份蛇头
 66             g_Snake[0].XOD = g_Snake[0].X;
 67             g_Snake[0].YOD = g_Snake[0].Y;
 68 
 69             if (KEY_DOWN(VK_UP))
 70             {
 71                 if (nSnakeMove == e_Move_Down)
 72                 {
 73                     nSnakeMove = e_Move_Down;
 74                 }
 75                 else
 76                 {
 77                     nSnakeMove = e_Move_Up;
 78                 }
 79             }
 80             else if (KEY_DOWN(VK_DOWN))
 81             {
 82                 if (nSnakeMove == e_Move_Up)
 83                 {
 84                     nSnakeMove = e_Move_Up;
 85                 }
 86                 else
 87                 {
 88                     nSnakeMove = e_Move_Down;
 89                 }
 90             }
 91             else if (KEY_DOWN(VK_LEFT))
 92             {
 93 
 94                 if (nSnakeMove == e_Move_Right)
 95                 {
 96                     nSnakeMove = e_Move_Right;
 97                 }
 98                 else
 99                 {
100                     nSnakeMove = e_Move_Left;
101                 }
102             }
103             else if (KEY_DOWN(VK_RIGHT))
104             {
105                 if (nSnakeMove == e_Move_Left)
106                 {
107                     nSnakeMove = e_Move_Left;
108                 }
109                 else
110                 {
111                     nSnakeMove = e_Move_Right;
112                 }
113             }
114         }
115         switch (nSnakeMove)
116         {
117         case e_Move_Up: g_Snake[0].X--; break;
118         case e_Move_Down: g_Snake[0].X++; break;
119         case e_Move_Left: g_Snake[0].Y--; break;
120         case e_Move_Right: g_Snake[0].Y++; break;
121         }
122 
123         //蛇撞墙
124         if (nGameFrame[g_Snake[0].X][g_Snake[0].Y] == 1)
125         {
126             nKeyState = e_GO_GameOver;
127         }
128         //过关
129         if (nSnakeLeng == 20)
130         {
131             nKeyState = e_Go_WinGame;
132         }
133 
134 
135         //蛇吃到自己`
136         for (int i = 4; i <= nSnakeLeng; i++)
137         {
138             if (g_Snake[i].X == g_Snake[0].X&&g_Snake[i].Y == g_Snake[0].Y)
139             {
140                 nKeyState = e_GO_GameOver;
141             }
142         }
143 
144         //遍历蛇身位置
145         for (int i = 1; i < nSnakeLeng; i++)
146         {
147             g_Snake[i].XOD = g_Snake[i].X;
148             g_Snake[i].YOD = g_Snake[i].Y;
149             g_Snake[i].X = g_Snake[i - 1].XOD;
150             g_Snake[i].Y = g_Snake[i - 1].YOD;
151         }
152 
153         //吃到食物
154         if (g_Food.X==g_Snake[0].X&&g_Food.Y==g_Snake[0].Y)
155         {
156             srand((unsigned)time(NULL));
157             g_Food.X = rand() % 18 + 1;
158             g_Food.Y = rand() % 18 + 1;
159             nSnakeLeng++;
160         }
161 
162         //渲染.
163         if (nKeyState < 3)
164         {
165             cout << "■■■■■■■■■■■■■■■■■" << endl;
166             cout << "■■■■■■■■■■■■■■■■■" << endl;
167             cout << "■■■■                  ■■■■" << endl;
168             if (nKeyState == e_Menu_StartGame)
169             {
170                 cout << "■■■■   >-开始游戏     ■■■■" << endl;
171                 cout << "■■■■     游戏设置     ■■■■" << endl;
172                 cout << "■■■■     游戏结束     ■■■■" << endl;
173             }
174             else if (nKeyState == e_Menu_GameSet)
175             {
176                 cout << "■■■■     开始游戏     ■■■■" << endl;
177                 cout << "■■■■   >-游戏设置     ■■■■" << endl;
178                 cout << "■■■■     游戏结束     ■■■■" << endl;
179             }
180             else if (nKeyState == e_Menu_EndGame)
181             {
182                 cout << "■■■■     开始游戏     ■■■■" << endl;
183                 cout << "■■■■     游戏设置     ■■■■" << endl;
184                 cout << "■■■■   >-游戏结束     ■■■■" << endl;
185             }
186             cout << "■■■■                  ■■■■" << endl;
187             cout << "■■■■■■■■■■■■■■■■■" << endl;
188             cout << "■■■■■■■■■■■■■■■■■" << endl;
189         }
190         else if (nKeyState==e_GO_StartGame)
191         {
192             for (int i = 0; i < 20; i++)
193             {
194                 for (int a = 0; a < 20; a++)
195                 {
196                     for (int n = 1; n < nSnakeLeng; n++)
197                     {
198                         if (i == g_Snake[n].X&&a == g_Snake[n].Y)
199                         {
200                             bSnake = true;
201                             break;
202                         }
203                         else
204                         {
205                             bSnake = false;
206                         }
207                     }
208                     if (nGameFrame[i][a] == 1)
209                     {
210                         cout << "";
211                     }
212                     else if (i == g_Snake[0].X&&a == g_Snake[0].Y)
213                     {
214                         cout << "";
215                     }
216                     else if (bSnake == true)
217                     {
218                         cout << "";
219                     }
220                     else if (i == g_Food.X&&a == g_Food.Y)
221                     {
222                         cout << ""; 
223                     }
224                     else
225                     {
226                         cout << "  ";
227                     }
228                 }
229                 cout << endl;
230             }
231         }
232         else if (nKeyState == e_GO_GameOver)
233         {
234             cout << "GAME OVER"<<endl;
235         }
236         else if (nKeyState==e_Go_WinGame)
237         {
238             cout << "老铁666!"<<endl;
239         }
240 
241     }
242     return 0;
243 }
控制台贪吃蛇小游戏

 

转载于:https://www.cnblogs.com/kpxy/p/9581123.html

你可能感兴趣的:(C++ 简单的控制台贪吃蛇小游戏)