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 }