当前位置:   article > 正文

贪吃蛇_代码+解析_贪吃蛇代码

贪吃蛇代码

头文件

宏定义

一大堆变量

颜色及移动光标函数

打印游戏规则

初始化

画地图

画蛇

移动函数

擦除尾巴

输入

放置食物

增加长度

GAME OVER

主函数

完整代码

头文件

  1. #include<windows.h>
  2. #include<stdlib.h>
  3. #include<fstream>
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<time.h>

宏定义

  1. #define UP 0 //上
  2. #define DOWN 1 //下
  3. #define LEFT 2 //左
  4. #define RIGHT 3 //右
  5. #define X 23 //X坐标
  6. #define Y 40 //Y坐标
  7. #define MAXLEN 200 //最大长度
  8. #define MINTIME 75 //等待时间

一大堆变量

  1. unsigned int snake[MAXLEN][2];//蛇的坐标
  2. unsigned int len; //长度
  3. unsigned int lastt[2]; //蛇尾坐标
  4. unsigned int score; //得分
  5. unsigned int max_score; //最大得分
  6. unsigned int way; //方向
  7. double wait; //等待时间
  8. int input; //输入
  9. unsigned int food[2]; //食物信息
  10. bool empty=true; //食物空

颜色及移动光标函数

  1. void color(int _color){
  2. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
  3. return;
  4. }
  5. void gotoxy(int xx,int yy){
  6. COORD position={yy*2,xx};
  7. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
  8. return;
  9. }

打印游戏规则

  1. void welcome(){
  2. printf("游戏规则:\n");
  3. printf("w,a,s,d,控制移动\n");
  4. getch();
  5. system("cls");
  6. return;
  7. }

初始化

  1. void init(){
  2. system("title 贪吃蛇");//标题
  3. CONSOLE_CURSOR_INFO cursor_info={1,0}; //隐藏
  4. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);//光标
  5. fstream in("score.txt",ios::in);
  6. if(!in.fail())
  7. in>>max_score;//读取存档
  8. else
  9. welcome();//输出规则
  10. in.close();
  11. len=4; //以下代码设置设定蛇的初始信息
  12. snake[0][0]=X>>1;
  13. snake[0][1]=Y>>1;
  14. snake[1][0]=(X>>1)+1;
  15. snake[1][1]=Y>>1;
  16. snake[2][0]=(X>>1)+2;
  17. snake[2][1]=Y>>1;
  18. snake[3][0]=(X>>1)+3;
  19. snake[3][1]=Y>>1;
  20. way=UP;
  21. wait=150.0;
  22. return;
  23. }

画地图

  1. void drawmap(){
  2. color(255);
  3. for(unsigned int xx=0;xx<X;xx++){
  4. gotoxy(xx,0);
  5. printf(" ");
  6. gotoxy(xx,Y-1);
  7. printf(" ");
  8. }
  9. for(unsigned int yy=0;yy<Y;yy++){
  10. gotoxy(0,yy);
  11. printf(" ");
  12. gotoxy(X-1,yy);
  13. printf(" ");
  14. }
  15. return;
  16. }

画蛇

  1. void drawsnake(){
  2. color(255);
  3. gotoxy(0,0);
  4. printf(" ");
  5. color(10);
  6. gotoxy(snake[0][0],snake[0][1]);
  7. printf("□");
  8. gotoxy(snake[1][0],snake[1][1]);
  9. printf("■");
  10. return;
  11. }

移动函数

  1. void move(){
  2. lastt[0]=snake[len-1][0];
  3. lastt[1]=snake[len-1][1];
  4. for(unsigned int tc=len-1;tc>0;tc--){
  5. snake[tc][0]=snake[tc-1][0];
  6. snake[tc][1]=snake[tc-1][1];
  7. }
  8. switch(way){
  9. case UP:{
  10. snake[0][0]--;
  11. break;
  12. }
  13. case DOWN:{
  14. snake[0][0]++;
  15. break;
  16. }
  17. case LEFT:{
  18. snake[0][1]--;
  19. break;
  20. }
  21. case RIGHT:{
  22. snake[0][1]++;
  23. break;
  24. }
  25. }
  26. return;
  27. }

擦除尾巴

  1. void clt(){
  2. color(0);
  3. gotoxy(lastt[0],lastt[1]);
  4. printf(" ");
  5. return;
  6. }

输入

  1. void getin(){
  2. if(kbhit()!=0){
  3. while(kbhit()!=0)
  4. input=getch();
  5. switch(input){
  6. case 'W':case 'w':{
  7. if(way!=DOWN)
  8. way=UP;
  9. break;
  10. }
  11. case 'S':case 's':{
  12. if(way!=UP)
  13. way=DOWN;
  14. break;
  15. }
  16. case 'A':case 'a':{
  17. if(way!=RIGHT)
  18. way=LEFT;
  19. break;
  20. }
  21. case 'D':case 'd':{
  22. if(way!=LEFT)
  23. way=RIGHT;
  24. break;
  25. }
  26. }
  27. }
  28. return;
  29. }

放置食物

  1. void putfood(){
  2. if(empty){
  3. bool flag=true;
  4. srand(time(NULL));
  5. while(flag){
  6. food[0]=rand()%(X-2)+1;
  7. food[1]=rand()%(Y-2)+1;
  8. flag=false;
  9. for(unsigned int tc=0;tc<len;tc++)
  10. if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
  11. flag=true;
  12. }
  13. empty=false;
  14. color(14);
  15. gotoxy(food[0],food[1]);
  16. printf("◆");
  17. }
  18. return;
  19. }

增加长度

  1. void eatfood(){
  2. if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
  3. empty=true;
  4. score++;
  5. if(wait>=MINTIME)
  6. wait-=0.5;
  7. if(len<MAXLEN)
  8. len++;
  9. }
  10. return;
  11. }

GAME OVER

  1. void gameover(){
  2. bool over=false;
  3. if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
  4. over=true;
  5. for(int tc=1;tc<len;tc++)
  6. if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
  7. over=true;
  8. if(over==true){
  9. system("cls");
  10. while(kbhit()!=0)
  11. getch();
  12. printf("Game over!\n");
  13. printf("Score:%d\n",score);
  14. printf("Max score:%d\n",max_score);
  15. getch();
  16. system("cls");
  17. printf("Save...\n");
  18. if(score>max_score){
  19. fstream write;
  20. write.open("score.txt",ios::out);
  21. write<<score;
  22. write.close();
  23. max_score=score;
  24. }
  25. exit(0);
  26. }
  27. return;
  28. }

主函数

  1. int main(){
  2. init();
  3. drawmap();
  4. _sleep(3000);
  5. while(true){
  6. clt();
  7. drawsnake();
  8. putfood();
  9. eatfood();
  10. getin();
  11. move();
  12. gameover();
  13. _sleep(int(wait));
  14. }
  15. return 0;
  16. }

完整代码

  1. #include<windows.h>
  2. #include<stdlib.h>
  3. #include<fstream>
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<time.h>
  7. using namespace std;
  8. #define UP 0
  9. #define DOWN 1
  10. #define LEFT 2
  11. #define RIGHT 3
  12. #define X 23
  13. #define Y 40
  14. #define MAXLEN 200
  15. #define MINTIME 75
  16. unsigned int snake[MAXLEN][2];
  17. unsigned int len;
  18. unsigned int lastt[2];
  19. unsigned int score;
  20. unsigned int max_score;
  21. unsigned int way;
  22. double wait;
  23. int input;
  24. unsigned int food[2];
  25. bool empty=true;
  26. void color(int _color){
  27. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
  28. return;
  29. }
  30. void gotoxy(int xx,int yy){
  31. COORD position={yy*2,xx};
  32. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
  33. return;
  34. }
  35. void welcome(){
  36. printf("游戏规则:\n");
  37. printf("w,a,s,d,控制移动\n");
  38. getch();
  39. system("cls");
  40. return;
  41. }
  42. void init(){
  43. system("title 贪吃蛇");
  44. CONSOLE_CURSOR_INFO cursor_info={1,0};
  45. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
  46. fstream in("score.txt",ios::in);
  47. if(!in.fail())
  48. in>>max_score;
  49. else
  50. welcome();
  51. in.close();
  52. len=4;
  53. snake[0][0]=X>>1;
  54. snake[0][1]=Y>>1;
  55. snake[1][0]=(X>>1)+1;
  56. snake[1][1]=Y>>1;
  57. snake[2][0]=(X>>1)+2;
  58. snake[2][1]=Y>>1;
  59. snake[3][0]=(X>>1)+3;
  60. snake[3][1]=Y>>1;
  61. way=UP;
  62. wait=150.0;
  63. return;
  64. }
  65. void drawmap(){
  66. color(255);
  67. for(unsigned int xx=0;xx<X;xx++){
  68. gotoxy(xx,0);
  69. printf(" ");
  70. gotoxy(xx,Y-1);
  71. printf(" ");
  72. }
  73. for(unsigned int yy=0;yy<Y;yy++){
  74. gotoxy(0,yy);
  75. printf(" ");
  76. gotoxy(X-1,yy);
  77. printf(" ");
  78. }
  79. return;
  80. }
  81. void drawsnake(){
  82. color(255);
  83. gotoxy(0,0);
  84. printf(" ");
  85. color(10);
  86. gotoxy(snake[0][0],snake[0][1]);
  87. printf("□");
  88. gotoxy(snake[1][0],snake[1][1]);
  89. printf("■");
  90. return;
  91. }
  92. void move(){
  93. lastt[0]=snake[len-1][0];
  94. lastt[1]=snake[len-1][1];
  95. for(unsigned int tc=len-1;tc>0;tc--){
  96. snake[tc][0]=snake[tc-1][0];
  97. snake[tc][1]=snake[tc-1][1];
  98. }
  99. switch(way){
  100. case UP:{
  101. snake[0][0]--;
  102. break;
  103. }
  104. case DOWN:{
  105. snake[0][0]++;
  106. break;
  107. }
  108. case LEFT:{
  109. snake[0][1]--;
  110. break;
  111. }
  112. case RIGHT:{
  113. snake[0][1]++;
  114. break;
  115. }
  116. }
  117. return;
  118. }
  119. void clt(){
  120. color(0);
  121. gotoxy(lastt[0],lastt[1]);
  122. printf(" ");
  123. return;
  124. }
  125. void getin(){
  126. if(kbhit()!=0){
  127. while(kbhit()!=0)
  128. input=getch();
  129. switch(input){
  130. case 'W':case 'w':{
  131. if(way!=DOWN)
  132. way=UP;
  133. break;
  134. }
  135. case 'S':case 's':{
  136. if(way!=UP)
  137. way=DOWN;
  138. break;
  139. }
  140. case 'A':case 'a':{
  141. if(way!=RIGHT)
  142. way=LEFT;
  143. break;
  144. }
  145. case 'D':case 'd':{
  146. if(way!=LEFT)
  147. way=RIGHT;
  148. break;
  149. }
  150. }
  151. }
  152. return;
  153. }
  154. void putfood(){
  155. if(empty){
  156. bool flag=true;
  157. srand(time(NULL));
  158. while(flag){
  159. food[0]=rand()%(X-2)+1;
  160. food[1]=rand()%(Y-2)+1;
  161. flag=false;
  162. for(unsigned int tc=0;tc<len;tc++)
  163. if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
  164. flag=true;
  165. }
  166. empty=false;
  167. color(14);
  168. gotoxy(food[0],food[1]);
  169. printf("◆");
  170. }
  171. return;
  172. }
  173. void eatfood(){
  174. if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
  175. empty=true;
  176. score++;
  177. if(wait>=MINTIME)
  178. wait-=0.5;
  179. if(len<MAXLEN)
  180. len++;
  181. }
  182. return;
  183. }
  184. void gameover(){
  185. bool over=false;
  186. if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
  187. over=true;
  188. for(int tc=1;tc<len;tc++)
  189. if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
  190. over=true;
  191. if(over==true){
  192. system("cls");
  193. while(kbhit()!=0)
  194. getch();
  195. printf("Game over!\n");
  196. printf("Score:%d\n",score);
  197. printf("Max score:%d\n",max_score);
  198. getch();
  199. system("cls");
  200. printf("Save...\n");
  201. if(score>max_score){
  202. fstream write;
  203. write.open("score.txt",ios::out);
  204. write<<score;
  205. write.close();
  206. max_score=score;
  207. }
  208. exit(0);
  209. }
  210. return;
  211. }
  212. int main(){
  213. init();
  214. drawmap();
  215. _sleep(3000);
  216. while(true){
  217. clt();
  218. drawsnake();
  219. putfood();
  220. eatfood();
  221. getin();
  222. move();
  223. gameover();
  224. _sleep(int(wait));
  225. }
  226. return 0;
  227. }

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

闽ICP备14008679号