当前位置:   article > 正文

贪吃蛇的实现—C语言_c语言贪吃蛇怎么通过f1f2控制加速减速

c语言贪吃蛇怎么通过f1f2控制加速减速

目录

简单解释几个函数:

1、记录最高分的函数

2、 隐藏光标

3、初始化蛇身

 4、播放背景音乐的函数

完整代码如下:

 运行界面显示:


█ 开发工具:Dev C++

 

简单解释几个函数:

1、记录最高分的函数

分为读取和储存,即分为 File_out() 和 File_in();

  1. /**
  2. * 储存最高分进文件
  3. */
  4. void File_in()
  5. {
  6. FILE *fp;
  7. fp = fopen("save.txt", "w+"); //以读写的方式建立一个名为save.txt的文件
  8. fprintf(fp, "%d", score); //把分数写进文件中
  9. fclose(fp); //关闭文件
  10. }
  11. /**
  12. * 在文件中读取最高分
  13. */
  14. void File_out()
  15. {
  16. FILE *fp;
  17. fp = fopen("save.txt", "a+"); //打开文件save.txt
  18. fscanf(fp, "%d", &HighScore); //把文件中的最高分读出来
  19. fclose(fp); //关闭文件
  20. }

 ✔ 文件中写入数据的步骤,首先使用fopen()方法来打开文件,如果要打开的文件不存在,那么创建此文件;然后通过fprintf()方法把数据写入文件;最后使用fclose()方法关闭文件。

 

2、 隐藏光标

  1. //隐藏光标
  2. void HideCursor()
  3. {
  4. CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
  5. curInfo.dwSize = 1; //如果没赋值的话,隐藏光标无效
  6. curInfo.bVisible = FALSE; //将光标设置为不可见
  7. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
  8. SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
  9. }

 

3、初始化蛇身

  1. /**
  2. * 初始化蛇身,画蛇身
  3. */
  4. void initsnake()
  5. {
  6. snakee *tail; //定义蛇尾指针
  7. int i;
  8. tail=(snakee*)malloc(sizeof(snakee)); //初始化指针,给指针开辟一块初始空间
  9. //从蛇尾开始,头插法,以x,y设定开始的位置//
  10. tail->x=24; //蛇的初始位置(24,5)
  11. tail->y=5;
  12. tail->next=NULL;
  13. for(i=1;i<=4;i++) //设置蛇身,长度为5
  14. {
  15. head=(snakee*)malloc(sizeof(snakee)); //初始化蛇头 lgm 前面已经全局变量蛇头指针 snake *head
  16. head->next=tail; //蛇头的下一位为蛇尾
  17. head->x=24+2*i; //设置蛇头位置
  18. head->y=5;
  19. tail=head; //蛇头变成蛇尾,然后重复循环
  20. }
  21. while(tail!=NULL) //从头到尾,输出蛇身
  22. {
  23. gotoxy(tail->x,tail->y);
  24. color(2);
  25. printf("★"); //输出蛇身,蛇身使用★组成
  26. tail=tail->next; //蛇头输出完毕,输出蛇头的下一位,一直输出到蛇尾
  27. }
  28. }

 4、播放背景音乐的函数

  1. #include<mmsystem.h> //多媒体设备接口
  2. #pragma comment(lib,"winmm.lib") //预处理命令
  3. mciSendString("open music1.MP3",0,0,0);
  4. mciSendString("play music1.MP3 repeat",0,0,0);

✔ music1.MP3 是同路径下的一个MP3文件。

如果报错,可能需要在编译器环境里添加-lwinmm;即点击工具-->点击编译选项-->在下图位置添加。

完整代码如下:

注:有借鉴他人博客。

  1. //#include<stdafx.h> //vc自带头文件 在Dev-c++中不需要
  2. #include<stdio.h>
  3. #include<conio.h> //标准输入输出函数库
  4. #include<time.h> //用于获得随机数
  5. #include<windows.h> //控制dos界面
  6. #include<mmsystem.h> //多媒体设备接口
  7. #pragma comment(lib,"winmm.lib") //预处理命令
  8. #include<stdlib.h> //即standard library标志库头文件,里面定义了一些宏和通用工具函数
  9. //接收键盘输入输出
  10. /*******宏 定 义*******/
  11. #define U 1
  12. #define D 2
  13. #define L 3
  14. #define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右
  15. /*******定 义 全 局 变 量 *******/
  16. typedef struct snake //蛇身的一个节点
  17. {
  18. int x;
  19. int y;
  20. struct snake *next;
  21. }snakee;
  22. int score=0,add=10; //总得分与每次吃食物得分
  23. int HighScore = 0; //最高分
  24. int status,sleeptime=400; //蛇前进状态,每次运行的时间间隔
  25. snakee *head, *food; //蛇头指针,食物指针
  26. snakee *q; //遍历蛇的时候用到的指针
  27. int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。
  28. HANDLE hOut; //控制台句柄
  29. /*******函 数 声 明 *******/
  30. void gotoxy(int x,int y); //设置光标位置
  31. int color(int c); //更改文字颜色
  32. void printsnake(); //字符画---蛇
  33. void welcometogame(); //开始界面
  34. void createMap(); //绘制地图
  35. void scoreandtips(); //游戏界面右侧的得分和小提示
  36. void initsnake(); //初始化蛇身,画蛇身
  37. void createfood(); //创建并随机出现食物
  38. int biteself(); //判断是否咬到了自己
  39. void cantcrosswall(); //设置蛇撞墙的情况
  40. void speedup(); //加速
  41. void speeddown(); //减速
  42. void snakemove(); //控制蛇前进方向
  43. void keyboardControl(); //控制键盘按键
  44. void Lostdraw(); //游戏结束界面
  45. void endgame(); //游戏结束
  46. void choose(); //游戏失败之后的选择
  47. void File_out(); //在文件中读取最高分
  48. void File_in(); //储存最高分进文件
  49. void HideCursor(); //隐藏光标
  50. void explation(); //游戏说明
  51. //主函数
  52. int main()
  53. {
  54. HideCursor();
  55. mciSendString("open music1.MP3",0,0,0);
  56. mciSendString("play music1.MP3 repeat",0,0,0);
  57. system("mode con cols=100 lines=30"); //设置控制台的宽高
  58. printsnake(); //to draw the snake by character
  59. welcometogame(); //start to play game
  60. File_out();
  61. keyboardControl();
  62. endgame();
  63. return 0;
  64. }
  65. //
  66. /**
  67. * 设置光标位置
  68. */
  69. void gotoxy(int x,int y)
  70. {
  71. COORD c;
  72. c.X=x;
  73. c.Y=y;
  74. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
  75. }
  76. //隐藏光标
  77. void HideCursor()
  78. {
  79. CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
  80. curInfo.dwSize = 1; //如果没赋值的话,隐藏光标无效
  81. curInfo.bVisible = FALSE; //将光标设置为不可见
  82. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
  83. SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
  84. }
  85. /**
  86. * 文字颜色函数 此函数的局限性:1、只能Windows系统下使用 2、不能改变背景颜色
  87. */
  88. int color(int c)
  89. {
  90. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色
  91. return 0;
  92. }
  93. /*
  94. * 使用字符画---snake
  95. */
  96. void printsnake()
  97. {
  98. color(2);
  99. printf(" \n");
  100. printf(" __________ ___ \n");
  101. printf(" / \\ / \\ \\ |____ __\\__ \n");
  102. printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
  103. printf(" | | |__| _/_ |_| / [|] |/ \n");
  104. printf(" | | | | | / _|_ \\__/ \n");
  105. printf(" \\ \\_______ / \\ |___/ ____ \n");
  106. printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
  107. printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
  108. printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
  109. printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
  110. printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
  111. printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
  112. printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
  113. }
  114. void welcometogame()
  115. {
  116. int n;
  117. int i,j = 1;
  118. gotoxy(43,18);
  119. color(15);
  120. printf("贪 吃 蛇 大 作 战");
  121. color(15); //黄色边框
  122. for (i = 20; i <= 26; i++) //输出上下边框┅
  123. {
  124. for (j = 27; j <= 74; j++) //输出左右边框┇
  125. {
  126. gotoxy(j, i);
  127. if (i == 20 || i == 26)
  128. {
  129. printf("-");
  130. }
  131. else if (j == 27 || j == 74)
  132. {
  133. printf("|");
  134. }
  135. }
  136. }
  137. color(2);
  138. gotoxy(35, 22);
  139. printf("1.开始游戏");
  140. gotoxy(55, 22);
  141. printf("2.游戏说明");
  142. gotoxy(35, 24);
  143. printf("3.退出游戏");
  144. gotoxy(29,27);
  145. color(15);
  146. printf("请选择[1 2 3]:[ ]\b\b"); //\b为退格,使得光标处于[]中间
  147. color(2);
  148. scanf("%d", &n); //输入选项
  149. switch (n)
  150. {
  151. case 1:
  152. system("cls");
  153. createMap(); //创建地图
  154. initsnake(); //初始化蛇身
  155. createfood(); //创建食物
  156. keyboardControl(); //按键控制
  157. break;
  158. case 2:
  159. explation();
  160. break;
  161. case 3:
  162. exit(0); //退出游戏
  163. break;
  164. default: //输入非1~3之间的选项
  165. color(12);
  166. gotoxy(40,28);
  167. printf("请输入1~3之间的数!");
  168. getch(); //输入任意键
  169. system("cls"); //清屏
  170. printsnake();
  171. welcometogame();
  172. }
  173. }
  174. /**
  175. * 创建地图
  176. */
  177. void createMap()
  178. {
  179. int i,j;
  180. for(i=0;i<58;i+=2) //打印上下边框
  181. {
  182. gotoxy(i,0);
  183. color(2); //深紫色的边框
  184. printf("□");
  185. gotoxy(i,26);
  186. printf("□");
  187. }
  188. for(i=1;i<26;i++) //打印左右边框
  189. {
  190. gotoxy(0,i);
  191. printf("□");
  192. gotoxy(56,i);
  193. printf("□");
  194. }
  195. for(i = 2;i<56;i+=2) //打印中间网格
  196. {
  197. for(j = 1;j<26;j++)
  198. {
  199. gotoxy(i,j);
  200. color(15);
  201. printf("■");
  202. }
  203. }
  204. }
  205. /**
  206. * 游戏界面右侧的得分和小提示
  207. */
  208. void scoreandtips()
  209. {
  210. File_out();
  211. gotoxy(64,4);
  212. color(11);
  213. printf("☆最高记录☆:%d",HighScore);
  214. gotoxy(64,8);
  215. color(14);
  216. printf("得分:%d ",score);
  217. gotoxy(64,9);
  218. color(14);
  219. printf("等级:%d",score/100);
  220. color(13);
  221. gotoxy(73,11);
  222. printf("小 提 示");
  223. gotoxy(60,13);
  224. color(6);
  225. printf("╬ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ ╬");
  226. gotoxy(60,25);
  227. printf("╬ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ ╬");
  228. color(3);
  229. gotoxy(64,14);
  230. printf("每个食物得分:%d分",add);
  231. gotoxy(64,16);
  232. printf("不能穿墙,不能咬到自己");
  233. gotoxy(64,18);
  234. printf("用↑ ↓ ← →分别控制蛇的移动");
  235. gotoxy(64,20);
  236. printf("F1 为加速,F2 为减速");
  237. gotoxy(64,22);
  238. printf("space:暂停游戏");
  239. gotoxy(64,24);
  240. printf("ESC :退出游戏");
  241. }
  242. /**
  243. * 初始化蛇身,画蛇身
  244. */
  245. void initsnake()
  246. {
  247. snakee *tail; //定义蛇尾指针
  248. int i;
  249. tail=(snakee*)malloc(sizeof(snakee)); //初始化指针,给指针开辟一块初始空间
  250. //从蛇尾开始,头插法,以x,y设定开始的位置//
  251. tail->x=24; //蛇的初始位置(24,5)
  252. tail->y=5;
  253. tail->next=NULL;
  254. for(i=1;i<=4;i++) //设置蛇身,长度为5
  255. {
  256. head=(snakee*)malloc(sizeof(snakee)); //初始化蛇头 lgm 前面已经全局变量蛇头指针 snake *head
  257. head->next=tail; //蛇头的下一位为蛇尾
  258. head->x=24+2*i; //设置蛇头位置
  259. head->y=5;
  260. tail=head; //蛇头变成蛇尾,然后重复循环
  261. }
  262. while(tail!=NULL) //从头到尾,输出蛇身
  263. {
  264. gotoxy(tail->x,tail->y);
  265. color(2);
  266. printf("★"); //输出蛇身,蛇身使用★组成
  267. tail=tail->next; //蛇头输出完毕,输出蛇头的下一位,一直输出到蛇尾
  268. }
  269. }
  270. /**
  271. * 随机出现食物
  272. */
  273. void createfood()
  274. {
  275. snakee *food_1; //定义临时食物指针
  276. //srand((unsigned)time(NULL)); //初始化随机数
  277. food_1=(snakee*)malloc(sizeof(snakee)); //初始化food_1
  278. srand(time(NULL)); //初始化随机数
  279. //保证其为偶数,使得食物能与蛇头对其,然后食物会出现在网格线上
  280. food_1->x=rand()%52+2;
  281. //food_1->x=23;
  282. while((food_1->x%2)!=0)
  283. {
  284. food_1->x=rand()%52+2; //食物随机出现
  285. }
  286. food_1->y=rand()%24+1;
  287. q=head;
  288. while(q->next==NULL)
  289. {
  290. if(q->x==food_1->x && q->y==food_1->y) //判断蛇身是否与食物重合
  291. {
  292. free(food_1); //如果蛇身和食物重合,那么释放食物指针
  293. createfood(); //重新创建食物
  294. }
  295. q=q->next;
  296. }
  297. gotoxy(food_1->x,food_1->y);
  298. food=food_1;
  299. color(12);
  300. printf("◎"); //输出食物
  301. }
  302. /**** to see if the snake bite himdelf**/
  303. int biteself()
  304. {
  305. snakee *self; //定义self为蛇身上的一个节点
  306. self=head->next; //self是蛇头之外的蛇身上的节点,从蛇头的下一个节点开始
  307. while(self!=NULL)
  308. {
  309. if(self->x==head->x && self->y==head->y) //如果self和蛇身上的节点重合
  310. {
  311. return 1; //返回1
  312. }
  313. self=self->next;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * 设置蛇撞墙的情况
  319. */
  320. void cantcrosswall()
  321. {
  322. if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) //如果蛇头碰到了墙壁
  323. {
  324. endgamestatus=1; //返回第一种情况
  325. endgame(); //出现游戏结束界面
  326. }
  327. }
  328. /**
  329. * 加速,蛇吃到食物会自动提速,并且按F1会加速
  330. */
  331. void speedup()
  332. {
  333. if(sleeptime>=50) //如果时间间隔大于等于50
  334. {
  335. sleeptime=sleeptime-10; //时间间隔减10
  336. add=add+2; //每吃一次食物,得分加2
  337. }
  338. }
  339. /**
  340. * 减速,按F2会减速
  341. */
  342. void speeddown()
  343. {
  344. if(sleeptime<350) //如果时间间隔小于350
  345. {
  346. sleeptime=sleeptime+30; //时间间隔加上30
  347. add=add-2; //每吃一次食物的得分减2
  348. }
  349. }
  350. void keyboardControl()
  351. {
  352. status=D; //初始蛇向下移动
  353. while(1)
  354. {
  355. scoreandtips();
  356. if(GetAsyncKeyState(VK_UP) && status!=D) //GetAsyncKeyState函数用来判断函数调用时指定虚拟键的状态
  357. {
  358. status=U; //如果蛇不是向下前进的时候,按上键,执行向上前进操作
  359. }
  360. else if(GetAsyncKeyState(VK_DOWN) && status!=U) //如果蛇不是向上前进的时候,按下键,执行向下前进操作
  361. {
  362. status=D;
  363. }
  364. else if(GetAsyncKeyState(VK_LEFT)&& status!=R) //如果蛇不是向右前进的时候,按左键,执行向左前进
  365. {
  366. status=L;
  367. }
  368. else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) //如果蛇不是向左前进的时候,按右键,执行向右前进
  369. {
  370. status=R;
  371. }
  372. if(GetAsyncKeyState(VK_SPACE)) //按暂停键,执行pause暂停函数
  373. {
  374. while(1)
  375. {
  376. Sleep(300); //sleep()函数,头文件#include <unistd.h> 另进程暂停,知道达到里面设定的参数的时间。
  377. if(GetAsyncKeyState(VK_SPACE)) //按空格键暂停
  378. {
  379. break;
  380. }
  381. }
  382. }
  383. else if(GetAsyncKeyState(VK_ESCAPE))
  384. {
  385. endgamestatus=3; //按esc键,直接到结束界面
  386. break;
  387. }
  388. else if(GetAsyncKeyState(VK_F1)) //按F1键,加速
  389. {
  390. speedup();
  391. }
  392. else if(GetAsyncKeyState(VK_F2)) //按F2键,减速
  393. {
  394. speeddown();
  395. }
  396. Sleep(sleeptime);
  397. snakemove();
  398. }
  399. }
  400. /**
  401. * 控制方向
  402. */
  403. void snakemove() //蛇前进,上U,下D,左L,右R
  404. {
  405. snakee *nexthead;
  406. cantcrosswall();
  407. nexthead=(snakee*)malloc(sizeof(snakee)); //为下一步开辟空间
  408. //
  409. // 向上运动程序段
  410. //
  411. if(status==U)
  412. {
  413. nexthead->x=head->x; //向上前进时,x坐标不动,y坐标-1
  414. nexthead->y=head->y-1;
  415. nexthead->next=head;
  416. head=nexthead;
  417. q=head; //指针q指向蛇头
  418. if(nexthead->x==food->x && nexthead->y==food->y) //如果下一个有食物 下一个位置的坐标和食物的坐标相同
  419. {
  420. while(q!=NULL)
  421. {
  422. gotoxy(q->x,q->y);
  423. color(2);
  424. printf("★"); //原来食物的位置,从●换成★
  425. q=q->next; //指针q指向的蛇身的下一位也执行循环里的操作
  426. }
  427. score=score+add; //吃了一个食物,在总分上加上食物的分
  428. speedup();
  429. createfood(); //创建食物
  430. }
  431. else
  432. {
  433. while(q->next->next!=NULL) //如果没遇到食物
  434. {
  435. gotoxy(q->x,q->y);
  436. color(2);
  437. printf("★"); //蛇正常往前走,输出当前位置的蛇身
  438. q=q->next; //继续输出整个蛇身
  439. }
  440. gotoxy(q->next->x,q->next->y); //经过上面的循环,q指向蛇尾,蛇尾的下一位,就是蛇走过去的位置
  441. color(15);
  442. printf("■");
  443. free(q->next); //进行输出■之后,释放指向下一位的指针
  444. q->next=NULL; //指针下一位指向空
  445. }
  446. }
  447. //
  448. // 向下运动程序段
  449. //
  450. if(status==D)
  451. {
  452. nexthead->x=head->x; //向下前进时,x坐标不动,y坐标+1
  453. nexthead->y=head->y+1;
  454. nexthead->next=head;
  455. head=nexthead;
  456. q=head;
  457. if(nexthead->x==food->x && nexthead->y==food->y) //有食物
  458. {
  459. while(q!=NULL)
  460. {
  461. gotoxy(q->x,q->y);
  462. color(2);
  463. printf("★");
  464. q=q->next;
  465. }
  466. score=score+add;
  467. speedup();
  468. createfood();
  469. }
  470. else //没有食物
  471. {
  472. while(q->next->next!=NULL)
  473. {
  474. gotoxy(q->x,q->y);
  475. color(2);
  476. printf("★");
  477. q=q->next;
  478. }
  479. gotoxy(q->next->x,q->next->y);
  480. color(15);
  481. printf("■");
  482. free(q->next);
  483. q->next=NULL;
  484. }
  485. }
  486. //
  487. // 向左运动程序段
  488. //
  489. if(status==L)
  490. {
  491. nexthead->x=head->x-2; //向左前进时,x坐标向左移动-2,y坐标不动
  492. nexthead->y=head->y;
  493. nexthead->next=head;
  494. head=nexthead;
  495. q=head;
  496. if(nexthead->x==food->x && nexthead->y==food->y)//有食物
  497. {
  498. while(q!=NULL)
  499. {
  500. gotoxy(q->x,q->y);
  501. color(2);
  502. printf("★");
  503. q=q->next;
  504. }
  505. score=score+add;
  506. speedup();
  507. createfood();
  508. }
  509. else //没有食物
  510. {
  511. while(q->next->next!=NULL)
  512. {
  513. gotoxy(q->x,q->y);
  514. color(2);
  515. printf("★");
  516. q=q->next;
  517. }
  518. gotoxy(q->next->x,q->next->y);
  519. color(15);
  520. printf("■");
  521. free(q->next);
  522. q->next=NULL;
  523. }
  524. }
  525. //
  526. // 向右运动程序段
  527. //
  528. if(status==R)
  529. {
  530. nexthead->x=head->x+2; //向右前进时,x坐标向右移动+2,y坐标不动
  531. nexthead->y=head->y;
  532. nexthead->next=head;
  533. head=nexthead; //此句以为让蛇不断前进
  534. q=head;
  535. if(nexthead->x==food->x && nexthead->y==food->y)//有食物
  536. {
  537. while(q!=NULL)
  538. {
  539. gotoxy(q->x,q->y);
  540. color(2);
  541. printf("★");
  542. q=q->next;
  543. }
  544. score=score+add;
  545. speedup();
  546. createfood();
  547. }
  548. else //没有食物
  549. {
  550. while(q->next->next!=NULL)
  551. {
  552. gotoxy(q->x,q->y);
  553. color(2);
  554. printf("★");
  555. q=q->next;
  556. }
  557. gotoxy(q->next->x,q->next->y);
  558. color(15);
  559. printf("■");
  560. free(q->next);
  561. q->next=NULL;
  562. }
  563. }
  564. //
  565. if(biteself()==1) //判断是否会咬到自己
  566. {
  567. endgamestatus=2;
  568. endgame();
  569. }
  570. }
  571. /**
  572. * 控制键盘按键控制蛇的前进方向
  573. */
  574. /**
  575. * 储存最高分进文件
  576. */
  577. void File_in()
  578. {
  579. FILE *fp;
  580. fp = fopen("save.txt", "w+"); //以读写的方式建立一个名为save.txt的文件
  581. fprintf(fp, "%d", score); //把分数写进文件中
  582. fclose(fp); //关闭文件
  583. }
  584. /**
  585. * 在文件中读取最高分
  586. */
  587. void File_out()
  588. {
  589. FILE *fp;
  590. fp = fopen("save.txt", "a+"); //打开文件save.txt
  591. fscanf(fp, "%d", &HighScore); //把文件中的最高分读出来
  592. fclose(fp); //关闭文件
  593. }
  594. /*
  595. * 游戏说明
  596. */
  597. void explation()
  598. {
  599. int i,j = 1;
  600. system("cls");
  601. color(13);
  602. gotoxy(44,3);
  603. printf("游戏说明");
  604. color(2);
  605. for (i = 6; i <= 22; i++) //输出上下边框===
  606. {
  607. for (j = 20; j <= 75; j++) //输出左右边框||
  608. {
  609. gotoxy(j, i);
  610. if (i == 6 || i == 22) printf("=");
  611. else if (j == 20 || j == 75) printf("||");
  612. }
  613. }
  614. color(3);
  615. gotoxy(30,8);
  616. printf("tip1: 不能穿墙,不能咬到自己");
  617. color(10);
  618. gotoxy(30,11);
  619. printf("tip2: 用↑.↓.←.→分别控制蛇的移动");
  620. color(14);
  621. gotoxy(30,14);
  622. printf("tip3: F1 为加速,F2 为减速");
  623. color(11);
  624. gotoxy(30,17);
  625. printf("tip4: 按空格键暂停游戏,再按空格键继续");
  626. color(4);
  627. gotoxy(30,20);
  628. printf("tip5: ESC :退出游戏.space:暂停游戏");
  629. getch(); //按任意键返回主界面
  630. system("cls");
  631. printsnake();
  632. welcometogame();
  633. }
  634. /**
  635. * 结束游戏
  636. */
  637. void endgame()
  638. {
  639. system("cls");
  640. if(endgamestatus==1)
  641. {
  642. Lostdraw();
  643. gotoxy(35,9);
  644. color(12);
  645. printf("对不起,您撞到墙了。游戏结束!");
  646. }
  647. else if(endgamestatus==2)
  648. {
  649. Lostdraw();
  650. gotoxy(35,9);
  651. color(12);
  652. printf("对不起,您咬到自己了。游戏结束!");
  653. }
  654. else if(endgamestatus==3)
  655. {
  656. Lostdraw();
  657. gotoxy(40,9);
  658. color(12);
  659. printf("您已经结束了游戏。");
  660. }
  661. gotoxy(43,12);
  662. color(13);
  663. printf("您的得分是 %d",score);
  664. gotoxy(43,14);
  665. color(13);
  666. printf("游戏等级为 %d",score/100);
  667. if(score >= HighScore)
  668. {
  669. color(10);
  670. gotoxy(33,16);
  671. printf("创纪录啦!最高分被你刷新啦,真棒!!!");
  672. File_in(); //把最高分写进文件
  673. }
  674. else
  675. {
  676. color(10);
  677. gotoxy(33,16);
  678. printf("继续努力吧~ 你离最高分还差:%d",HighScore-score);
  679. }
  680. choose();
  681. }
  682. /**
  683. * 边框下面的分支选项
  684. */
  685. void choose()
  686. {
  687. int n;
  688. gotoxy(25,23);
  689. color(12);
  690. printf("我要重新玩一局-------1");
  691. gotoxy(52,23);
  692. printf("不玩了,退出吧-------2");
  693. gotoxy(46,25);
  694. color(11);
  695. printf("选择:");
  696. scanf("%d", &n);
  697. switch (n)
  698. {
  699. case 1:
  700. system("cls"); //清屏
  701. score=0; //分数归零
  702. sleeptime=200; //设定初始速度
  703. add = 10; //使add设定为初值,吃一个食物得分10,然后累加
  704. printsnake(); //返回欢迎界面
  705. welcometogame();
  706. break;
  707. case 2:
  708. exit(0); //退出游戏
  709. break;
  710. default:
  711. gotoxy(35,27);
  712. color(12);
  713. printf("※※您的输入有误,请重新输入※※");
  714. system("pause >nul");
  715. endgame();
  716. choose();
  717. break;
  718. }
  719. }
  720. /**
  721. * 失败界面
  722. */
  723. void Lostdraw()
  724. {
  725. system("cls");
  726. int i;
  727. gotoxy(45,2);
  728. color(6);
  729. printf("\\\\\\|///");
  730. gotoxy(43,3);
  731. printf("\\\\");
  732. gotoxy(47,3);
  733. color(15);
  734. printf(".-.-");
  735. gotoxy(54,3);
  736. color(6);
  737. printf("//");
  738. gotoxy(44,4);
  739. color(14);
  740. printf("(");
  741. gotoxy(47,4);
  742. color(15);
  743. printf(".@.@");
  744. gotoxy(54,4);
  745. color(14);
  746. printf(")");
  747. gotoxy(17,5);
  748. color(11);
  749. printf("+------------------------");
  750. gotoxy(35,5);
  751. color(14);
  752. printf("oOOo");
  753. gotoxy(39,5);
  754. color(11);
  755. printf("----------");
  756. gotoxy(48,5);
  757. color(14);
  758. printf("(_)");
  759. gotoxy(51,5);
  760. color(11);
  761. printf("----------");
  762. gotoxy(61,5);
  763. color(14);
  764. printf("oOOo");
  765. gotoxy(65,5);
  766. color(11);
  767. printf("-----------------+");
  768. for(i = 6;i<=19;i++) //竖边框
  769. {
  770. gotoxy(17,i);
  771. printf("|");
  772. gotoxy(82,i);
  773. printf("|");
  774. }
  775. gotoxy(17,20);
  776. printf("+---------------------------------");
  777. gotoxy(52,20);
  778. color(14);
  779. printf("〝 ☆☆☆ 〞");
  780. gotoxy(60,20);
  781. color(11);
  782. printf("----------------------+");
  783. }

 

 运行界面显示:

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/867435
推荐阅读
相关标签
  

闽ICP备14008679号