经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言编程入门:控制台字符界面贪吃蛇小游戏制作!
来源:cnblogs  作者:大天使维迦  时间:2021/3/1 9:20:34  对本文有异议

游戏的实现主要是对一个二维数组按一定逻辑进行修改、变换。这里不对编写过程进行赘述,主要说一下最基本功能的逻辑、和一些之前较少用的函数等。

效果图:

一、 基本功能逻辑
1、游戏的背景、打印

定义一个二维字符串,用“”和空格表示边界、蛇身、空白等。打印是用for循环遍历整个字符串,并以一定频率刷新,就可以达到游戏效果。

2、建立蛇数组

考虑到没用链表做过东西,不太熟练,我采用了数组来做蛇。数组主要有容量有限,最长长度需要先定义(只要我定的足够长hhhh),以及很多地方需要取地址(N次打掉了”&“)等缺点。数组存储蛇的节数、XY坐标、移动方向等参数。主要需要注意“”占两个字节,在写坐标时很多地方要乘二。

3、生成蛇的随机坐标

首先种随机种子,采用系统时间做种子。定义x、y两个变量作为坐标值,用rand()函数搭配取余来获得想要的坐标值范围。然后初始生成两三节就可以了。

4、把蛇画到地图上

建立for循环遍历整条蛇,利用strncpy()函数将空白部分复制为“”就行了。

5、蛇的运动

这里卡了比较久,期间去玩了玩贪吃蛇,发现蛇的运动方式不是很复杂,可以说就是蛇尾去一个,蛇头加一个。我采用了整个蛇身向前移,蛇头单独处理的方法,这样也便于以后控制方向。

6、擦除运动轨迹

写到上一步运行会发现蛇越来越长。。。。就像死机了以后的鼠标光标一样。。。。是因为虽然前一节点的属性赋给了后一个节点,但是这个节点并没有变。所以在每次运动前把之前的蛇擦掉,方法同第四步,只是把“”换成两个空格。

7、蛇改变方向

由于蛇运动方式的特殊性,只需要对蛇头处理。用GetAsyncKeyState()函数读取键盘输入,并需要注意通过附加条件防止蛇掉头。

8、生成食物

随机坐标、复制、打印。

9、蛇吃食物长长

蛇运动到食物的地方会把食物覆盖掉,所以吃掉食物的效果不用写。只用判断蛇头坐标和食物坐标重合,然后判断运动方向来确定在哪里加一节就行了。然后用一个布尔值判断场上是否还有食物,来生成新的食物。计分也可以在此处写。

代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>#include <math.h>#include <conio.h>#include <time.h>#include <windows.h>#define MAXWIDTH 30#define MAXHEIGHT 30#define INITLEN 3 //贪吃蛇的初始长度 struct{
  2.  
  3. char *ch; int color; char type;
  4. }
  5. charBorder = { "", 4, 1 }, //边框charBg = { "", 2, 2 }, //背景charSnake = { "", 0xe, 3 }, //贪吃蛇节点charFood = { "", 0xc, 4 }; //食物//用一个结构体数组保存地图中的各个点struct{
  6.  
  7. char type; int index;
  8. }globalMap[MAXWIDTH][MAXHEIGHT];struct{
  9. int x; int y;
  10. } snakeMap[(MAXWIDTH - 2)*(MAXHEIGHT - 2)], scoresPostion;int scores = 0; //得分int snakeMapLen = (MAXWIDTH - 2)*(MAXHEIGHT - 2);int headerIndex, tailIndex;
  11. HANDLE hStdin;
  12. // 设置光标位置,x为行,y为列void setPosition(int x, int y){
  13. COORD coord;
  14. coord.X = 2 * y;
  15. coord.Y = x;
  16. SetConsoleCursorPosition(hStdin, coord);
  17. }// 设置颜色void setColor(int color){
  18. SetConsoleTextAttribute(hStdin, color);
  19. }//创建食物void createFood(){ int index, rang, x, y;
  20. srand((unsigned)time(NULL)); if (tailIndex<headerIndex){
  21. rang = headerIndex - tailIndex - 1;
  22. index = rand() % rang + tailIndex + 1;
  23. } else{
  24. rang = snakeMapLen - (tailIndex - headerIndex + 1);
  25. index = rand() % rang; if (index >= headerIndex){
  26. index += (tailIndex - headerIndex + 1);
  27. }
  28. }
  29. x = snakeMap[index].x;
  30. y = snakeMap[index].y;
  31. setPosition(x, y);
  32. setColor(charFood.color); printf("%s", charFood.ch);
  33. globalMap[x][y].type = charFood.type;
  34. }//死了void die(){ int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
  35. setPosition(xCenter, yCenter - 5);
  36. setColor(0xC); exit(1);
  37. _getch(); exit(0);
  38. }// 蛇移动void move(char direction){ int newHeaderX, newHeaderY; //新蛇头的坐标
  39.  
  40. int newHeaderPreIndex; //新蛇头坐标以前对应的索引
  41.  
  42. int newHeaderPreX, newHeaderPreY; //新蛇头的索引以前对应的坐标
  43.  
  44. int newHeaderPreType; //新蛇头以前的类型
  45.  
  46. int oldTailX, oldTailY; //老蛇尾坐标
  47.  
  48. switch (direction){ case 'w':
  49. newHeaderX = snakeMap[headerIndex].x - 1;
  50. newHeaderY = snakeMap[headerIndex].y; break; case 's':
  51. newHeaderX = snakeMap[headerIndex].x + 1;
  52. newHeaderY = snakeMap[headerIndex].y; break; case 'a':
  53. newHeaderX = snakeMap[headerIndex].x;
  54. newHeaderY = snakeMap[headerIndex].y - 1; break; case 'd':
  55. newHeaderX = snakeMap[headerIndex].x;
  56. newHeaderY = snakeMap[headerIndex].y + 1; break;
  57. }
  58. headerIndex = headerIndex == 0 ? snakeMapLen - 1 : headerIndex - 1;
  59. newHeaderPreIndex = globalMap[newHeaderX][newHeaderY].index;
  60. newHeaderPreX = snakeMap[headerIndex].x;
  61. newHeaderPreY = snakeMap[headerIndex].y;
  62. snakeMap[headerIndex].x = newHeaderX;
  63. snakeMap[headerIndex].y = newHeaderY;
  64. globalMap[newHeaderX][newHeaderY].index = headerIndex;
  65. snakeMap[newHeaderPreIndex].x = newHeaderPreX;
  66. snakeMap[newHeaderPreIndex].y = newHeaderPreY;
  67. globalMap[newHeaderPreX][newHeaderPreY].index = newHeaderPreIndex; //新蛇头以前的类型
  68. newHeaderPreType = globalMap[newHeaderX][newHeaderY].type; //设置新蛇头类型
  69. globalMap[newHeaderX][newHeaderY].type = charSnake.type; // 判断是否出界或撞到自己
  70.  
  71. if (newHeaderPreType == charBorder.type || newHeaderPreType == charSnake.type){
  72. die();
  73. } //输出新蛇头
  74. setPosition(newHeaderX, newHeaderY);
  75. setColor(charSnake.color); printf("%s", charSnake.ch); //判断是否吃到食物
  76.  
  77. if (newHeaderPreType == charFood.type){ //吃到食物
  78. createFood(); //更改分数
  79. setPosition(scoresPostion.x, scoresPostion.y); printf("%d", ++scores);
  80. } else{ //老蛇尾坐标
  81. oldTailX = snakeMap[tailIndex].x;
  82. oldTailY = snakeMap[tailIndex].y; //删除蛇尾
  83. setPosition(oldTailX, oldTailY);
  84. setColor(charBg.color); printf("%s", charBg.ch);
  85. globalMap[oldTailX][oldTailY].type = charBg.type;
  86. tailIndex = (tailIndex == 0) ? snakeMapLen - 1 : tailIndex - 1;
  87. }
  88. }//下次移动的方向char nextDirection(char ch, char directionOld){ int sum = ch + directionOld;
  89. ch = tolower(ch); if ((ch == 'w' || ch == 'a' || ch == 's' || ch == 'd') && sum != 197 && sum != 234){ return ch;
  90. } else{ return directionOld;
  91. }
  92. }//暂停char pause(){ return _getch();
  93. }// 初始化void init(){ // 设置相关变量
  94.  
  95. int x, y, index; int xCenter = MAXHEIGHT % 2 == 0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter = MAXWIDTH % 2 == 0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
  96. CONSOLE_CURSOR_INFO cci; //控制台光标信息
  97. //判断相关设置是否合理
  98.  
  99. if (MAXWIDTH<16){ printf("'MAXWIDTH' is too small!");
  100. _getch(); exit(0);
  101. } //设置窗口大小
  102. system("mode con: cols=96 lines=32"); //隐藏光标
  103. hStdin = GetStdHandle(STD_OUTPUT_HANDLE);
  104. GetConsoleCursorInfo(hStdin, &cci);
  105. cci.bVisible = 0;
  106. SetConsoleCursorInfo(hStdin, &cci); //打印背景
  107.  
  108. for (x = 0; x<MAXHEIGHT; x++){ for (y = 0; y<MAXWIDTH; y++){ if (y == 0 || y == MAXWIDTH - 1 || x == 0 || x == MAXHEIGHT - 1){
  109. globalMap[x][y].type = charBorder.type;
  110. setColor(charBorder.color); printf("%s", charBorder.ch);
  111. } else{
  112. index = (x - 1)*(MAXWIDTH - 2) + (y - 1);
  113. snakeMap[index].x = x;
  114. snakeMap[index].y = y;
  115. globalMap[x][y].type = charBg.type;
  116. globalMap[x][y].index = index;
  117. setColor(charBg.color); printf("%s", charBg.ch);
  118. }
  119. } printf("\n");
  120. } //初始化贪吃蛇
  121. globalMap[xCenter][yCenter - 1].type = globalMap[xCenter][yCenter].type = globalMap[xCenter][yCenter + 1].type = charSnake.type;
  122. headerIndex = (xCenter - 1)*(MAXWIDTH - 2) + (yCenter - 1) - 1;
  123. tailIndex = headerIndex + 2;
  124. setPosition(xCenter, yCenter - 1);
  125. setColor(charSnake.color); for (y = yCenter - 1; y <= yCenter + 1; y++){ printf("%s", charSnake.ch);
  126. } //生成食物
  127. createFood(); //设置程序信息
  128. setPosition(xCenter - 1, MAXWIDTH + 2); printf(" 得分 : 0");
  129. setPosition(xCenter, MAXWIDTH + 2); printf(" 姓名班级 :33班杨超");
  130. scoresPostion.x = xCenter - 1;
  131. scoresPostion.y = MAXWIDTH + 8;
  132. }int main(){ char charInput, direction = 'a';
  133. init();
  134. charInput = tolower(_getch());
  135. direction = nextDirection(charInput, direction); while (1){ if (_kbhit()){
  136. charInput = tolower(_getch()); if (charInput == ' '){
  137. charInput = pause();
  138. }
  139. direction = nextDirection(charInput, direction);
  140. }
  141. move(direction);
  142. Sleep(500);
  143. }
  144. _getch(); return 0;
  145. }

 

另外如果你想更好的提升你的编程能力,学好C语言C++编程!弯道超车,快人一步!笔者这里或许可以帮到你~

分享(源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

免费学习书籍:


免费学习资料:

原文链接:http://www.cnblogs.com/zuishuaideou/p/14453909.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号