[C] 贪吃蛇 →→→→→进入此内容的聊天室

来自 , 2020-03-31, 写在 C, 查看 116 次.
URL http://www.code666.cn/view/1f34004e
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <conio.h>
  7. #define N 21
  8. //●★
  9. int apple[3];
  10. char score[3];
  11. char tail[3];
  12.  
  13. void gotoxy(int x, int y)    //输出坐标
  14. {
  15.         COORD pos;//WINDOWS API中定义的一个结构表示一个字符在控制台屏幕上的坐标
  16.         pos.X = x;
  17.         pos.Y = y;
  18.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  19.                 //使光标 到(x,y)这个位置的函数.
  20. }
  21.  
  22. void color(int b)         //颜色函数
  23. {
  24.     HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;//用来修改命令行窗口的颜色
  25.     SetConsoleTextAttribute(hConsole,b) ;
  26. }
  27.  
  28. int Block(char head[2])   //判断出界
  29. {
  30.         if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))
  31.                         return 1;
  32.                 return 0;
  33. }
  34.  
  35. int Eat(char snake[2])   //吃了苹果
  36. {
  37.         if ((snake[0] == apple[0]) && (snake[1] == apple[1]))
  38.        {
  39.                 apple[0] = apple[1] = apple[2] = 0;
  40.                 gotoxy(N+44,10);
  41.                 color(13);
  42.                 printf("%d",score[0]*10);
  43.                 color(11);
  44.                 return 1;
  45.            }
  46.         return 0;
  47. }
  48.  
  49. void Draw(char **snake, int len)    //蛇移动
  50. {
  51.         if (apple[2])
  52.                 {
  53.                 gotoxy(apple[1] * 2, apple[0]);
  54.                 color(12);
  55.                 printf("●");
  56.                 color(18);//轨迹颜色
  57.         }
  58.         gotoxy(tail[1] * 2, tail[0]);
  59.         if (tail[2])
  60.          {  
  61.                         color(14);
  62.             printf("★");
  63.             color(11);
  64.          }
  65.     else
  66.         printf("■");
  67.         gotoxy(snake[0][1] * 2, snake[0][0]);
  68.         color(14);
  69.         printf("★");
  70.         color(11);
  71.         putchar('\n');
  72. }
  73.  
  74. char** Move(char **snake, char dirx, int *len)   //控制方向
  75. {
  76.         int i, full = Eat(snake[0]);
  77.         memcpy(tail, snake[(*len)-1], 2);
  78.         for (i = (*len) - 1; i > 0; --i)
  79.                 memcpy(snake[i], snake[i-1], 2);
  80.         switch (dirx)
  81.           {
  82.            case 'w': case 'W': --snake[0][0]; break;
  83.            case 's': case 'S': ++snake[0][0]; break;
  84.            case 'a': case 'A': --snake[0][1]; break;
  85.            case 'd': case 'D': ++snake[0][1]; break;
  86.            default: ;
  87.          }  
  88.         if (full)  
  89.            {
  90.                 snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));
  91.                 snake[(*len)] = (char *)malloc(sizeof(char) * 2);
  92.                 memcpy(snake[(*len)], tail, 2);
  93.                 ++(*len);
  94.                 ++score[0];
  95.                 if(score[3] < 16)
  96.                 ++score[3];
  97.                 tail[2] = 1;
  98.            }
  99.            else
  100.                 tail[2] = 0;
  101.                 return snake;
  102. }
  103.  
  104. void init(char plate[N+2][N+2], char ***snake_x, int *len)  //初始化
  105. {
  106.         int i, j;
  107.         char **snake = NULL;
  108.  
  109.         *len = 3;
  110.         score[0] = score[3] =3;
  111.         snake = (char **)realloc(snake, sizeof(char *) * (*len));
  112.         for (i = 0; i < *len; ++i)
  113.                 snake[i] = (char *)malloc(sizeof(char) * 2);
  114.                
  115.         for (i = 0; i < 3; ++i)
  116.                 {
  117.                 snake[i][0] = N/2 + 1;
  118.                 snake[i][1] = N/2 + 1 + i;
  119.              }
  120.              
  121.         for (i = 1; i <= N; ++i)
  122.                 for (j = 1; j <= N; ++j)
  123.                         plate[i][j] = 1;
  124.                          
  125.         apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;
  126.         apple[2] = 1;
  127.  
  128.         for (i = 0; i < N + 2; ++i)
  129.                 {
  130.                 gotoxy(0, i);
  131.                 for (j = 0; j < N + 2; ++j)
  132.                         {
  133.                         switch (plate[i][j])
  134.                                 {
  135.                              case 0:
  136.                                  color(12);printf("□");color(11); continue;
  137.                         case 1: printf("■"); continue;
  138.                              default: ;
  139.                              }
  140.                     }
  141.                    putchar('\n');
  142.             }
  143.         for (i = 0; i < (*len); ++i)
  144.        {
  145.                 gotoxy(snake[i][1] * 2, snake[i][0]);
  146.                 printf("★");
  147.             }
  148.         putchar('\n');
  149.         *snake_x = snake;
  150. }
  151.  
  152. void Manual()
  153. {
  154.         gotoxy(N+30,2);
  155.         color(10);
  156.         printf("按 W S A D 移动方向");
  157.         gotoxy(N+30,4);
  158.         printf("按 space 键暂停");
  159.         gotoxy(N+30,8);
  160.         color(11);
  161.         printf("历史最高分为: ");
  162.         color(12);
  163.         gotoxy(N+44,8);
  164.         printf("%d",score[1]*10);
  165.         color(11);
  166.         gotoxy(N+30,12);
  167.         printf("你现在得分为: 0");        
  168. }
  169.  
  170. int File_in()     //取记录的分数
  171. {
  172.    FILE *fp;
  173.    if((fp = fopen("C:\\tcs.txt","a+")) == NULL)
  174.    {
  175.             gotoxy(N+18, N+2);
  176.      printf("文件不能打开\n");
  177.          exit(0);
  178.    }
  179.    if((score[1] = fgetc(fp)) != EOF);
  180.    else
  181.    score[1] = 0;
  182.    return 0;
  183. }
  184.  
  185. int File_out()    //存数据
  186. {
  187.        
  188.         FILE *fp;
  189.         if(score[1] > score[0])
  190.         {gotoxy(10,10);
  191.         color(12);
  192.         puts("Demi 加油哦");
  193.         gotoxy(0,N+2);
  194.         return 0;
  195.         }
  196.         if((fp = fopen("C:\\tcs.txt","w+")) == NULL)
  197.         {
  198.                 printf("文件不能打开\n");
  199.                 exit(0);
  200.         }
  201.     if(fputc(--score[0],fp)==EOF)
  202.            printf("输出失败\n");
  203.     gotoxy(10,10);
  204.         color(12);
  205.         puts("恭喜Demi打破记录");
  206.         gotoxy(0,N+2);
  207.         return 0;
  208. }
  209.  
  210.  
  211. void Free(char **snake, int len)    //释放空间
  212. {
  213.         int i;
  214.         for (i = 0; i < len; ++i)
  215.                 free(snake[i]);
  216.         free(snake);
  217. }
  218.  
  219. int main(void)
  220. {
  221.         int len;
  222.         char ch = 'g';
  223.         char a[N+2][N+2] = {{0}};
  224.         char **snake;
  225.         srand((unsigned)time(NULL));
  226.         color(11);
  227.         File_in();
  228.         init(a, &snake, &len);
  229.         Manual();
  230.         while (ch != 0x1B)   // 按 ESC 结束
  231.          {
  232.                 Draw(snake, len);
  233.                 if (!apple[2]) {
  234.                         apple[0] = rand()%N + 1;
  235.                         apple[1] = rand()%N + 1;
  236.                         apple[2] = 1;
  237.                 }
  238.                 Sleep(200-score[3]*10);
  239.                 setbuf(stdin, NULL);
  240.                 if (kbhit())
  241.                    {
  242.                         gotoxy(0, N+2);
  243.                         ch = getche();
  244.                     }
  245.                  snake = Move(snake, ch, &len);
  246.                  if (Block(snake[0])==1)
  247.                   {
  248.                         gotoxy(N+2, N+2);
  249.                         puts("你输了");
  250.                         File_out();
  251.                         Free(snake, len);
  252.                         getche();
  253.                         exit(0);
  254.                   }                        
  255.         }
  256.         Free(snake, len);
  257.         exit(0);
  258. }

回复 "贪吃蛇"

这儿你可以回复上面这条便签

captcha