贪食蛇

最近看大二的学长都在写课程设计,编出了好多游戏,觉得他们玩得好爽的,于是我也小试牛刀,编了一个小游戏贪食蛇,在玩的时候请玩家仔细阅读首页的游戏说明

ContractedBlock.gif ExpandedBlockStart.gif 贪食蛇
 
   
1 #include < stdio.h >
2 #include < stdlib.h >
3 #include < time.h >
4 #include < windows.h >
5 #include < conio.h >
6   #define Y1 32
7 #define X1 32
8 #define PL 5 // 通关所吃食物个数
9 void gaibian();
10 int shang();
11 int xia();
12 int zuo();
13 int you();
14 void print();
15 void origina();
16 void start();
17
18 int Length; // 当前长度
19 int C; // 记住已吃食物数量
20 int L; //
21 int speed;
22 int a[Y1][X1][ 3 ];
23 int d; // 记录蛇的方向
24 int x,y; // 坐标
25 int x0,y0; // 记录尾点坐标
26 int p,q; // 食物坐标
27 HANDLE hConsole;
28 char key;
29 char name[ 20 ]; // 记录输入者名称
30 void gotoxy( int x, int y)
31 {
32 COORD coord;
33 coord.X = x;
34 coord.Y = y;
35 SetConsoleCursorPosition(hConsole, coord);
36 }
37 int main()
38 {
39 int i,j,c;
40 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
41 SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
42 Length = 4 ; // 初始化长度
43 speed = 450 ; // 初始化速度
44 C = 0 ; // 初始化所吃东西数量
45 start();
46 for (;;)
47 {
48 srand(time(NULL)); /* 初始化随机数种子 */
49 x = rand() % (X1 - 2 * Length) + Length + 1 ; /* 生成一个Length--X1-Length区间内的整数 */
50 y = rand() % (Y1 - 2 * Length) + Length + 1 ; // 生成一个Length--Y1-Length区间内的整数
51 origina();
52 p = rand() % (X1 - 2 ) + 1 ;
53 q = rand() % (Y1 - 2 ) + 1 ;
54 a[q][p][ 0 ] = 4 ;
55 print();
56 for (;;)
57 {
58 while ( ! kbhit() ) // 键盘没有输入时
59 {
60
61 switch ( d )
62 {
63 case 0 :
64 i = shang();
65 break ;
66 case 1 :
67 i = xia();
68 break ;
69 case 2 :
70 i = zuo();
71 break ;
72 case 3 :
73 i = you();
74 break ;
75 }
76 if (C == PL) // 若达到升级长度
77 {
78 i = 2 ;
79 break ;
80 }
81 if ( i == 0 )
82 break ;
83 print(); // 输出敲键盘钱最后一次变动情况
84
85 }
86 if ( i == 0 ) // 如果撞墙 // i判断是否咬到自己
87 {
88 break ;
89 }
90 if (C == PL)
91 {
92 i = 2 ;
93 break ;
94 }
95 key = getch();
96 if (key == 13 )
97 {
98 i = 0 ;
99 break ;
100 }
101 switch (key)
102 {
103 case ' ' :
104 system( " pause " );
105 system( " cls " );
106 break ;
107 case - 32 :
108 key = getch();
109 switch (key)
110 {
111 case 72 :
112 i = shang();
113 break ;
114 case 80 :
115 i = xia();
116 break ;
117 case 75 :
118 i = zuo();
119 break ;
120 case 77 :
121 i = you();
122 break ;
123 }
124 break ;
125 }
126 if (i == 0 ) // 撞墙
127 {
128 break ;
129 }
130 print();
131 }
132 if (i == 0 ) // 若挂了
133 {
134 system( " pause " );
135 system( " cls " );
136 printf( " \n\n\n\n\n\n\n\n\n\n\t\t\t\t\t您的游戏结束了\n\n\n\n\t\t\t " );
137 printf( " 您是想 :\n\t\t\t\t " );
138 printf( " 1 继续 2 结束\n\t\t\t\t " );
139 fflush( stdin );
140 scanf( " %d " , & c );
141 while (c != 1 && c != 2 )
142 {
143 printf( " \n\n\n\t\t\t您的输入有误,请重新输入\n\t\t\t\t " );
144 system( " pause " );
145 system( " cls " );
146 printf( " \n\n\n\n\n\n\n\n\t\t\t您是想 :\n\t\t\t\t " );
147 printf( " 1 继续 2 结束\n\t\t\t\t " );
148 scanf( " %d " , & c );
149 }
150 if (c == 2 )
151 return 0 ;
152 }
153 if ( i == 2 ) // 若通关
154 {
155 system( " cls " );
156 printf( " \n\n\n\n\n\n\n\t\t\t这小子真牛!过第%d关了 " , 10 - speed / 50 );
157 if ( 10 - speed / 50 > 8 )
158 {
159 printf( " 恭喜您过全关了!您将获得李立为你颁奖的机会!!\n\n\t\t\t\t\t呵呵!按任意键结束!!\n\n\t\t\t\t " );
160 system( " pause " );
161 return 0 ;
162 system( " cls " );
163 }
164 printf( " \n\n\t\t\t接下来进入第%d关,这关难度加大你要小心噢!\n\n\n\t\t\t " , 10 - (speed = speed - 50 ) / 50 );
165 i = 1 ;
166 C = 0 ;
167 system( " pause " );
168 system( " cls " );
169 }
170 }
171
172 return 0 ;
173 }
174 void gaibian()
175 {
176 int i,j;
177 a[y0][x0][ 0 ] = 0 ;
178 a[y0][x0][ 1 ] = 0 ;
179 if (a[y0 - 1 ][x0][ 1 ] == Length - 1 )
180 -- y0;
181 else if (a[y0 + 1 ][x0][ 1 ] == Length - 1 )
182 ++ y0;
183 else if (a[y0][x0 - 1 ][ 1 ] == Length - 1 )
184 -- x0;
185 else if (a[y0][x0 + 1 ][ 1 ] == Length - 1 )
186 ++ x0;
187 for (i = 0 ;i < Y1;i ++ )
188 {
189 for (j = 0 ;j < X1;j ++ )
190 {
191 if (a[i][j][ 1 ] != 0 ) // 对蛇头的处理
192 a[i][j][ 1 ] ++ ;
193 }
194 }
195 }
196 int shang()
197 {
198 int i,j;
199 if (d == 1 ) // 防止倒退
200 return 1 ;
201 if (y - 1 == q && p == x) // 若向上吃到食物
202 {
203 Length ++ ;
204 for (i = 0 ;i < X1;i ++ ) // 改变前驱
205 {
206 for (j = 0 ;j < Y1;j ++ )
207 {
208 if (a[i][j][ 1 ] != 0 )
209 a[i][j][ 1 ] ++ ;
210 }
211 }
212 a[q][p][ 1 ] = 1 ; // 改变加长后的最前驱
213 a[q][p][ 0 ] = 1 ; // 改变加长后蛇头
214 a[y][x][ 0 ] = 2 ; // 改变原来蛇头为蛇身
215 y -- ; // 新蛇头
216 return 1 ;
217 }
218 gaibian();
219 if (a[y - 1 ][x][ 0 ] == 2 || (y - 1 ) == 0 ) // 若咬到自己或者碰墙
220 return 0 ;
221 a[y][x][ 0 ] = 2 ; // 用蛇身覆盖原来的蛇头
222 y -- ;
223 a[y][x][ 1 ] = 1 ;
224 a[y][x][ 0 ] = 1 ;
225 d = 0 ;
226 return 1 ;
227 }
228 int xia()
229 {
230 int i,j;
231 if (d == 0 )
232 return 1 ;
233 if (y + 1 == q && p == x) // 若向上吃到食物
234 {
235 Length ++ ;
236 for (i = 0 ;i < X1;i ++ ) // 改变前驱
237 {
238 for (j = 0 ;j < Y1;j ++ )
239 {
240 if (a[i][j][ 1 ] != 0 )
241 a[i][j][ 1 ] ++ ;
242 }
243 }
244 a[q][p][ 1 ] = 1 ; // 改变加长后的最前驱
245 a[q][p][ 0 ] = 1 ; // 改变加长后蛇头
246 a[y][x][ 0 ] = 2 ; // 改变原来蛇头为蛇身
247 y ++ ; // 新蛇头
248 return 1 ;
249 }
250 gaibian();
251 if (a[y + 1 ][x][ 0 ] == 2 || (y + 1 ) == Y1 - 1 ) // 防止因尾部变化而产生不良后果
252 return 0 ;
253 a[y][x][ 0 ] = 2 ;
254 y ++ ;
255 a[y][x][ 1 ] = 1 ;
256 a[y][x][ 0 ] = 1 ;
257 d = 1 ;
258 return 1 ;
259 }
260 int zuo()
261 {
262 int i,j;
263 if (d == 3 )
264 return 1 ;
265 if (y == q && p == x - 1 ) // 若向上吃到食物
266 {
267 Length ++ ;
268 for (i = 0 ;i < X1;i ++ ) // 改变前驱
269 {
270 for (j = 0 ;j < Y1;j ++ )
271 {
272 if (a[i][j][ 1 ] != 0 )
273 a[i][j][ 1 ] ++ ;
274 }
275 }
276 a[q][p][ 1 ] = 1 ; // 改变加长后的最前驱
277 a[q][p][ 0 ] = 1 ; // 改变加长后蛇头
278 a[y][x][ 0 ] = 2 ; // 改变原来蛇头为蛇身
279 x -- ; // 新蛇头
280 return 1 ;
281 }
282 gaibian();
283 if (a[y][x - 1 ][ 0 ] == 2 || (x - 1 ) == 0 )
284 return 0 ;
285 a[y][x][ 0 ] = 2 ;
286 x -- ;
287 a[y][x][ 1 ] = 1 ;
288 a[y][x][ 0 ] = 1 ;
289 d = 2 ;
290 return 1 ;
291 }
292 int you()
293 {
294 int i,j;
295 if (d == 2 )
296 return 1 ;
297 if (y == q && p == x + 1 ) // 若向上吃到食物
298 {
299 Length ++ ;
300 for (i = 0 ;i < X1;i ++ ) // 改变前驱
301 {
302 for (j = 0 ;j < Y1;j ++ )
303 {
304 if (a[i][j][ 1 ] != 0 )
305 a[i][j][ 1 ] ++ ;
306 }
307 }
308 a[q][p][ 1 ] = 1 ; // 改变加长后的最前驱
309 a[q][p][ 0 ] = 1 ; // 改变加长后蛇头
310 a[y][x][ 0 ] = 2 ; // 改变原来蛇头为蛇身
311 x ++ ; // 新蛇头
312 return 1 ;
313 }
314 gaibian();
315 if (a[y][x + 1 ][ 0 ] == 2 || (x + 1 ) == X1 - 1 )
316 return 0 ;
317 a[y][x][ 0 ] = 2 ;
318 x ++ ;
319 a[y][x][ 1 ] = 1 ;
320 a[y][x][ 0 ] = 1 ;
321 d = 3 ;
322 return 1 ;
323 }
324 void origina( )
325 {
326 int i,j;
327 for (i = 0 ;i < Y1;i ++ ) // 对数组进行初始化
328 {
329 for (j = 0 ;j < X1;j ++ )
330 {
331 a[i][j][ 0 ] = 0 ;
332 a[i][j][ 1 ] = 0 ;
333 }
334 }
335 a[y][x][ 0 ] = 1 ;
336 a[y][x][ 1 ] = 1 ;
337 srand(time(NULL));
338 d = rand() % 4 ;
339 if (d == 0 )
340 {
341 x0 = x;
342 y0 = y + Length - 1 ;
343 for (i = 1 ;i < Length;i ++ )
344 {
345 a[y + i][x][ 0 ] = 2 ;
346 a[y + i][x][ 1 ] = i + 1 ;
347 }
348 }
349 else if (d == 1 )
350 {
351 x0 = x;
352 y0 = y - Length + 1 ;
353 for (i = 1 ;i < Length;i ++ )
354 {
355 a[y - i][x][ 0 ] = 2 ;
356 a[y - i][x][ 1 ] = i + 1 ;
357 }
358 }
359 else if (d == 2 )
360 {
361 x0 = x + Length - 1 ;
362 y0 = y;
363 for (i = 1 ;i < Length;i ++ )
364 {
365 a[y][x + i][ 0 ] = 2 ;
366 a[y][x + i][ 1 ] = i + 1 ;
367 }
368 }
369 else if (d == 3 )
370 {
371 x0 = x - Length + 1 ;
372 y0 = y;
373 for (i = 1 ;i < Length;i ++ )
374 {
375 a[y][x - i][ 0 ] = 2 ;
376 a[y][x - i][ 1 ] = i + 1 ; // a[i][j][1]来记住前驱的位置
377 }
378 }
379 }
380
381 void print()
382 {
383 gotoxy( 0 , 2 );
384 printf( " \t\t\t\t\t\t贪食蛇\n\n\n " );
385 int i,j,k; // 用k来随时改变前驱的值
386 for (i = 0 ,k = 2 ;i < Y1;i ++ )
387 {
388
389 printf( " \t\t■ " );
390 for (j = 1 ;j < X1 - 1 ;j ++ )
391 {
392 if (i == 0 || i == Y1 - 1 )
393 printf( " " );
394 else
395 {
396 if (a[i][j][ 0 ] == 1 )
397 {
398 printf( " " );
399 }
400 else if (a[i][j][ 0 ] == 2 )
401 {
402 printf( " " );
403 }
404 else if (a[i][j][ 0 ] == 4 )
405 printf( " " );
406 else
407 printf( " " );
408 }
409
410 }
411 printf( " " );
412 if (i == 5 )
413 printf( " \t 玩家: %s " ,name );
414 if (i == 9 )
415 printf( " \t长度: %d " ,Length );
416 if (i == 13 )
417 printf( " \t已吃食物: %d " ,C );
418 if (i == 17 )
419 printf( " \t通关所需食物: %d " ,PL );
420 if (i == 21 )
421 printf( " \t等级: %d " , 10 - speed / 50 );
422 if (i == 24 )
423 printf( " \t按space键可以暂停 " );
424 if (i == 27 )
425 printf( " \t按回车结束 " );
426 puts( "" );
427 if (a[q][p][ 0 ] == 1 )
428 {
429 C ++ ; // 所吃数量加1
430 while (a[q][p][ 0 ] != 0 ) // 防止食物出现在蛇身
431 {
432 p = rand() % (X1 - 2 ) + 1 ;
433 q = rand() % (Y1 - 2 ) + 1 ;
434 }
435 a[q][p][ 0 ] = 4 ;
436 }
437 }
438 // printf( "\t\t\t%d %d\n\t\t",x,y );
439 Sleep(speed);
440 }
441 void start()
442 {
443 int c,l,s;
444 printf( " \n\n\t\t\t欢迎来玩李立的贪食蛇!\n\n " );
445 printf( " \t在玩之前请仔细阅读说明书:\n\n " );
446 printf( " \t\t\t\t1 请不要按着键盘不松\n\n\n\n " );
447 printf( " \t\t\t\t2 两次按键盘的时间间隔请不要太短\n\n\n\n " );
448 printf( " \t\t\t注意:在进入游戏之前请先设置你的窗口大小 方法如下\n\n\n\n " );
449 printf( " \t\t\t\t 1 当运行时,右点击运行窗口,选择属性,然后选择布局\n\n\n\n " );
450 printf( " \t\t\t\t 2 屏幕缓冲区分别为 宽 110 高 300\n\n\n\n " );
451 printf( " \t\t\t\t 3 窗口大小 宽 110 高 45 " );
452 printf( " \n\n\n\t\t\t " );
453 system( " pause " );
454 system( " cls " );
455 printf( " \n\n\n\n\t\t\t请输入您的姓名: " );
456 scanf( " %s " ,name );
457 printf( " \n\n\n\t\t\t " );
458 system( " pause " );
459 system( " cls " );
460 for (;;)
461 {
462 printf( " \n\n\n\n\n\t\t\t\t 1 进入游戏 " );
463 printf( " \n\n\t\t\t\t 2 设置\n\n\t\t\t\t " );
464 scanf( " %d " , & c );
465 while ( c != 1 && c != 2 )
466 {
467 printf( " \n\t\t\t您的输入有误,请重新输入\n\t\t\t " );
468 system( " pause " );
469 system( " cls " );
470 printf( " \n\n\n\n\n\t\t\t\t 1 进入游戏 " );
471 printf( " \n\n\t\t\t\t 2 设置\n\n\t\t\t\t " );
472 scanf( " %d " , & c );
473 }
474 if (c == 1 )
475 break ;
476 if ( c == 2 )
477 {
478 for (;;)
479 {
480 system( " cls " );
481 printf( " \n\n\n\n\t\t\t\t 1 初始长度 " );
482 printf( " \n\n\t\t\t\t 2初始等级\n\t\t\t\t " );
483 printf( " \n\t\t\t\t 3完成\n\t\t\t\t " );
484 scanf( " %d " , & c );
485 while ( c != 1 && c != 2 && c != 3 )
486 {
487 printf( " \n\t\t\t\t您的输入有误,请重新输入\n\t\t\t " );
488 system( " pause " );
489 system( " cls " );
490 printf( " \n\n\n\n\t\t\t\t 1 初始长度 " );
491 printf( " \n\n\t\t\t\t 2初始等级\n\t\t\t\t " );
492 printf( " \n\t\t\t\t 3完成\n\t\t\t\t " );
493 scanf( " %d " , & c );
494 }
495 if (c == 1 )
496 {
497 system( " cls " );
498 printf( " \n\n\n\n\t\t\t\t请输入初始长度 ( 2--8 )\n\t\t\t\t " );
499 scanf( " %d " , & l );
500 while ( l < 2 || l > 8 )
501 {
502 printf( " \n\t\t\t您的输入有误请重新输入\n\t\t\t " );
503 system( " pause " );
504 system( " cls " );
505 printf( " \n\n\n\n\t\t\t\t请输入初始长度 ( 2--8 )\n\t\t\t\t " );
506 scanf( " %d " , & l );
507 }
508 Length = l;
509 }
510 if (c == 2 )
511 {
512 system( " cls " );
513 printf( " \n\n\n\n\t\t\t\t请输入初始等级 (1——10)\n\t\t\t\t " );
514 scanf( " %d " , & s );
515 while ( s < 1 || s > 10 )
516 {
517 printf( " \n\t\t\t\t您的输入有误,请重新输入\n\t\t\t " );
518 system( " pause " );
519 system( " cls " );
520 printf( " \n\n\n\n\t\t\t\t请输入初始等级 (1——10)\n\t\t\t\t " );
521 scanf( " %d " , & s );
522 }
523 speed = ( 10 - s) * 50 ;
524 }
525 if (c == 3 )
526 {
527 system( " cls " );
528 break ;
529 }
530 printf( " \n\t\t\t\t设置成功\n\t\t\t\t " );
531 system( " pause " );
532 system( " cls " );
533 }
534 }
535 }
536 return ;
537 }
538
539

 

转载于:https://www.cnblogs.com/3498/archive/2011/01/10/1932341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值