鱼C论坛

 找回密码
 立即注册
查看: 2513|回复: 0

[作品展示] C语言贪吃蛇

[复制链接]
发表于 2020-9-5 08:49:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<Windows.h>
  4. #include<conio.h>
  5. #include<time.h>

  6. #define Map_long 20
  7. #define Map_high 15

  8. typedef struct snake{
  9.     int goods_class;
  10.     int direction;
  11. }SNAKE;

  12. /*-------------------------------------------------------*/
  13. /*                    全局变量声明区                      */

  14. /* 二维结构体数组里goods_class的值,0代表活动区域(空格),
  15. * 1代表■(地图砖头),2代表★(食物),3代表●(蛇头)
  16. * 3之后的数字代表◎(蛇身),3+蛇长度-1表示◎(蛇尾)*/
  17. /*direction表示蛇移动的方向,↑8,↓2,←4,→6*/
  18. SNAKE Snake[Map_high][Map_long] = { { 0 } };

  19. unsigned die = 0;  //1表示游戏结束
  20. unsigned snake_move = 0;  //移动成功,用来跳出三重循环
  21. unsigned eat_food = 0;  //1表示碰到食物,2表示用来跳出循环
  22. unsigned snake_long = 3;  //记录蛇的长度

  23. void Move(int ch);  //蛇移动的函数

  24. void HideCursor();  //隐藏光标函数

  25. int main(void)
  26. {
  27.     system("mode con:cols=43 lines=20");  //将控制台设置成长41,高18
  28.     HideCursor();

  29.     /*定义局部变量区域*/
  30.     unsigned i, j;
  31.     unsigned food = 0;  //食物存在的标志,1代表食物存在
  32.     unsigned food_x, food_y;  //产生食物的临时坐标
  33.     unsigned score = 0;  //统计分数
  34.     char ch = '\0';  //用来获取↑↓←→

  35.     /*播种子*/
  36.     srand((unsigned)time(NULL));

  37.     /*初始化地图*/
  38.     for (i = 0; i < Map_long; i++)
  39.     {
  40.         Snake[0][i].goods_class = 1;
  41.         Snake[Map_high - 1][i].goods_class = 1;
  42.     }
  43.     for (i = 0; i < Map_high; i++)
  44.     {
  45.         Snake[i][0].goods_class = 1;
  46.         Snake[i][Map_long - 1].goods_class = 1;
  47.     }

  48.     /*初始化蛇的位置*/
  49.     for (i = 0; i < snake_long; i++)
  50.         Snake[Map_high / 2][Map_long / 2 + i].goods_class = snake_long + i;

  51.     while (1)
  52.     {
  53.         while (!_kbhit())
  54.         {
  55.             switch (ch)
  56.             {
  57.             case 72:Move(8); break;
  58.             case 75:Move(4); break;
  59.             case 77:Move(6); break;
  60.             case 80:Move(2); break;
  61.             default:break;
  62.             }

  63.             if (eat_food == 2)  //2表示吃到食物,开始重置状态
  64.             {
  65.                 food = 0;  //食物没有了
  66.                 eat_food = 0;  //重新判定是否吃到食物
  67.                 snake_long++;  //蛇的长度加1
  68.                 score++;  //增加分数
  69.             }

  70.             /*产生食物*/
  71.             do{
  72.                 if (food == 1)
  73.                     break;
  74.                 food_x = rand() % Map_long;
  75.                 food_y = rand() % Map_high;
  76.                 if (Snake[food_y][food_x].goods_class == 0)
  77.                 {
  78.                     Snake[food_y][food_x].goods_class = 2;
  79.                     food++;
  80.                     break;
  81.                 }
  82.             } while (1);

  83.             printf("Grade:%u\n", score);
  84.             for (i = 0; i < Map_high; i++)
  85.             {
  86.                 if (die == 1)
  87.                     break;

  88.                 for (j = 0; j < Map_long; j++)
  89.                 {
  90.                     switch (Snake[i][j].goods_class)
  91.                     {
  92.                     case 0:printf("  "); break;
  93.                     case 1:printf("■"); break;
  94.                     case 2:printf("★"); break;
  95.                     case 3:printf("●"); break;
  96.                     default:printf("◎"); break;
  97.                     }
  98.                 }
  99.                 putchar('\n');
  100.             }
  101.             printf("Up↑ Down↓ Left← Right→ Other Pause.");

  102.             Sleep(50);
  103.             system("cls");

  104.             if (die == 1)
  105.                 break;
  106.         }
  107.         if (die == 1)
  108.             break;

  109.         do{
  110.             ch = _getch();
  111.         } while (ch != -32 && ch != 80 && ch != 72 && ch != 77 && ch != 75);
  112.         ch = _getch();
  113.     }
  114.     system("cls");
  115.     printf("Game Over,your grade is %u.\n", score);
  116.     getchar();
  117.     return 0;
  118. }

  119. void Move(int ch)
  120. {
  121.     int i, j, n;
  122.     int direction;  //临时存储方向

  123.     for (n = 0; n < snake_long; n++)
  124.     for (i = 0; i < Map_high; i++)
  125.     {
  126.         if (snake_move == 1)
  127.         {
  128.             snake_move = 0;
  129.             break;
  130.         }
  131.         if (eat_food == 2)
  132.             break;
  133.         for (j = 0; j < Map_long; j++)
  134.         if (Snake[i][j].goods_class == 3 + n)
  135.         {
  136.             /*第一次获取按键,初始化蛇的运动状态*/
  137.             if (Snake[i][j].goods_class == 3 && Snake[i][j].direction == 0)
  138.             {
  139.                 Snake[i][j].direction = ch;
  140.                 Snake[i][j + 1].direction = 4;
  141.                 Snake[i][j + 2].direction = 4;
  142.             }
  143.             else if (Snake[i][j].goods_class == 3)  //重置蛇头的方向
  144.                 Snake[i][j].direction = ch;

  145.             /*进行移动前,先判断蛇头是否碰到死亡因子*/
  146.             if (Snake[i][j].goods_class == 3)
  147.             {
  148.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class != 0 && Snake[i - 1][j].goods_class != 2)
  149.                     die++;

  150.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class != 0 && Snake[i + 1][j].goods_class != 2)
  151.                     die++;

  152.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class != 0 && Snake[i][j - 1].goods_class != 2)
  153.                     die++;

  154.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class != 0 && Snake[i][j + 1].goods_class != 2)
  155.                     die++;
  156.             }

  157.             /*进行移动前,先判断蛇头是否碰到食物*/
  158.             if (Snake[i][j].goods_class == 3)
  159.             {
  160.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class == 2)
  161.                     eat_food++;

  162.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class == 2)
  163.                     eat_food++;

  164.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class == 2)
  165.                     eat_food++;

  166.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class == 2)
  167.                     eat_food++;
  168.             }

  169.             if (die == 1)
  170.                 break;

  171.             /*蛇尾的移动*/
  172.             if (Snake[i][j].goods_class == 3 + snake_long - 1)
  173.             {
  174.                 if (eat_food == 1)  //吃到食物就更新蛇尾
  175.                 {
  176.                     if (Snake[i][j].direction == 8)
  177.                     {
  178.                         direction = Snake[i - 1][j].direction;
  179.                         Snake[i - 1][j] = Snake[i][j];
  180.                         Snake[i - 1][j].direction = direction;
  181.                         Snake[i][j].goods_class = 3 + snake_long;
  182.                     }
  183.                     else if (Snake[i][j].direction == 2)
  184.                     {
  185.                         direction = Snake[i + 1][j].direction;
  186.                         Snake[i + 1][j] = Snake[i][j];
  187.                         Snake[i + 1][j].direction = direction;
  188.                         Snake[i][j].goods_class = 3 + snake_long;
  189.                     }
  190.                     else if (Snake[i][j].direction == 4)
  191.                     {
  192.                         direction = Snake[i][j - 1].direction;
  193.                         Snake[i][j - 1] = Snake[i][j];
  194.                         Snake[i][j - 1].direction = direction;
  195.                         Snake[i][j].goods_class = 3 + snake_long;
  196.                     }
  197.                     else if (Snake[i][j].direction == 6)
  198.                     {
  199.                         direction = Snake[i][j + 1].direction;
  200.                         Snake[i][j + 1] = Snake[i][j];
  201.                         Snake[i][j + 1].direction = direction;
  202.                         Snake[i][j].goods_class = 3 + snake_long;
  203.                     }
  204.                     snake_move++;
  205.                     eat_food++;
  206.                     break;
  207.                 }
  208.                 else{
  209.                     if (Snake[i][j].direction == 8)
  210.                     {
  211.                         Snake[i][j].direction = Snake[i - 1][j].direction;
  212.                         Snake[i - 1][j] = Snake[i][j];
  213.                         Snake[i][j].goods_class = 0;
  214.                     }
  215.                     else if (Snake[i][j].direction == 2)
  216.                     {
  217.                         Snake[i][j].direction = Snake[i + 1][j].direction;
  218.                         Snake[i + 1][j] = Snake[i][j];
  219.                         Snake[i][j].goods_class = 0;
  220.                     }
  221.                     else if (Snake[i][j].direction == 4)
  222.                     {
  223.                         Snake[i][j].direction = Snake[i][j - 1].direction;
  224.                         Snake[i][j - 1] = Snake[i][j];
  225.                         Snake[i][j].goods_class = 0;
  226.                     }
  227.                     else if (Snake[i][j].direction == 6)
  228.                     {
  229.                         Snake[i][j].direction = Snake[i][j + 1].direction;
  230.                         Snake[i][j + 1] = Snake[i][j];
  231.                         Snake[i][j].goods_class = 0;
  232.                     }
  233.                     snake_move++;
  234.                     break;
  235.                 }
  236.             }

  237.             /*蛇头的移动*/
  238.             if (Snake[i][j].goods_class == 3)
  239.             {
  240.                 if (Snake[i][j].direction == 8)
  241.                     Snake[i - 1][j] = Snake[i][j];

  242.                 else if (Snake[i][j].direction == 2)
  243.                     Snake[i + 1][j] = Snake[i][j];

  244.                 else if (Snake[i][j].direction == 4)
  245.                     Snake[i][j - 1] = Snake[i][j];

  246.                 else if (Snake[i][j].direction == 6)
  247.                     Snake[i][j + 1] = Snake[i][j];
  248.                 snake_move++;
  249.                 break;
  250.             }

  251.             /*蛇身的移动*/
  252.             if (Snake[i][j].direction == 8)
  253.             {
  254.                 direction = Snake[i - 1][j].direction;
  255.                 Snake[i - 1][j] = Snake[i][j];
  256.                 Snake[i - 1][j].direction = direction;
  257.             }
  258.             else if (Snake[i][j].direction == 2)
  259.             {
  260.                 direction = Snake[i + 1][j].direction;
  261.                 Snake[i + 1][j] = Snake[i][j];
  262.                 Snake[i + 1][j].direction = direction;
  263.             }
  264.             else if (Snake[i][j].direction == 4)
  265.             {
  266.                 direction = Snake[i][j - 1].direction;
  267.                 Snake[i][j - 1] = Snake[i][j];
  268.                 Snake[i][j - 1].direction = direction;
  269.             }
  270.             else if (Snake[i][j].direction == 6)
  271.             {
  272.                 direction = Snake[i][j + 1].direction;
  273.                 Snake[i][j + 1] = Snake[i][j];
  274.                 Snake[i][j + 1].direction = direction;
  275.             }
  276.             snake_move++;
  277.             break;
  278.         }
  279.     }
  280. }

  281. void HideCursor()//隐藏光标函数
  282. {
  283.     CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
  284.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  285. }
  286. #include<stdio.h>
  287. #include<stdlib.h>
  288. #include<Windows.h>
  289. #include<conio.h>
  290. #include<time.h>

  291. #define Map_long 20
  292. #define Map_high 15

  293. typedef struct snake{
  294.     int goods_class;
  295.     int direction;
  296. }SNAKE;

  297. /*-------------------------------------------------------*/
  298. /*                    全局变量声明区                      */

  299. /* 二维结构体数组里goods_class的值,0代表活动区域(空格),
  300. * 1代表■(地图砖头),2代表★(食物),3代表●(蛇头)
  301. * 3之后的数字代表◎(蛇身),3+蛇长度-1表示◎(蛇尾)*/
  302. /*direction表示蛇移动的方向,↑8,↓2,←4,→6*/
  303. SNAKE Snake[Map_high][Map_long] = { { 0 } };

  304. unsigned die = 0;  //1表示游戏结束
  305. unsigned snake_move = 0;  //移动成功,用来跳出三重循环
  306. unsigned eat_food = 0;  //1表示碰到食物,2表示用来跳出循环
  307. unsigned snake_long = 3;  //记录蛇的长度

  308. void Move(int ch);  //蛇移动的函数

  309. void HideCursor();  //隐藏光标函数

  310. int main(void)
  311. {
  312.     system("mode con:cols=43 lines=20");  //将控制台设置成长41,高18
  313.     HideCursor();

  314.     /*定义局部变量区域*/
  315.     unsigned i, j;
  316.     unsigned food = 0;  //食物存在的标志,1代表食物存在
  317.     unsigned food_x, food_y;  //产生食物的临时坐标
  318.     unsigned score = 0;  //统计分数
  319.     char ch = '\0';  //用来获取↑↓←→

  320.     /*播种子*/
  321.     srand((unsigned)time(NULL));

  322.     /*初始化地图*/
  323.     for (i = 0; i < Map_long; i++)
  324.     {
  325.         Snake[0][i].goods_class = 1;
  326.         Snake[Map_high - 1][i].goods_class = 1;
  327.     }
  328.     for (i = 0; i < Map_high; i++)
  329.     {
  330.         Snake[i][0].goods_class = 1;
  331.         Snake[i][Map_long - 1].goods_class = 1;
  332.     }

  333.     /*初始化蛇的位置*/
  334.     for (i = 0; i < snake_long; i++)
  335.         Snake[Map_high / 2][Map_long / 2 + i].goods_class = snake_long + i;

  336.     while (1)
  337.     {
  338.         while (!_kbhit())
  339.         {
  340.             switch (ch)
  341.             {
  342.             case 72:Move(8); break;
  343.             case 75:Move(4); break;
  344.             case 77:Move(6); break;
  345.             case 80:Move(2); break;
  346.             default:break;
  347.             }

  348.             if (eat_food == 2)  //2表示吃到食物,开始重置状态
  349.             {
  350.                 food = 0;  //食物没有了
  351.                 eat_food = 0;  //重新判定是否吃到食物
  352.                 snake_long++;  //蛇的长度加1
  353.                 score++;  //增加分数
  354.             }

  355.             /*产生食物*/
  356.             do{
  357.                 if (food == 1)
  358.                     break;
  359.                 food_x = rand() % Map_long;
  360.                 food_y = rand() % Map_high;
  361.                 if (Snake[food_y][food_x].goods_class == 0)
  362.                 {
  363.                     Snake[food_y][food_x].goods_class = 2;
  364.                     food++;
  365.                     break;
  366.                 }
  367.             } while (1);

  368.             printf("Grade:%u\n", score);
  369.             for (i = 0; i < Map_high; i++)
  370.             {
  371.                 if (die == 1)
  372.                     break;

  373.                 for (j = 0; j < Map_long; j++)
  374.                 {
  375.                     switch (Snake[i][j].goods_class)
  376.                     {
  377.                     case 0:printf("  "); break;
  378.                     case 1:printf("■"); break;
  379.                     case 2:printf("★"); break;
  380.                     case 3:printf("●"); break;
  381.                     default:printf("◎"); break;
  382.                     }
  383.                 }
  384.                 putchar('\n');
  385.             }
  386.             printf("Up↑ Down↓ Left← Right→ Other Pause.");

  387.             Sleep(50);
  388.             system("cls");

  389.             if (die == 1)
  390.                 break;
  391.         }
  392.         if (die == 1)
  393.             break;

  394.         do{
  395.             ch = _getch();
  396.         } while (ch != -32 && ch != 80 && ch != 72 && ch != 77 && ch != 75);
  397.         ch = _getch();
  398.     }
  399.     system("cls");
  400.     printf("Game Over,your grade is %u.\n", score);
  401.     getchar();
  402.     return 0;
  403. }

  404. void Move(int ch)
  405. {
  406.     int i, j, n;
  407.     int direction;  //临时存储方向

  408.     for (n = 0; n < snake_long; n++)
  409.     for (i = 0; i < Map_high; i++)
  410.     {
  411.         if (snake_move == 1)
  412.         {
  413.             snake_move = 0;
  414.             break;
  415.         }
  416.         if (eat_food == 2)
  417.             break;
  418.         for (j = 0; j < Map_long; j++)
  419.         if (Snake[i][j].goods_class == 3 + n)
  420.         {
  421.             /*第一次获取按键,初始化蛇的运动状态*/
  422.             if (Snake[i][j].goods_class == 3 && Snake[i][j].direction == 0)
  423.             {
  424.                 Snake[i][j].direction = ch;
  425.                 Snake[i][j + 1].direction = 4;
  426.                 Snake[i][j + 2].direction = 4;
  427.             }
  428.             else if (Snake[i][j].goods_class == 3)  //重置蛇头的方向
  429.                 Snake[i][j].direction = ch;

  430.             /*进行移动前,先判断蛇头是否碰到死亡因子*/
  431.             if (Snake[i][j].goods_class == 3)
  432.             {
  433.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class != 0 && Snake[i - 1][j].goods_class != 2)
  434.                     die++;

  435.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class != 0 && Snake[i + 1][j].goods_class != 2)
  436.                     die++;

  437.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class != 0 && Snake[i][j - 1].goods_class != 2)
  438.                     die++;

  439.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class != 0 && Snake[i][j + 1].goods_class != 2)
  440.                     die++;
  441.             }

  442.             /*进行移动前,先判断蛇头是否碰到食物*/
  443.             if (Snake[i][j].goods_class == 3)
  444.             {
  445.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class == 2)
  446.                     eat_food++;

  447.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class == 2)
  448.                     eat_food++;

  449.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class == 2)
  450.                     eat_food++;

  451.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class == 2)
  452.                     eat_food++;
  453.             }

  454.             if (die == 1)
  455.                 break;

  456.             /*蛇尾的移动*/
  457.             if (Snake[i][j].goods_class == 3 + snake_long - 1)
  458.             {
  459.                 if (eat_food == 1)  //吃到食物就更新蛇尾
  460.                 {
  461.                     if (Snake[i][j].direction == 8)
  462.                     {
  463.                         direction = Snake[i - 1][j].direction;
  464.                         Snake[i - 1][j] = Snake[i][j];
  465.                         Snake[i - 1][j].direction = direction;
  466.                         Snake[i][j].goods_class = 3 + snake_long;
  467.                     }
  468.                     else if (Snake[i][j].direction == 2)
  469.                     {
  470.                         direction = Snake[i + 1][j].direction;
  471.                         Snake[i + 1][j] = Snake[i][j];
  472.                         Snake[i + 1][j].direction = direction;
  473.                         Snake[i][j].goods_class = 3 + snake_long;
  474.                     }
  475.                     else if (Snake[i][j].direction == 4)
  476.                     {
  477.                         direction = Snake[i][j - 1].direction;
  478.                         Snake[i][j - 1] = Snake[i][j];
  479.                         Snake[i][j - 1].direction = direction;
  480.                         Snake[i][j].goods_class = 3 + snake_long;
  481.                     }
  482.                     else if (Snake[i][j].direction == 6)
  483.                     {
  484.                         direction = Snake[i][j + 1].direction;
  485.                         Snake[i][j + 1] = Snake[i][j];
  486.                         Snake[i][j + 1].direction = direction;
  487.                         Snake[i][j].goods_class = 3 + snake_long;
  488.                     }
  489.                     snake_move++;
  490.                     eat_food++;
  491.                     break;
  492.                 }
  493.                 else{
  494.                     if (Snake[i][j].direction == 8)
  495.                     {
  496.                         Snake[i][j].direction = Snake[i - 1][j].direction;
  497.                         Snake[i - 1][j] = Snake[i][j];
  498.                         Snake[i][j].goods_class = 0;
  499.                     }
  500.                     else if (Snake[i][j].direction == 2)
  501.                     {
  502.                         Snake[i][j].direction = Snake[i + 1][j].direction;
  503.                         Snake[i + 1][j] = Snake[i][j];
  504.                         Snake[i][j].goods_class = 0;
  505.                     }
  506.                     else if (Snake[i][j].direction == 4)
  507.                     {
  508.                         Snake[i][j].direction = Snake[i][j - 1].direction;
  509.                         Snake[i][j - 1] = Snake[i][j];
  510.                         Snake[i][j].goods_class = 0;
  511.                     }
  512.                     else if (Snake[i][j].direction == 6)
  513.                     {
  514.                         Snake[i][j].direction = Snake[i][j + 1].direction;
  515.                         Snake[i][j + 1] = Snake[i][j];
  516.                         Snake[i][j].goods_class = 0;
  517.                     }
  518.                     snake_move++;
  519.                     break;
  520.                 }
  521.             }

  522.             /*蛇头的移动*/
  523.             if (Snake[i][j].goods_class == 3)
  524.             {
  525.                 if (Snake[i][j].direction == 8)
  526.                     Snake[i - 1][j] = Snake[i][j];

  527.                 else if (Snake[i][j].direction == 2)
  528.                     Snake[i + 1][j] = Snake[i][j];

  529.                 else if (Snake[i][j].direction == 4)
  530.                     Snake[i][j - 1] = Snake[i][j];

  531.                 else if (Snake[i][j].direction == 6)
  532.                     Snake[i][j + 1] = Snake[i][j];
  533.                 snake_move++;
  534.                 break;
  535.             }

  536.             /*蛇身的移动*/
  537.             if (Snake[i][j].direction == 8)
  538.             {
  539.                 direction = Snake[i - 1][j].direction;
  540.                 Snake[i - 1][j] = Snake[i][j];
  541.                 Snake[i - 1][j].direction = direction;
  542.             }
  543.             else if (Snake[i][j].direction == 2)
  544.             {
  545.                 direction = Snake[i + 1][j].direction;
  546.                 Snake[i + 1][j] = Snake[i][j];
  547.                 Snake[i + 1][j].direction = direction;
  548.             }
  549.             else if (Snake[i][j].direction == 4)
  550.             {
  551.                 direction = Snake[i][j - 1].direction;
  552.                 Snake[i][j - 1] = Snake[i][j];
  553.                 Snake[i][j - 1].direction = direction;
  554.             }
  555.             else if (Snake[i][j].direction == 6)
  556.             {
  557.                 direction = Snake[i][j + 1].direction;
  558.                 Snake[i][j + 1] = Snake[i][j];
  559.                 Snake[i][j + 1].direction = direction;
  560.             }
  561.             snake_move++;
  562.             break;
  563.         }
  564.     }
  565. }

  566. void HideCursor()//隐藏光标函数
  567. {
  568.     CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
  569.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  570. }
  571. #include<stdio.h>
  572. #include<stdlib.h>
  573. #include<Windows.h>
  574. #include<conio.h>
  575. #include<time.h>

  576. #define Map_long 20
  577. #define Map_high 15

  578. typedef struct snake{
  579.     int goods_class;
  580.     int direction;
  581. }SNAKE;

  582. /*-------------------------------------------------------*/
  583. /*                    全局变量声明区                      */

  584. /* 二维结构体数组里goods_class的值,0代表活动区域(空格),
  585. * 1代表■(地图砖头),2代表★(食物),3代表●(蛇头)
  586. * 3之后的数字代表◎(蛇身),3+蛇长度-1表示◎(蛇尾)*/
  587. /*direction表示蛇移动的方向,↑8,↓2,←4,→6*/
  588. SNAKE Snake[Map_high][Map_long] = { { 0 } };

  589. unsigned die = 0;  //1表示游戏结束
  590. unsigned snake_move = 0;  //移动成功,用来跳出三重循环
  591. unsigned eat_food = 0;  //1表示碰到食物,2表示用来跳出循环
  592. unsigned snake_long = 3;  //记录蛇的长度

  593. void Move(int ch);  //蛇移动的函数

  594. void HideCursor();  //隐藏光标函数

  595. int main(void)
  596. {
  597.     system("mode con:cols=43 lines=20");  //将控制台设置成长41,高18
  598.     HideCursor();

  599.     /*定义局部变量区域*/
  600.     unsigned i, j;
  601.     unsigned food = 0;  //食物存在的标志,1代表食物存在
  602.     unsigned food_x, food_y;  //产生食物的临时坐标
  603.     unsigned score = 0;  //统计分数
  604.     char ch = '\0';  //用来获取↑↓←→

  605.     /*播种子*/
  606.     srand((unsigned)time(NULL));

  607.     /*初始化地图*/
  608.     for (i = 0; i < Map_long; i++)
  609.     {
  610.         Snake[0][i].goods_class = 1;
  611.         Snake[Map_high - 1][i].goods_class = 1;
  612.     }
  613.     for (i = 0; i < Map_high; i++)
  614.     {
  615.         Snake[i][0].goods_class = 1;
  616.         Snake[i][Map_long - 1].goods_class = 1;
  617.     }

  618.     /*初始化蛇的位置*/
  619.     for (i = 0; i < snake_long; i++)
  620.         Snake[Map_high / 2][Map_long / 2 + i].goods_class = snake_long + i;

  621.     while (1)
  622.     {
  623.         while (!_kbhit())
  624.         {
  625.             switch (ch)
  626.             {
  627.             case 72:Move(8); break;
  628.             case 75:Move(4); break;
  629.             case 77:Move(6); break;
  630.             case 80:Move(2); break;
  631.             default:break;
  632.             }

  633.             if (eat_food == 2)  //2表示吃到食物,开始重置状态
  634.             {
  635.                 food = 0;  //食物没有了
  636.                 eat_food = 0;  //重新判定是否吃到食物
  637.                 snake_long++;  //蛇的长度加1
  638.                 score++;  //增加分数
  639.             }

  640.             /*产生食物*/
  641.             do{
  642.                 if (food == 1)
  643.                     break;
  644.                 food_x = rand() % Map_long;
  645.                 food_y = rand() % Map_high;
  646.                 if (Snake[food_y][food_x].goods_class == 0)
  647.                 {
  648.                     Snake[food_y][food_x].goods_class = 2;
  649.                     food++;
  650.                     break;
  651.                 }
  652.             } while (1);

  653.             printf("Grade:%u\n", score);
  654.             for (i = 0; i < Map_high; i++)
  655.             {
  656.                 if (die == 1)
  657.                     break;

  658.                 for (j = 0; j < Map_long; j++)
  659.                 {
  660.                     switch (Snake[i][j].goods_class)
  661.                     {
  662.                     case 0:printf("  "); break;
  663.                     case 1:printf("■"); break;
  664.                     case 2:printf("★"); break;
  665.                     case 3:printf("●"); break;
  666.                     default:printf("◎"); break;
  667.                     }
  668.                 }
  669.                 putchar('\n');
  670.             }
  671.             printf("Up↑ Down↓ Left← Right→ Other Pause.");

  672.             Sleep(50);
  673.             system("cls");

  674.             if (die == 1)
  675.                 break;
  676.         }
  677.         if (die == 1)
  678.             break;

  679.         do{
  680.             ch = _getch();
  681.         } while (ch != -32 && ch != 80 && ch != 72 && ch != 77 && ch != 75);
  682.         ch = _getch();
  683.     }
  684.     system("cls");
  685.     printf("Game Over,your grade is %u.\n", score);
  686.     getchar();
  687.     return 0;
  688. }

  689. void Move(int ch)
  690. {
  691.     int i, j, n;
  692.     int direction;  //临时存储方向

  693.     for (n = 0; n < snake_long; n++)
  694.     for (i = 0; i < Map_high; i++)
  695.     {
  696.         if (snake_move == 1)
  697.         {
  698.             snake_move = 0;
  699.             break;
  700.         }
  701.         if (eat_food == 2)
  702.             break;
  703.         for (j = 0; j < Map_long; j++)
  704.         if (Snake[i][j].goods_class == 3 + n)
  705.         {
  706.             /*第一次获取按键,初始化蛇的运动状态*/
  707.             if (Snake[i][j].goods_class == 3 && Snake[i][j].direction == 0)
  708.             {
  709.                 Snake[i][j].direction = ch;
  710.                 Snake[i][j + 1].direction = 4;
  711.                 Snake[i][j + 2].direction = 4;
  712.             }
  713.             else if (Snake[i][j].goods_class == 3)  //重置蛇头的方向
  714.                 Snake[i][j].direction = ch;

  715.             /*进行移动前,先判断蛇头是否碰到死亡因子*/
  716.             if (Snake[i][j].goods_class == 3)
  717.             {
  718.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class != 0 && Snake[i - 1][j].goods_class != 2)
  719.                     die++;

  720.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class != 0 && Snake[i + 1][j].goods_class != 2)
  721.                     die++;

  722.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class != 0 && Snake[i][j - 1].goods_class != 2)
  723.                     die++;

  724.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class != 0 && Snake[i][j + 1].goods_class != 2)
  725.                     die++;
  726.             }

  727.             /*进行移动前,先判断蛇头是否碰到食物*/
  728.             if (Snake[i][j].goods_class == 3)
  729.             {
  730.                 if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class == 2)
  731.                     eat_food++;

  732.                 else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class == 2)
  733.                     eat_food++;

  734.                 else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class == 2)
  735.                     eat_food++;

  736.                 else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class == 2)
  737.                     eat_food++;
  738.             }

  739.             if (die == 1)
  740.                 break;

  741.             /*蛇尾的移动*/
  742.             if (Snake[i][j].goods_class == 3 + snake_long - 1)
  743.             {
  744.                 if (eat_food == 1)  //吃到食物就更新蛇尾
  745.                 {
  746.                     if (Snake[i][j].direction == 8)
  747.                     {
  748.                         direction = Snake[i - 1][j].direction;
  749.                         Snake[i - 1][j] = Snake[i][j];
  750.                         Snake[i - 1][j].direction = direction;
  751.                         Snake[i][j].goods_class = 3 + snake_long;
  752.                     }
  753.                     else if (Snake[i][j].direction == 2)
  754.                     {
  755.                         direction = Snake[i + 1][j].direction;
  756.                         Snake[i + 1][j] = Snake[i][j];
  757.                         Snake[i + 1][j].direction = direction;
  758.                         Snake[i][j].goods_class = 3 + snake_long;
  759.                     }
  760.                     else if (Snake[i][j].direction == 4)
  761.                     {
  762.                         direction = Snake[i][j - 1].direction;
  763.                         Snake[i][j - 1] = Snake[i][j];
  764.                         Snake[i][j - 1].direction = direction;
  765.                         Snake[i][j].goods_class = 3 + snake_long;
  766.                     }
  767.                     else if (Snake[i][j].direction == 6)
  768.                     {
  769.                         direction = Snake[i][j + 1].direction;
  770.                         Snake[i][j + 1] = Snake[i][j];
  771.                         Snake[i][j + 1].direction = direction;
  772.                         Snake[i][j].goods_class = 3 + snake_long;
  773.                     }
  774.                     snake_move++;
  775.                     eat_food++;
  776.                     break;
  777.                 }
  778.                 else{
  779.                     if (Snake[i][j].direction == 8)
  780.                     {
  781.                         Snake[i][j].direction = Snake[i - 1][j].direction;
  782.                         Snake[i - 1][j] = Snake[i][j];
  783.                         Snake[i][j].goods_class = 0;
  784.                     }
  785.                     else if (Snake[i][j].direction == 2)
  786.                     {
  787.                         Snake[i][j].direction = Snake[i + 1][j].direction;
  788.                         Snake[i + 1][j] = Snake[i][j];
  789.                         Snake[i][j].goods_class = 0;
  790.                     }
  791.                     else if (Snake[i][j].direction == 4)
  792.                     {
  793.                         Snake[i][j].direction = Snake[i][j - 1].direction;
  794.                         Snake[i][j - 1] = Snake[i][j];
  795.                         Snake[i][j].goods_class = 0;
  796.                     }
  797.                     else if (Snake[i][j].direction == 6)
  798.                     {
  799.                         Snake[i][j].direction = Snake[i][j + 1].direction;
  800.                         Snake[i][j + 1] = Snake[i][j];
  801.                         Snake[i][j].goods_class = 0;
  802.                     }
  803.                     snake_move++;
  804.                     break;
  805.                 }
  806.             }

  807.             /*蛇头的移动*/
  808.             if (Snake[i][j].goods_class == 3)
  809.             {
  810.                 if (Snake[i][j].direction == 8)
  811.                     Snake[i - 1][j] = Snake[i][j];

  812.                 else if (Snake[i][j].direction == 2)
  813.                     Snake[i + 1][j] = Snake[i][j];

  814.                 else if (Snake[i][j].direction == 4)
  815.                     Snake[i][j - 1] = Snake[i][j];

  816.                 else if (Snake[i][j].direction == 6)
  817.                     Snake[i][j + 1] = Snake[i][j];
  818.                 snake_move++;
  819.                 break;
  820.             }

  821.             /*蛇身的移动*/
  822.             if (Snake[i][j].direction == 8)
  823.             {
  824.                 direction = Snake[i - 1][j].direction;
  825.                 Snake[i - 1][j] = Snake[i][j];
  826.                 Snake[i - 1][j].direction = direction;
  827.             }
  828.             else if (Snake[i][j].direction == 2)
  829.             {
  830.                 direction = Snake[i + 1][j].direction;
  831.                 Snake[i + 1][j] = Snake[i][j];
  832.                 Snake[i + 1][j].direction = direction;
  833.             }
  834.             else if (Snake[i][j].direction == 4)
  835.             {
  836.                 direction = Snake[i][j - 1].direction;
  837.                 Snake[i][j - 1] = Snake[i][j];
  838.                 Snake[i][j - 1].direction = direction;
  839.             }
  840.             else if (Snake[i][j].direction == 6)
  841.             {
  842.                 direction = Snake[i][j + 1].direction;
  843.                 Snake[i][j + 1] = Snake[i][j];
  844.                 Snake[i][j + 1].direction = direction;
  845.             }
  846.             snake_move++;
  847.             break;
  848.         }
  849.     }
  850. }

  851. void HideCursor()//隐藏光标函数
  852. {
  853.     CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
  854.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  855. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 19:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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