当前位置:   article > 正文

C语言---双人贪吃蛇_有两条的贪吃蛇c程序代码

有两条的贪吃蛇c程序代码

贪吃蛇双人小游戏,每局游戏两分钟,死亡则直接失败,若时间结束,则分高者获胜。 

 上源代码:

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. #include <time.h>
  5. #include<stdbool.h>
  6. #include <conio.h>
  7. #define SNAKESIZE 100
  8. #define MAPWIDTH 118
  9. #define MAPHEIGHT 29
  10. struct { //保存食物坐标
  11. int x;
  12. int y;
  13. }food;
  14. struct {
  15. int len;
  16. int x[SNAKESIZE];
  17. int y[SNAKESIZE];
  18. }snake;
  19. struct {
  20. int len;
  21. int x[SNAKESIZE];
  22. int y[SNAKESIZE];
  23. }snake1;
  24. char key = '8';//初始方向向上
  25. char key1 = 'w';
  26. int changeFlag = 0 , changeFlag1 = 0;
  27. int speed=150 , sorce = 0 , sorce1 = 0 , sec=0 , min=2;
  28. void gotoxy(int x, int y)//移动光标到指定位置
  29. {
  30. COORD coord;
  31. coord.X = x;
  32. coord.Y = y;
  33. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  34. }
  35. void MAP()//打印边框和两条蛇的起始位置
  36. {
  37. for (int i = 0; i <= MAPWIDTH; i += 2)//打印最上面和最下面两横边框
  38. {
  39. gotoxy(i, 0);
  40. printf("■");
  41. gotoxy(i, MAPHEIGHT);
  42. printf("■");
  43. }
  44. for (int i = 1; i < MAPHEIGHT; i++)//打印最左面和最右面
  45. {
  46. gotoxy(0, i);
  47. printf("■");
  48. gotoxy(MAPWIDTH, i);
  49. printf("■");
  50. }
  51. while (1)//打印一个食物
  52. {
  53. srand((unsigned int)time(NULL));
  54. food.x = rand() % (MAPWIDTH - 4) + 2;
  55. food.y = rand() % (MAPHEIGHT - 2) + 1;
  56. if (food.x % 2 == 0)
  57. break;
  58. }
  59. gotoxy(food.x, food.y);
  60. printf("★");
  61. snake.len = snake1.len = 4;//给两条蛇的长度赋初值
  62. snake.x[0] = MAPWIDTH / 2 + 31;//然后分别打印两条蛇身部分
  63. snake.y[0] = MAPHEIGHT / 2;
  64. snake1.x[0] = MAPWIDTH / 2 -31;
  65. snake1.y[0] = MAPHEIGHT / 2;
  66. gotoxy(snake.x[0], snake.y[0]);
  67. printf("■");
  68. gotoxy(snake1.x[0], snake1.y[0]);
  69. printf("●");
  70. for (int i = 1; i < snake.len; i++)
  71. {
  72. snake.x[i] = snake.x[i - 1];
  73. snake.y[i] = snake.y[i - 1]+1;
  74. gotoxy(snake.x[i], snake.y[i]);
  75. printf("■");
  76. snake1.x[i] = snake1.x[i - 1];
  77. snake1.y[i] = snake1.y[i - 1]+1;
  78. gotoxy(snake1.x[i], snake1.y[i]);
  79. printf("●");
  80. }
  81. gotoxy(MAPWIDTH , 0);//把光标移走
  82. return;
  83. }
  84. void OPERATION()//操作函数
  85. {
  86. char pre_key = key , pre_key1 = key1 , s;//保存两条蛇上一次的方向
  87. if (_kbhit())
  88. {
  89. s = getch();
  90. if(s=='w'||s=='s'||s=='a'||s=='d'||s=='W'||s=='S'||s=='A'||s=='D')
  91. key1=s;
  92. else if(s=='8'||s=='5'||s=='4'||s=='6')
  93. key=s;
  94. }
  95. if (changeFlag == 0)//没吃到食物
  96. {
  97. gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);
  98. printf(" ");//在蛇尾处输出空格即擦去蛇尾
  99. }
  100. if (changeFlag1 == 0)
  101. {
  102. gotoxy(snake1.x[snake1.len - 1], snake1.y[snake1.len - 1]);
  103. printf(" ");//在蛇尾处输出空格即擦去蛇尾
  104. }
  105. //将蛇的每一节依次向前移动一节(蛇头除外)
  106. for (int i = snake.len - 1; i > 0; i--)
  107. {
  108. snake.x[i] = snake.x[i - 1];
  109. snake.y[i] = snake.y[i - 1];
  110. }
  111. for (int i = snake1.len - 1; i > 0; i--)
  112. {
  113. snake1.x[i] = snake1.x[i - 1];
  114. snake1.y[i] = snake1.y[i - 1];
  115. }
  116. //蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走
  117. //如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向
  118. if (pre_key == '8' && key == '5')
  119. key = '8';
  120. if (pre_key == '5' && key == '8')
  121. key = '5';
  122. if (pre_key == '4' && key == '6')
  123. key = '4';
  124. if (pre_key == '6' && key == '4')
  125. key = '6';
  126. if (pre_key1 == 'w' && key1 == 's')
  127. key1 = 'w';
  128. if (pre_key1 == 's' && key1 == 'w')
  129. key1 = 's';
  130. if (pre_key1 == 'a' && key1 == 'd')
  131. key1 = 'a';
  132. if (pre_key1 == 'd' && key1 == 'a')
  133. key1 = 'd';
  134. //判断蛇头应该往哪个方向移动
  135. switch (key)
  136. {
  137. case '4':
  138. snake.x[0] -= 2;//往左
  139. break;
  140. case '6':
  141. snake.x[0] += 2;//往右
  142. break;
  143. case '8':
  144. snake.y[0]--;//往上
  145. break;
  146. case '5':
  147. snake.y[0]++;//往下
  148. break;
  149. }
  150. gotoxy(snake.x[0], snake.y[0]);
  151. printf("■");
  152. changeFlag = 0;
  153. switch (key1)
  154. {
  155. case 'a':
  156. case 'A':
  157. snake1.x[0] -= 2;//往左
  158. break;
  159. case 'd':
  160. case 'D':
  161. snake1.x[0] += 2;//往右
  162. break;
  163. case 'w':
  164. case 'W':
  165. snake1.y[0]--;//往上
  166. break;
  167. case 's':
  168. case 'S':
  169. snake1.y[0]++;//往下
  170. break;
  171. }
  172. gotoxy(snake1.x[0], snake1.y[0]);
  173. printf("●");
  174. changeFlag1 = 0;
  175. gotoxy(MAPWIDTH, 0);
  176. return;
  177. }
  178. void createFood()
  179. {
  180. if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物
  181. {
  182. //蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
  183. while (1)
  184. {
  185. int a = 1 , b=1;
  186. srand((unsigned int)time(NULL));
  187. food.x = rand() % (MAPWIDTH - 4) + 2;
  188. food.y = rand() % (MAPHEIGHT - 2) + 1;
  189. //随机生成的食物不能在蛇的身体上
  190. for (int i = 0; i < snake.len; i++)
  191. {
  192. if (snake.x[i] == food.x && snake.y[i] == food.y)
  193. {
  194. a = 0;
  195. break;
  196. }
  197. }
  198. for (int i = 0; i < snake1.len; i++)
  199. {
  200. if (snake1.x[i] == food.x && snake1.y[i] == food.y)
  201. {
  202. b = 0;
  203. break;
  204. }
  205. }
  206. //随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成
  207. if (a==1&&b==1 && food.x % 2 == 0)
  208. break;
  209. }
  210. //绘制食物
  211. gotoxy(food.x, food.y);
  212. printf("★");
  213. snake.len++;//吃到食物,蛇身长度加1
  214. sorce += 10;
  215. speed -= 5;//随着吃的食物越来越多,速度会越来越快
  216. changeFlag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
  217. }
  218. return;
  219. }
  220. void createFood1()
  221. {
  222. if (snake1.x[0] == food.x && snake1.y[0] == food.y)//蛇头碰到食物
  223. {
  224. //蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
  225. while (1)
  226. {
  227. int a = 1 , b=1;
  228. srand((unsigned int)time(NULL));
  229. food.x = rand() % (MAPWIDTH - 4) + 2;
  230. food.y = rand() % (MAPHEIGHT - 2) + 1;
  231. //随机生成的食物不能在蛇的身体上
  232. for (int i = 0; i < snake.len; i++)
  233. {
  234. if (snake.x[i] == food.x && snake.y[i] == food.y)
  235. {
  236. a = 0;
  237. break;
  238. }
  239. }
  240. for (int i = 0; i < snake1.len; i++)
  241. {
  242. if (snake1.x[i] == food.x && snake1.y[i] == food.y)
  243. {
  244. b = 0;
  245. break;
  246. }
  247. }
  248. //随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成
  249. if (a==1&&b==1&& food.x % 2 == 0)
  250. break;
  251. }
  252. //绘制食物
  253. gotoxy(food.x, food.y);
  254. printf("★");
  255. snake1.len++;//吃到食物,蛇身长度加1
  256. sorce1 += 10;
  257. speed -= 5;//随着吃的食物越来越多,速度会越来越快
  258. changeFlag1 = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
  259. }
  260. return;
  261. }
  262. bool check()
  263. {
  264. //蛇头碰到上下边界,游戏结束
  265. if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT)
  266. return true;
  267. //蛇头碰到左右边界,游戏结束
  268. if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH)
  269. return true;
  270. //蛇头碰到蛇身,游戏结束
  271. for (int i = 1; i < snake.len; i++)
  272. {
  273. if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
  274. return true;
  275. }
  276. for (int i = 0; i < snake1.len; i++)
  277. {
  278. if(snake1.x[i] == snake.x[0]&&snake1.y[i] == snake.y[0])
  279. return true;
  280. }
  281. return false;
  282. }
  283. bool check1()
  284. {
  285. //蛇头碰到上下边界,游戏结束
  286. if (snake1.y[0] == 0 || snake1.y[0] == MAPHEIGHT)
  287. return true;
  288. //蛇头碰到左右边界,游戏结束
  289. if (snake1.x[0] == 0 || snake1.x[0] == MAPWIDTH)
  290. return true;
  291. //蛇头碰到蛇身,游戏结束
  292. for (int i = 1; i < snake1.len; i++)
  293. {
  294. if (snake1.x[i] == snake1.x[0] && snake1.y[i] == snake1.y[0])
  295. return true;
  296. }
  297. for (int i = 0; i < snake.len; i++)
  298. {
  299. if (snake.x[i] == snake1.x[0] && snake.y[i] == snake1.y[0])
  300. return true;
  301. }
  302. return false;
  303. }
  304. void MENU ()//打印菜单界面
  305. {
  306. printf("\n\n\n\n\t\t\t\t ╔═══════════════════════════════════════╗\n");
  307. printf("\t\t\t\t ║ ║\n");
  308. printf("\t\t\t\t ║ 欢迎来到贪吃蛇 ║\n");
  309. printf("\t\t\t\t ║ ║\n");
  310. printf("\t\t\t\t ║ ║\n");
  311. printf("\t\t\t\t ║ ┏━━┓ ┏━━┓ ┏━━┓ ║\n");
  312. printf("\t\t\t\t ║ 开始:┃ 1┃ 规则:┃ 2┃ 退出:┃ 3┃ ║\n");
  313. printf("\t\t\t\t ║ ┗━━┛ ┗━━┛ ┗━━┛ ║\n");
  314. printf("\t\t\t\t ║ ║\n");
  315. printf("\t\t\t\t ║ ║\n");
  316. printf("\t\t\t\t ╚═══════════════════════════════════════╝\n");
  317. switch(getch()){
  318. case '1':
  319. system("cls");
  320. START();
  321. break;
  322. case '2':
  323. system("cls");
  324. RULE();
  325. MENU();
  326. break;
  327. case '3':
  328. exit(0);
  329. break;
  330. default:
  331. system("cls");
  332. printf("error");
  333. MENU();
  334. }
  335. }
  336. void RULE ()
  337. {
  338. system("cls");//清屏
  339. printf("\t╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n");
  340. printf("\t║本游戏玩家一(左侧)通过按键W、S、A、D(不区分大小写)四个键分别控制snake1上移、下移、左移和右移。║\n");
  341. printf("\t║玩家二(右侧)过按键8、5、4、6 四个键分别控制snake2上移、下移、左移和右移。 ║\n");
  342. printf("\t║每局游戏两分钟,死亡则直接失败,若时间结束,则分高者获胜。 ║\n");
  343. printf("\t╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n");
  344. system("pause");//暂停
  345. system("cls");//清屏
  346. }
  347. void START()
  348. {
  349. time_t time_sec = 0;
  350. time_t old_sec = 0;
  351. MAP();
  352. system("pause");
  353. time(&time_sec); //获取时间
  354. old_sec = time_sec; //保存时间
  355. while (!check()&&!check1())
  356. {
  357. OPERATION();
  358. createFood();
  359. createFood1();
  360. Sleep(speed);
  361. time(&time_sec); //获取秒数保存到time_t变量
  362. if(time_sec != old_sec) //如果秒数改变(计时达到1秒)
  363. {
  364. old_sec = time_sec; //更新旧的秒数
  365. if(sec > 0)
  366. sec--; //计时秒数减1
  367. else
  368. {
  369. sec = 59; //如果原秒数为0,则变为59
  370. min--; //计时分钟减1
  371. }
  372. }
  373. if(min==0&&sec==0)
  374. break;
  375. if(sec>=8)
  376. printf("\a");
  377. gotoxy(4,1);
  378. printf("倒计时——%02d:%02d\r",min, sec);
  379. }
  380. system("cls");
  381. printf("\n\n\t\t\tGame Over!\n");
  382. system("pause");
  383. }
  384. void OVER ()
  385. {
  386. system("cls");
  387. printf("\n\n\n\n\t\t\t\t╔═══════════════════════════════════════════╗\n");
  388. printf("\t\t\t\t║ ║\n");
  389. printf("\t\t\t\t║ 游戏结束 ║\n");
  390. printf("\t\t\t\t║ ║\n");
  391. printf("\t\t\t\t║ ║\n");
  392. if(check()&&!check1())
  393. printf("\t\t\t\t║ 玩家一(左侧)获胜,玩家二(右侧)失败。 ║\n");
  394. else if(!check()&&check1())
  395. printf("\t\t\t\t║ 玩家一(左侧)失败,玩家二(右侧)获胜。 ║\n");
  396. else
  397. {
  398. printf("\t\t\t\t║ 玩家一本次游戏得分为:%4d ║\n", sorce1);
  399. printf("\t\t\t\t║ 玩家二本次游戏得分为:%4d ║\n", sorce);
  400. if(sorce>sorce1)
  401. printf("\t\t\t\t║ 玩家一(左侧)失败,玩家二(右侧)获胜。 ║\n");
  402. else if(sorce1>sorce)
  403. printf("\t\t\t\t║ 玩家一(左侧)获胜,玩家二(右侧)失败。 ║\n");
  404. else
  405. printf("\t\t\t\t║ 恭喜你们平局了! ║\n");
  406. }
  407. printf("\t\t\t\t║ ║\n");
  408. printf("\t\t\t\t║ ║\n");
  409. printf("\t\t\t\t╚═══════════════════════════════════════════╝\n");
  410. }
  411. int main ()
  412. {
  413. system("title 双人贪吃蛇");
  414. system("color F0");
  415. MENU();
  416. OVER();
  417. return 0;
  418. }
  419. [点击并拖拽以移动]

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

闽ICP备14008679号