C语言实现控制台版贪吃蛇游戏

时间:2022-12-03 16:19:13

用c语言写的期末作业:C语言实现控制台版贪吃蛇游戏的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
{
 conio.h
 阻塞式: getch():   从无回显的控制台获取字符。无缓冲区,只有当按下一个键才会执行后面的程序。
 非阻塞式: kbhit()    检测缓冲区中是否有字符;执行该函数后程序不会停下,而是继续执行下面的代码
 
 由于getch()和kbhit()已弃用,
 在编译时会产生警告,
 可以用_getch()和_kbhit()替换它们,
 或者在包含头文件前加上#pragma warning(disable : 4996)关闭警告
}
 
 
 
*/
#pragma warning(disable : 4996)   //用来关闭警告
#include<stdio.h>
#include <stdlib.h>
#include <conio.h>   //_kbhit()函数获取键盘事件
#include <time.h>
#include<Windows.h>
 
 
#define WIN_HEIGH  30   //窗口高度
#define WIN_WIDTH 105   //窗口宽度
#define G_HEIGHT  30   //游戏界面高度
#define G_WIDTH 80    //游戏界面宽度
#define STATUS int    //函数返回状态属性
#define SUCCESS 1    //执行成功
#define FAIL 0     //执行失败
 
 
 
typedef struct snake_node{    //蛇的某个点的信息
 char direct;    //即将前进的方向  udlr分别对应上下左右,刚吃掉的身体部分方向为o
 COORD postion;    //该点所在位置
 struct snake_node* next_node;  //下个节点的指针
}*Ps_node,S_node;
 
typedef struct snake {
 char head_type;   //蛇头的样子
 char body_type;   //蛇身的样子
 WORD color;    //颜色
 int level;    //等级
 int speed;    //速度
 int score;    //成绩
 int control;   //控制器 即按wasd进行移动,或者按上下左右进行移动
 S_node head;   //蛇头
}*Psnake,Snake;    //蛇结构
 
 
 
// 0输出空格 -1输出#
char canvas[G_HEIGHT][G_WIDTH] ; //画布
int bean_count;      //已创建食物的数量
POINT reward_position;    //奖励的位置
int reward_time = -1000;   //奖励剩余时间
int run_snake_threads;    //正在运行的蛇的数量
char keyboard_out[2];    //键盘的输出控制器 keyboard_out[0],代表控制器1,keyboard_out[1],代表控制器2,
 
int init_snake_speed = 400;   //蛇的初始速度 ,例=400时,蛇0.4s前进一步
 
//关于颜色取值,请查看help.txt
void gotoxy(int x, int y);           //设置光标到指定位置
void setcolor(WORD wAttributes);         //设置输出颜色
void prc_toxy(char c, int x, int y);        //指定位置输出字符
void prs_toxy(char* str, int x, int y);        //指定位置输出字符串
void prc_colortoxy(char str, int x, int y, WORD wAttributes);  //在指定位置,以wAttributes颜色输出字符
void prs_colortoxy(char* str, int x, int y, WORD wAttributes);  //在指定位置,以wAttributes颜色输出字符字符串
 
 
void one_play_mod();  //进入单人模式
void two_play_mod();  //进入双人模式
 
 
/***********************
 
 函数名:init_system
 功能:
   获取窗口句柄
   设置控制台颜色和大小
   隐藏光标显示
 
************************/
void init_system();
 
 
 
/***********************
 
 函数名:init_snode
 功能:初始化新的身体节点
 输入参数:
  direct:这个节点的下一个运动方向;
  snake_x,snake_y:这个节点的位置(x,y)
  next_node:下一个节点的指针
 返回值:
  Ps_node:返回已初始化的节点的指针
 
************************/
Ps_node init_snode(Ps_node n_snode,char drect,int x,int y,Ps_node next_node);
 
 
/************************
 
 函数名:init_snake
 功能:初始化蛇的相关参数,并为其创建身体,长度为三
 输入参数:
   she:  要初始化的蛇指针
   color:  蛇的颜色
   head_type:  用该字符显示蛇的身体
   x,y:  蛇最开始的位置
   direct:  默认前进方向
   control: 移动蛇的方式:默认1为WASD键,2为方向键盘上下左右键
 返回值:初始化后的蛇指针
 
**************************/
Psnake init_snake(Psnake she ,WORD color,char head_type,char body_type,int x,int y, char direct, int control);  //初始化小蛇
 
 
DWORD WINAPI Run_Snake_Thread(LPVOID lpParam);    //线程:运行一条蛇
DWORD WINAPI Del_reward_Thread(LPVOID lpParam);    //线程:生成奖励后,一段时间内不迟到奖励会消失
DWORD WINAPI Render_Thread(LPVOID lpParam);     //线程:用来渲染,游戏运行时,所有的打印显示由该进程完成
DWORD WINAPI Keyboard_Thread(LPVOID lpParam);    //线程:游戏运行时,监听按键输入,并设置keyboard_out
 
 
 
 
/************************
 
 函数名:get_canvas_toxy
 功能: 获取画布上该点的值
 输入参数:postion
 返回值:char类型,画布上该点的值
 
************************/
char get_canvas_toxy(COORD postion);
 
 
 
/***********************
 
 函数名:snode_gonext
 功能: 蛇身体上的点移动到下个位置
 输入参数:
  snode:要移动的点
  direct:要移动的方向
 返回值:char ,未移动前,该点要前进的方向
************************/
char snode_gonext(Ps_node snode, char direct);
 
 
 
/***********************
 
 函数名:snake_gonext
 功能: 蛇移动到下个位置
 输入参数:
  she:要移动的蛇
  direct:蛇头移动的方向
 返回值:无
************************/
void snake_gonext(Psnake she, char direct);
 
 
/********************
 
 函数名:init_canvas
 功能: 初始化画布,并为画布设置边框
 输入参数: 无
 返回值: 无
 
********************/
void init_canvas();
 
 
void draw_wall();      //在canvas上设置边框位置
 
 
/***********************
 
 函数名:snaketocanvas
 功能:将蛇的坐标信息拷贝到画布canvas上,在canvas上删除原来的位置信息,
 输入参数:she :要拷贝的蛇的指针
 
***********************/
void snaketocanvas(Psnake she);
 
/**************************
 
 函数名:eat_node
 功能:蛇吃食物,并把它加入蛇身,并增加成绩,触发升级加速
  (注:加入到蛇身后该点的位置还在原来的地方,需要身体都经过那里后长度才会增加)
 输入参数:
   she:吃食物的那条蛇
   position:食物的位置
 
**************************/
void eat_node(Psnake she, COORD postion);
 
 
 
/**********************
 
 函数名:create_bean
 功能:在画布的一个随机位置创建豆
 
 返回值:
   status:返回是否创建成功
**********************/
STATUS createrand_bean();
 
/**********************
 
 函数名:free_snake
 功能:释放小蛇的内存
 输入参数:
   she:要释放内存的小蛇
 
************************/
void free_snake(Psnake she);
 
void  menu();   //开始菜单, 用来选择模式,单人或者双人
 
void show_menu(int flag);  //根据不同的flag显示不同的菜单
 
HANDLE handle;
int main() {
 init_system();
 menu();
 return 0;
}
void init_system() {
 system("color F0 ");
 handle = GetStdHandle(STD_OUTPUT_HANDLE);//获取窗口句柄
 char chCmd[32];
 sprintf(chCmd, "mode con cols=%d lines=%d", WIN_WIDTH, WIN_HEIGH);  //设置控制台大小
 system(chCmd);
 CONSOLE_CURSOR_INFO cursor_info = { 1,0 };   //设置光标不可见,第一个值为光标厚度,第二个值为光标是否显示
 SetConsoleCursorInfo(handle, &cursor_info);   //
 srand((int)time(NULL));        //初始化随机数种子
}
void menu() {
 int ch1, ch2;
 int flag = 1;  //标志当前选择模式
 show_menu(flag);
 while (1) {
  ch1 = _getch();
  if (ch1 == 13) {
   printf("此处是进入函数\n");
   if (flag == 1)
    one_play_mod();
   else if (flag == 2)
    two_play_mod();
   else {
    system("cls");
    printf("退出\n");
    break;
   }
  }
  if (ch1==224) {
   ch2 = _getch();
   if (ch2 == 72) {
    flag = (flag + 2)% 3;
   }
   else if (ch2 == 80) {
    flag = (flag + 4) % 3;
   }
  }
  show_menu(flag);
 }
}
void show_menu(int flag) {
 char gamename[30] = "贪      吃      蛇";
 char mod1[20] = "单人模式";
 char mod2[20] = "双人模式";
 char modexit[20] = "退出";
 int xstr = (WIN_WIDTH - strlen(mod1)) / 2;   //要打印选项的位置x列
 int ystr;           //要打印选项的位置y行
 WORD color[3] = {0xf0,0xf0,0xf0};           //要设置的颜色
 system("cls");
 setcolor(0xf0);
 prs_toxy(gamename, (WIN_WIDTH - strlen(gamename) )/ 2, 5);
 color[flag] = FOREGROUND_RED | 0xf0;
 prs_colortoxy(mod1, (WIN_WIDTH - strlen(mod1)) / 2, 11, color[1]);
 prs_colortoxy(mod2, (WIN_WIDTH - strlen(mod2)) / 2, 13, color[2]);
 prs_colortoxy(modexit, (WIN_WIDTH - strlen(modexit)) / 2, 15, color[0]);
 if (flag == 1) ystr = 11; else if (flag == 2) ystr = 13; else ystr = 15;  //设置箭头位于哪一行
 prs_colortoxy("→", xstr - 4, ystr, color[flag]);
 gotoxy(0, 0);
}
void gotoxy(int x, int y) {
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle, pos);
}
 
void setcolor(WORD wAttributes) {
 SetConsoleTextAttribute(handle, wAttributes);
}
void prc_toxy(char c, int x, int y) {
 gotoxy(x, y);
 printf("%c", c);
}
 
void prs_toxy(char* str, int x, int y) {
 gotoxy(x, y);
 printf("%s", str);
}
void prc_colortoxy(char c, int x, int y, WORD wAttributes) {
 gotoxy(x, y);
 SetConsoleTextAttribute(handle, wAttributes);
 printf("%c", c);
}
void prs_colortoxy(char* str, int x, int y, WORD wAttributes) {
 gotoxy(x, y);
 SetConsoleTextAttribute(handle, wAttributes);
 printf("%s", str);
}
Ps_node init_snode(Ps_node n_snode, char drect, int x, int y, Ps_node next_node) {
 n_snode->direct = drect;
 n_snode->postion.X = x;
 n_snode->postion.Y = y;
 n_snode->next_node = next_node;
 return n_snode;
}
Psnake init_snake(Psnake she , WORD color, char head_type, char body_type, int x, int y, char direct,int control) {
 int ax=x,ay=y,bx=x,by=y;
 she->head_type = head_type;
 she->body_type = body_type;
 she->color = color;
 she->control = control;
 she->level = 1;
 she->speed = init_snake_speed;
 she->score = 0;
 Ps_node s1 = (Ps_node)malloc(sizeof(S_node));
 Ps_node s2 = (Ps_node)malloc(sizeof(S_node));
 switch (direct)
 {
 case 'l':
  ax = x + 2; bx = x + 1;
  break;
 case 'r':
  ax = x - 2; bx = x - 1;
  break;
 case 'u':
  ay = y + 2; by = y + 1;
  break;
 case 'd':
  ay = y - 2; by = y - 1;
  break;
 default:
  break;
 }
 init_snode(s2, direct, ax, ay, NULL);     //要根据方向设置下一个节点的位置
 init_snode(s1, direct, bx , by, s2);
 init_snode(&she->head, direct, x, y,s1);
 return she;
}
void one_play_mod() {
 HANDLE hThread;
 HANDLE hThread_render;
 HANDLE hT_getkey;
 Snake xiaoshe;
 system("cls");
 bean_count = 0;
 init_canvas();
 init_snake(&xiaoshe,0xf2, '@','*', G_WIDTH/2, G_HEIGHT/2, 'r', 2);
 snaketocanvas(&xiaoshe);
 createrand_bean();
 run_snake_threads = 1;
 draw_wall();
 hThread = CreateThread(NULL, 0, Run_Snake_Thread,(LPVOID)&xiaoshe, 0, NULL);
 hThread_render = CreateThread(NULL, 0, Render_Thread, (LPVOID)&xiaoshe, 0, NULL);
 hT_getkey= CreateThread(NULL, 0,Keyboard_Thread, NULL, 0, NULL);
 WaitForSingleObject(hThread, INFINITE);
 run_snake_threads = 0;
 WaitForSingleObject(hT_getkey, INFINITE);
 WaitForSingleObject(hThread_render, INFINITE);
 CloseHandle(hThread);
 CloseHandle(hT_getkey);
 CloseHandle(hThread_render);
 gotoxy(G_WIDTH / 2-2, G_HEIGHT / 2);
 printf("游戏结束\n");
 Sleep(2000);
}
void two_play_mod() {
 HANDLE hThread_a;
 HANDLE hThread_b;
 HANDLE hThread_render;
 HANDLE hT_getkey;
 Snake she[2];
 system("cls");
 bean_count = 0;
 init_canvas();
 init_snake(&she[0], 0xf2, '@', '*', G_WIDTH / 2-4, G_HEIGHT / 2 -4, 'r', 2);
 init_snake(&she[1], 0xE5, 'U', 'S', G_WIDTH / 2, G_HEIGHT / 2, 'l', 1);
 snaketocanvas(&she[0]);
 snaketocanvas(&she[1]);
 draw_wall();
 createrand_bean();
 /*createrand_bean();  //创建多个食物
 createrand_bean();
 createrand_bean();*/
 run_snake_threads = 2;
 hThread_b = CreateThread(NULL, 0, Run_Snake_Thread, (LPVOID)&she[0], 0, NULL);
 hThread_a = CreateThread(NULL, 0, Run_Snake_Thread, (LPVOID)&she[1], 0, NULL);
 hThread_render= CreateThread(NULL, 0, Render_Thread, (LPVOID)&she, 0, NULL);
 hT_getkey = CreateThread(NULL, 0, Keyboard_Thread, NULL, 0, NULL);
 WaitForSingleObject(hThread_a, INFINITE);
 WaitForSingleObject(hThread_b, INFINITE);
 run_snake_threads = 0;
 WaitForSingleObject(hT_getkey, INFINITE);
 WaitForSingleObject(hThread_render, INFINITE);
 CloseHandle(hThread_a);
 CloseHandle(hThread_b);
 CloseHandle(hT_getkey);
 CloseHandle(hThread_render);
 gotoxy(G_WIDTH / 2, G_HEIGHT / 2);
 printf("游戏结束\n");
 Sleep(1000);
}
DWORD WINAPI Keyboard_Thread(LPVOID lpParam) {
 int ch1;
 int ch2;
 keyboard_out[0] = 'n'; keyboard_out[1] = 'n'
 while (run_snake_threads > 0) {
  while (_kbhit())
  {
   ch1 = _getch();
   switch (ch1)
   {
   case 'w':
    keyboard_out[0] = 'u';
    break;
   case 's':
    keyboard_out[0] = 'd';
    break;
   case 'd':
    keyboard_out[0] = 'r';
    break;
   case 'a':
    keyboard_out[0] = 'l';
    break;
   case 224:
    ch2 = _getch();
    switch (ch2)
    {
    case 72:
     keyboard_out[1] = 'u';
     break;
    case 80:
     keyboard_out[1] = 'd';
     break;
    case 77:
     keyboard_out[1] = 'r';
     break;
    case 75:
     keyboard_out[1] = 'l';
     break;
    default:
     break;
    }
    break;
   default:
    break;
   }
  }
  Sleep(50);
 }
}
char get_canvas_toxy(COORD postion) {
 return canvas[postion.Y][postion.X];
}
char snode_gonext(Ps_node snode, char direct) {
 char old_direct = snode->direct;
 snode->direct = direct;
 switch (direct)
 {
 case 'u':
  snode->postion.Y = snode->postion.Y - 1;
  break;
 case 'd':
  snode->postion.Y = snode->postion.Y + 1;
  break;
 case 'l':
  snode->postion.X = snode->postion.X - 1;
  break;
 case 'r':
  snode->postion.X = snode->postion.X + 1;
  break;
 default:
  break;
 }
 return old_direct;
}
void snake_gonext(Psnake she, char direct) {
 Ps_node temp=&she->head;
 Ps_node end = temp;
 char dta=direct;   //方向
 while (temp!=NULL)  //不处理最后一个节点
 {
  if(temp->next_node!=NULL && temp->next_node->direct=='n'){
   if (temp->postion.X == temp->next_node->postion.X && temp->postion.Y == temp->next_node->postion.Y) {
    dta = snode_gonext(temp, dta);
    temp->next_node->direct = dta;
   }else
    dta = snode_gonext(temp, dta);
   break;
  }
  dta=snode_gonext(temp, dta);
  temp = temp->next_node;
 }
 snaketocanvas(she);
}
void snaketocanvas(Psnake she) {
 Ps_node temp=&she->head;
 int i, j,x,y;
 char body_type = she->body_type;
 for (i = 1; i < G_HEIGHT - 1; i++)
  for (j = 1; j < G_WIDTH - 1; j++)
   if (canvas[i][j] == she->head_type || canvas[i][j] == she->body_type)
    canvas[i][j] = '0';
 x = temp->postion.X;
 y = temp->postion.Y;
 while (temp->next_node != NULL) {
  canvas[temp->next_node->postion.Y][temp->next_node->postion.X] = body_type;
  temp = temp->next_node;
 }
 canvas[y][x] = she->head_type;   //单独处理蛇头,放在最后防止被覆盖
}
void draw_wall() {
 int i, j;
 setcolor(0xf0);
 gotoxy(0, 0);
 for (i = 0; i < G_HEIGHT; i++) {
  gotoxy(0, i);
  for (j = 0; j < G_WIDTH; j++) {
   if (canvas[i][j] == '#')
    printf("#");
   else
    printf(" ");
  }
 }
}
DWORD WINAPI Render_Thread(LPVOID lpParam) {
 Psnake shea = (Psnake)lpParam;
 Psnake sheb = (run_snake_threads==2)?((Psnake)lpParam+1):(shea);
 WORD color;
 char heada = shea->head_type;
 char headb = shea->head_type;
 char pr;
 char pr_score[50];
 char show_time[30];
 int snakes_counts = run_snake_threads;
 int i, j;
 if (snakes_counts == 1) {  //单人模式
  prs_colortoxy("player 1", G_WIDTH + 3, 6, 0xf0);
  prs_colortoxy("↑", G_WIDTH + 15, 5, 0xf0);
  prs_colortoxy("←↓→",G_WIDTH + 13, 6, 0xf0);
 }
 else {
  prs_colortoxy("player 1", G_WIDTH + 3, 6, 0xf0);
  prs_colortoxy("↑", G_WIDTH + 15, 5, 0xf0);
  prs_colortoxy("←↓→", G_WIDTH + 13, 6, 0xf0);
 
  prs_colortoxy("player 2", G_WIDTH + 3, 19, 0xf0);
  prs_colortoxy("W", G_WIDTH + 14, 18, 0xf0);
  prs_colortoxy("ASD", G_WIDTH + 13, 19, 0xf0);
 }
 while (run_snake_threads != 0)  //根据snake线程数结束循环
 {
  for (i = 1; i < G_HEIGHT-1; i++) {
   gotoxy(1, i);
   for (j = 1; j < G_WIDTH-1; j++) {
    switch (canvas[i][j])
    {
    case '@':   //蛇头
     color = (heada == '@') ? shea->color : sheb->color;
     pr = '@';
     break;
    case '*':   //蛇身
     color = (shea->body_type == '*') ? shea->color : sheb->color;
     pr = '*';
     break;
    case 'U':   //蛇头
     color = (shea->head_type == 'U') ? shea->color : sheb->color;
     pr = 'U';
     break;
    case 'S':   //蛇身
     color = (shea->body_type == 'S') ? shea->color : sheb->color;
     pr = 'S';
     break;
    case '$':   //豆子
     color = 0xf6;
     pr = '$';
     break;
    case '&':   //奖励
     color = 0xa4;
     pr = '&';
     break;
    case '0':
     color = 0xf0;
     pr = ' ';
     break;
    default:
     break;
    }
    setcolor(color);
    printf("%c", pr);
   }
  }
  sprintf(pr_score, "分数:      %d", shea->score);
  prs_colortoxy(pr_score, G_WIDTH + 3, 8, 0xf0);
  if (snakes_counts == 2) {
   sprintf(pr_score, "分数:      %d", sheb->score);
   prs_colortoxy(pr_score, G_WIDTH + 3, 21, 0xf0);
  }
  if (reward_time >= 0) {
   sprintf(show_time, "奖励剩余时间:%ds  ", reward_time / 1000);
   prs_colortoxy(show_time, G_WIDTH + 5, G_HEIGHT - 3, 0xf4);
  }
  else {
   prs_colortoxy("                    ", G_WIDTH + 5, G_HEIGHT - 3, 0xf4);
  }
  Sleep(50);  //显示频率
 }
}
 
STATUS createrand_bean() {
 int ran;  //canvas的第n个点为奖励位置
 int i,j,count,sum=0;
 char c = '$';
 HANDLE delrewarthread;
 
 for (i = 1; i < G_HEIGHT - 1; i++)
  for (j = 1; j < G_WIDTH - 1; j++)
   if (canvas[i][j] == '0')
    sum++;
 if (sum == 0)
  return FAIL;
 if (bean_count % 5 == 0 && bean_count!=0) {
  ran = rand() % sum;
  count = 0;
  for (i = 1; i < G_HEIGHT - 1; i++)
   for (j = 1; j < G_WIDTH - 1; j++)
    if (canvas[i][j] == '0') {
     if (count == ran) {
      reward_position.x = j;
      reward_position.y = i;
      canvas[i][j] = '&';
      delrewarthread= CreateThread(NULL, 0, Del_reward_Thread, NULL, 0, NULL);
      CloseHandle(delrewarthread);
     }
     count++;
    }
 }
 bean_count++;
 ran = rand() % sum;
 count = 0;
 for (i = 1; i < G_HEIGHT - 1; i++)
  for (j = 1; j < G_WIDTH - 1; j++)
   if (canvas[i][j] == '0') {
    if (count == ran) {
     canvas[i][j] = c;
     return SUCCESS;
    }
    count++;
   }
 return FAIL;
}
void eat_node(Psnake she, COORD postion) {
 Ps_node end = &she->head;
 Ps_node s = (Ps_node)malloc(sizeof(S_node));
 while (end->next_node != NULL)
  end = end->next_node;
 init_snode(s,'n', postion.X, postion.Y, NULL);
 end->next_node = s;
 canvas[postion.Y][postion.X]=she->head_type;
 she->score++;
 if (she->score / 10 >= she->level) {
  she->level++;
  she->speed = (int)(she->speed * 0.75);
 }
}
void free_snake(Psnake she) {
 Ps_node body = she->head.next_node;
 Ps_node temp;
 while (body!=NULL)
 {
  temp = body->next_node;
  free(body);
  body = temp;
 }
}
DWORD WINAPI Run_Snake_Thread(LPVOID lpParam) {
 Psnake pshe = (Psnake)lpParam;
 char direct;
 COORD next_node;   //下一个蛇头位置
 char v_canvas;
 boolean end = 0;
 while (!end) {
  direct = pshe->head.direct;
  if (keyboard_out[pshe->control - 1] != 'n') {       //获取键盘输入的目的是,获取蛇要前往的下一个方向
   switch (keyboard_out[pshe->control - 1])  //输入方向与运行方向相反
   {
   case 'u':
    direct = (direct != 'd') ? 'u' : direct;
    break;
   case 'd':
    direct = (direct != 'u') ? 'd' : direct;
    break;
   case 'l':
    direct = (direct != 'r') ? 'l' : direct;
    break;
   case 'r':
    direct = (direct != 'l') ? 'r' : direct;
    break;
   default:
    break;
   }
   keyboard_out[pshe->control - 1] = 'n';
  }
  next_node = pshe->head.postion;
  switch (direct)
  {
  case 'u':
   next_node.Y = next_node.Y - 1;
   break;
  case 'd':
   next_node.Y = next_node.Y + 1;
   break;
  case 'l':
   next_node.X = next_node.X - 1;
   break;
  case 'r':
   next_node.X = next_node.X + 1;
   break;
  default:
   break;
  }
  v_canvas = get_canvas_toxy(next_node);
  switch (v_canvas)
  {
  case '0':      //表示该点安全,没有东西
   snake_gonext(pshe, direct);
   break;
  case '$':
   eat_node(pshe, next_node);
   snake_gonext(pshe, direct);
   end = !createrand_bean();
   break;
  case '&':      //吃下道具,触发减速
   eat_node(pshe, next_node);
   snake_gonext(pshe, direct);
   pshe->speed =(int) (pshe->speed * 1.25);
   break;
  default:
   end = 1;
   break;
  }
  Sleep(pshe->speed);
 }
 free_snake(pshe);  //这里只释放了蛇的身体其他信息仍保留
 Sleep(500);
 return 0;
}
DWORD WINAPI Del_reward_Thread(LPVOID lpParam) {
 
 reward_time = 10000;
 while (reward_time >=0)
 {
  if (canvas[reward_position.y][reward_position.x] == '&' && run_snake_threads>0) {
   Sleep(1000);
   reward_time = reward_time - 1000;
  }
  else
   break;
 }
 canvas[reward_position.y][reward_position.x] = '0';
 reward_time = -1000;
 return 0;
}
void init_canvas() {
 int i, j;
 for (i = 0; i < G_HEIGHT; i++)
  for (j = 0; j < G_WIDTH; j++)
   canvas[i][j] = '0';
 for (i = 0; i < G_HEIGHT ; i++) {
  canvas[i][0] = '#';
  canvas[i][G_WIDTH - 1] = '#';
 }
 for (j = 0; j < G_WIDTH; j++) {
  canvas[0][j] = '#';
  canvas[G_HEIGHT  - 1][j] = '#';
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/YiLiuF/article/details/108134110