找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1288|回复: 0
收起左侧

贪吃蛇小游戏C语言程序

[复制链接]
ID:1013938 发表于 2022-5-17 14:22 | 显示全部楼层 |阅读模式
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<windows.h>
  4. #include<stdlib.h>

  5. #define U 1
  6. #define D 2
  7. #define L 3
  8. #define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右

  9. typedef struct SNAKE //蛇身的一个节点
  10. {
  11. int x;
  12. int y;
  13. struct SNAKE *next;
  14. }snake;

  15. //全局变量//
  16. int score=0,add=10;//总得分与每次吃食物得分。
  17. int status,sleeptime=200;//每次运行的时间间隔
  18. snake *head, *food;//蛇头指针,食物指针
  19. snake *q;//遍历蛇的时候用到的指针
  20. int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。

  21. //声明全部函数//
  22. void Pos();
  23. void creatMap();
  24. void initsnake();
  25. int biteself();
  26. void createfood();
  27. void cantcrosswall();
  28. void snakemove();
  29. void pause();
  30. void gamecircle();
  31. void welcometogame();
  32. void endgame();
  33. void gamestart();

  34. void Pos(int x,int y)//设置光标位置
  35. {
  36. COORD pos;
  37. HANDLE hOutput;
  38. pos.X=x;
  39. pos.Y=y;
  40. hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
  41. SetConsoleCursorPosition(hOutput,pos);
  42. }

  43. void creatMap()//创建地图
  44. {
  45. int i;
  46. for(i=0;i<58;i+=2)//打印上下边框
  47. {
  48. Pos(i,0);
  49. printf("■");
  50. Pos(i,26);
  51. printf("■");
  52. }
  53. for(i=1;i<26;i++)//打印左右边框
  54. {
  55. Pos(0,i);
  56. printf("■");
  57. Pos(56,i);
  58. printf("■");
  59. }
  60. }

  61. void initsnake()//初始化蛇身
  62. {
  63. snake *tail;
  64. int i;
  65. tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
  66. tail->x=24;
  67. tail->y=5;
  68. tail->next=NULL;
  69. for(i=1;i<=4;i++)
  70. {
  71. head=(snake*)malloc(sizeof(snake));
  72. head->next=tail;
  73. head->x=24+2*i;
  74. head->y=5;
  75. tail=head;
  76. }
  77. while(tail!=NULL)//从头到为,输出蛇身
  78. {
  79. Pos(tail->x,tail->y);
  80. printf("■");
  81. tail=tail->next;
  82. }
  83. }

  84. int biteself()//判断是否咬到了自己
  85. {
  86. snake *self;
  87. self=head->next;
  88. while(self!=NULL)
  89. {
  90. if(self->x==head->x && self->y==head->y)
  91. {
  92. return 1;
  93. }
  94. self=self->next;
  95. }
  96. return 0;
  97. }

  98. void createfood()//随机出现食物
  99. {
  100. snake *food_1;
  101. srand((unsigned)time(NULL));
  102. food_1=(snake*)malloc(sizeof(snake));
  103. while((food_1->x%2)!=0) //保证其为偶数,使得食物能与蛇头对其
  104. {
  105. food_1->x=rand()%52+2;
  106. }
  107. food_1->y=rand()%24+1;
  108. q=head;
  109. while(q->next==NULL)
  110. {
  111. if(q->x==food_1->x && q->y==food_1->y) //判断蛇身是否与食物重合
  112. {
  113. free(food_1);
  114. createfood();
  115. }
  116. q=q->next;
  117. }
  118. Pos(food_1->x,food_1->y);
  119. food=food_1;
  120. printf("■");
  121. }

  122. void cantcrosswall()//不能穿墙
  123. {
  124. if(head->x==0 || head->x==56 ||head->y==0 || head->y==26)
  125. {
  126. endgamestatus=1;
  127. endgame();
  128. }
  129. }

  130. void snakemove()//蛇前进,上U,下D,左L,右R
  131. {
  132. snake * nexthead;
  133. cantcrosswall();

  134. nexthead=(snake*)malloc(sizeof(snake));
  135. if(status==U)
  136. {
  137. nexthead->x=head->x;
  138. nexthead->y=head->y-1;
  139. if(nexthead->x==food->x && nexthead->y==food->y)//如果下一个有食物//
  140. {
  141. nexthead->next=head;
  142. head=nexthead;
  143. q=head;
  144. while(q!=NULL)
  145. {
  146. Pos(q->x,q->y);
  147. printf("■");
  148. q=q->next;
  149. }
  150. score=score+add;
  151. createfood();
  152. }
  153. else //如果没有食物//
  154. {
  155. nexthead->next=head;
  156. head=nexthead;
  157. q=head;
  158. while(q->next->next!=NULL)
  159. {
  160. Pos(q->x,q->y);
  161. printf("■");
  162. q=q->next;
  163. }
  164. Pos(q->next->x,q->next->y);
  165. printf(" ");
  166. free(q->next);
  167. q->next=NULL;
  168. }
  169. }
  170. if(status==D)
  171. {
  172. nexthead->x=head->x;
  173. nexthead->y=head->y+1;
  174. if(nexthead->x==food->x && nexthead->y==food->y) //有食物
  175. {
  176. nexthead->next=head;
  177. head=nexthead;
  178. q=head;
  179. while(q!=NULL)
  180. {
  181. Pos(q->x,q->y);
  182. printf("■");
  183. q=q->next;
  184. }
  185. score=score+add;
  186. createfood();
  187. }
  188. else //没有食物
  189. {
  190. nexthead->next=head;
  191. head=nexthead;
  192. q=head;
  193. while(q->next->next!=NULL)
  194. {
  195. Pos(q->x,q->y);
  196. printf("■");
  197. q=q->next;
  198. }
  199. Pos(q->next->x,q->next->y);
  200. printf(" ");
  201. free(q->next);
  202. q->next=NULL;
  203. }
  204. }
  205. if(status==L)
  206. {
  207. nexthead->x=head->x-2;
  208. nexthead->y=head->y;
  209. if(nexthead->x==food->x && nexthead->y==food->y)//有食物
  210. {
  211. nexthead->next=head;
  212. head=nexthead;
  213. q=head;
  214. while(q!=NULL)
  215. {
  216. Pos(q->x,q->y);
  217. printf("■");
  218. q=q->next;
  219. }
  220. score=score+add;
  221. createfood();
  222. }
  223. else //没有食物
  224. {
  225. nexthead->next=head;
  226. head=nexthead;
  227. q=head;
  228. while(q->next->next!=NULL)
  229. {
  230. Pos(q->x,q->y);
  231. printf("■");
  232. q=q->next;
  233. }
  234. Pos(q->next->x,q->next->y);
  235. printf(" ");
  236. free(q->next);
  237. q->next=NULL;
  238. }
  239. }
  240. if(status==R)
  241. {
  242. nexthead->x=head->x+2;
  243. nexthead->y=head->y;
  244. if(nexthead->x==food->x && nexthead->y==food->y)//有食物
  245. {
  246. nexthead->next=head;
  247. head=nexthead;
  248. q=head;
  249. while(q!=NULL)
  250. {
  251. Pos(q->x,q->y);
  252. printf("■");
  253. q=q->next;
  254. }
  255. score=score+add;
  256. createfood();
  257. }
  258. else //没有食物
  259. {
  260. nexthead->next=head;
  261. head=nexthead;
  262. q=head;
  263. while(q->next->next!=NULL)
  264. {
  265. Pos(q->x,q->y);
  266. printf("■");
  267. q=q->next;
  268. }
  269. Pos(q->next->x,q->next->y);
  270. printf(" ");
  271. free(q->next);
  272. q->next=NULL;
  273. }
  274. }
  275. if(biteself()==1) //判断是否会咬到自己
  276. {
  277. endgamestatus=2;
  278. endgame();
  279. }
  280. }

  281. void pause()//暂停
  282. {
  283. while(1)
  284. {
  285. Sleep(300);
  286. if(GetAsyncKeyState(VK_SPACE))
  287. {
  288. break;
  289. }

  290. }
  291. }

  292. void gamecircle()//控制游戏
  293. {

  294. Pos(64,15);
  295. printf("不能穿墙,不能咬到自己\n");
  296. Pos(64,16);
  297. printf("用↑.↓.←.→分别控制蛇的移动.");
  298. Pos(64,17);
  299. printf("F1 为加速,F2 为减速\n");
  300. Pos(64,18);
  301. printf("ESC :退出游戏.space:暂停游戏.");
  302. Pos(64,20);
  303. printf("张文斌修改完成C语言程序贪吃蛇");
  304. status=R;
  305. while(1)
  306. {
  307. Pos(64,10);
  308. printf("得分:%d ",score);
  309. Pos(64,11);
  310. printf("每个食物得分:%d分",add);j
  311. if(GetAsyncKeyState(VK_UP) && status!=D)
  312. {
  313. status=U;
  314. }
  315. else if(GetAsyncKeyState(VK_DOWN) && status!=U)
  316. {
  317. status=D;
  318. }
  319. else if(GetAsyncKeyState(VK_LEFT)&& status!=R)
  320. {
  321. status=L;
  322. }
  323. else if(GetAsyncKeyState(VK_RIGHT)&& status!=L)
  324. {
  325. status=R;
  326. }
  327. else if(GetAsyncKeyState(VK_SPACE))
  328. {
  329. pause();
  330. }
  331. else if(GetAsyncKeyState(VK_ESCAPE))
  332. {
  333. endgamestatus=3;
  334. break;
  335. }
  336. else if(GetAsyncKeyState(VK_F1))
  337. {
  338. if(sleeptime>=50)
  339. {
  340. sleeptime=sleeptime-30;
  341. add=add+2;
  342. if(sleeptime==320)
  343. {
  344. add=2;//防止减到1之后再加回来有错
  345. }
  346. }
  347. }
  348. else if(GetAsyncKeyState(VK_F2))
  349. {
  350. if(sleeptime<350)
  351. {
  352. sleeptime=sleeptime+30;
  353. add=add-2;
  354. if(sleeptime==350)
  355. {
  356. add=1; //保证最低分为1
  357. }
  358. }
  359. }
  360. Sleep(sleeptime);
  361. snakemove();
  362. }
  363. }

  364. void welcometogame()//开始界面
  365. {
  366. Pos(40,12);

  367. system("title c语言研究中心");
  368. printf("欢迎来到贪食蛇游戏!");
  369. Pos(40,25);
  370. system("pause");
  371. system("cls");
  372. Pos(25,12);
  373. printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");
  374. Pos(25,13);
  375. printf("加速将能得到更高的分数。\n");
  376. system("pause");
  377. system("cls");
  378. }

  379. void endgame()//结束游戏
  380. {

  381. system("cls");
  382. Pos(24,12);
  383. if(endgamestatus==1)
  384. {
  385. printf("对不起,您撞到墙了。游戏结束.");
  386. }
  387. else if(endgamestatus==2)
  388. {
  389. printf("对不起,您咬到自己了。游戏结束.");
  390. }
  391. else if(endgamestatus==3)
  392. {
  393. printf("您的已经结束了游戏。");
  394. }
  395. Pos(24,13);
  396. printf("您的得分是%d\n",score);
  397. exit(0);
  398. }

  399. void gamestart()//游戏初始化
  400. {
  401. system("mode con cols=100 lines=30");
  402. welcometogame();
  403. creatMap();
  404. initsnake();
  405. createfood();
  406. }

  407. int main()
  408. {
  409. gamestart();
  410. gamecircle();
  411. endgame();
  412. return 0;
  413. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表