[转] 贪吃蛇游戏(有BUG)

 

贪吃蛇游戏,现在还有很多BUG。等待大家挑错。。。。。
难度:
1最难,500最简单。。吃够20个食物就可以过关了 呵呵。。。无聊时候玩玩吧

 

  1. #include <stdio.h>   
  2. #include <conio.h>   
  3. #include <stdlib.h>   
  4. #include <time.h>   
  5.   
  6. const int maxn = 100;  
  7. const int n = 20;  
  8.   
  9. struct node  
  10. {  
  11.     int x, y;  
  12. };  
  13.   
  14. int map[maxn][maxn]; // 0表示空格,1表示蛇身,2表示食物,3表示撞死的位置, 4表示蛇头.   
  15. node food;  
  16. node squence[maxn]; // 蛇的身子的坐标.   
  17. int len; // 蛇的长度.   
  18. bool eat; // 判断当前事物是否被吃了.   
  19. bool gameover; // 游戏是否失败.   
  20. int direction; // 判断当前蛇头是朝哪个方向.   
  21.   
  22. void Output(void);  
  23. void Move(int direction);  
  24. void Move_up(void);  
  25. void Move_down(void);  
  26. void Move_left(void);  
  27. void Move_right(void);  
  28.   
  29. int main()  
  30. {  
  31.     int i, j;  
  32.     double start;  
  33.     int gamespeed; // 游戏速度自己调整.   
  34.     int timeover;  
  35.     int game_times = 1; // 游戏的次数.   
  36.     char c = 'x';  
  37.     printf("请输入游戏的级别(1到500,1最难,500最简单):\n");  
  38.     scanf("%d", &gamespeed);  
  39.     gamespeed = gamespeed;  
  40.     // 对图的初始化.   
  41.     for (i = 0; i <= n + 1; i++)  
  42.     {  
  43.         for (j = 0; j <= n + 1; j++)  
  44.         {  
  45.             map[i][j] = 0;  
  46.         }  
  47.     }  
  48.     // 对蛇的初始化.   
  49.     for (i = 1; i <= n; i++)  
  50.     {  
  51.         squence[i].x = 0;  
  52.         squence[i].y = 0;  
  53.     }  
  54.     len = 1; // 蛇的长度为1.   
  55.     squence[len].x = 1;  
  56.     squence[len].y = 1; // 初始位置在点(1, 1).   
  57.     map[1][1] = 4;  
  58.     direction = 4; // 默认开始时蛇向右走.   
  59.     srand(time(0));  
  60.     while (game_times <= 20)  
  61.     {  
  62.         eat = 0; // 食物还没被吃.   
  63.         while (true)  
  64.         { // 随机生出食物的坐标,注意不能与蛇身重合,否则就重新随机产生.   
  65.             food.x = rand() % 20 + 1;  
  66.             food.y = rand() % 20 + 1;  
  67.             if (map[food.x][food.y] == 0)  
  68.             {  
  69.                 break;  
  70.             }  
  71.         }  
  72.         map[food.x][food.y] = 2; // 食物位置.   
  73.         system("cls");  
  74.         Output();  
  75.         // 以下这段半秒钟不按键还取原方向继续前行.   
  76.         while (!eat)  
  77.         {  
  78.             start = clock();  
  79.             timeover=1;  
  80.             while(!kbhit())  
  81.             { // 说明没有按键.   
  82.                 if (clock() - start <= gamespeed)  
  83.                 { // 如果时间超过游戏时间.   
  84.                     timeover = 1;  
  85.                 }  
  86.                 else  
  87.                 {  
  88.                     timeover = 0;  
  89.                     break;  
  90.                 }  
  91.             }  
  92.             if (timeover)  
  93.             { // 说明有按键.   
  94.                 // 按一次键,可以连取两个   
  95.                 c = getch();  
  96.                 c = getch();  
  97.                 // 以下几行告诉怎样判断用户按的哪个方向键   
  98.                 if(c==72) direction = 1; // printf("向上");   
  99.                 if(c==80) direction = 2; // printf("向下");   
  100.                 if(c==75) direction = 3; // printf("向左");   
  101.                 if(c==77) direction = 4; // printf("向右");   
  102.             }  
  103.             Move(direction);  
  104.             system("cls");  
  105.             if (gameover)  
  106.             {  
  107.                 Output();  
  108.                 printf("Game Over!!!\n");  
  109.                 return 0;  
  110.             }  
  111.             Output();  
  112.         }  
  113.         game_times++; // 又成功吃到一次食物.   
  114.     }  
  115.     printf("You win!!!\n");  
  116.     return 0;  
  117. }  
  118.   
  119. void Move(int direction)  
  120. {  
  121.     switch (direction)  
  122.     {  
  123.     case 1 : Move_up(); break;  
  124.     case 2 : Move_down(); break;  
  125.     case 3 : Move_left(); break;  
  126.     default : Move_right();  
  127.     }  
  128. }  
  129.   
  130. void Output(void)  
  131. {  
  132.     int i, j;  
  133.     for (j = 0; j <= n + 1; j++)  
  134.     {  
  135.         printf("#");  
  136.     }  
  137.     printf("\n");  
  138.     for (i = 1; i <= n; i++)  
  139.     {  
  140.         for (j = 0; j <= n + 1; j++)  
  141.         {  
  142.             if (j == 0 || j == n + 1)  
  143.             {  
  144.                 if (map[i][j] == 3)  
  145.                 {  
  146.                     printf("!");  
  147.                 }  
  148.                 else  
  149.                 {  
  150.                     printf("#");  
  151.                 }  
  152.             }  
  153.             else  
  154.             {  
  155.                 if (map[i][j] == 1)  
  156.                 {  
  157.                     printf("*");  
  158.                 }  
  159.                 else if (map[i][j] == 2)  
  160.                 {  
  161.                     printf("@");  
  162.                 }  
  163.                 else if (map[i][j] == 3)  
  164.                 {  
  165.                     printf("!");  
  166.                 }  
  167.                 else if (map[i][j] == 4)  
  168.                 {  
  169.                     switch (direction)  
  170.                     {  
  171.                     case 1 : printf("%c", 30); break;  
  172.                     case 2 : printf("%c", 31); break;  
  173.                     case 3 : printf("%c", 17); break;  
  174.                     default : printf("%c", 16);  
  175.                     }  
  176.                 }  
  177.                 else  
  178.                 {  
  179.                     printf(" ");  
  180.                 }  
  181.             }  
  182.         }  
  183.         printf("\n");  
  184.     }  
  185.     for (j = 0; j <= n + 1; j++)  
  186.     {  
  187.         printf("#");  
  188.     }  
  189.     printf("\n");  
  190. }  
  191.   
  192. void Move_up(void)  
  193. {  
  194.     int i;  
  195.     int x, y;  
  196.     if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x + 1)  
  197.     { // 不能移动,则按原来的移动.   
  198.         direction = 2; // 按原来的向下移动.   
  199.         Move(direction);  
  200.         return ;  
  201.     }  
  202.     // 开始移动.   
  203.     x = squence[len].x - 1;  
  204.     y = squence[len].y;  
  205.     if (x == 0 || map[x][y] == 1)  
  206.     { // 撞到边界或者自己撞到自己.   
  207.         map[x][y] = 3;  
  208.         gameover = 1;  
  209.     }  
  210.     if (map[x][y] == 2)  
  211.     { // 说明已经吃到事物.   
  212.         map[squence[len].x][squence[len].y] = 1;  
  213.         len++;  
  214.         squence[len].x = x;  
  215.         squence[len].y = y;  
  216.         map[x][y] = 4;  
  217.         eat = 1;  
  218.     }  
  219.     else  
  220.     {  
  221.         map[squence[1].x][squence[1].y] = 0;  
  222.         for (i = 1; i <= len - 1; i++)  
  223.         {  
  224.             squence[i].x = squence[i + 1].x;  
  225.             squence[i].y = squence[i + 1].y;  
  226.             map[squence[i + 1].x][squence[i + 1].y] = 1;  
  227.         }  
  228.         squence[len].x = squence[len].x - 1;  
  229.         if (gameover)  
  230.         {  
  231.             map[squence[len].x][squence[len].y] = 3;  
  232.         }  
  233.         else  
  234.         {  
  235.             map[squence[len].x][squence[len].y] = 4;  
  236.         }  
  237.     }  
  238. }  
  239.   
  240. void Move_down(void)  
  241. {  
  242.     int i;  
  243.     int x, y;  
  244.     if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x - 1)  
  245.     { // 不能移动,则按原来的移动.   
  246.         direction = 1; // 按原来的向上移动.   
  247.         Move(direction);  
  248.         return ;  
  249.     }  
  250.     // 开始移动.   
  251.     x = squence[len].x + 1;  
  252.     y = squence[len].y;  
  253.     if (x == n + 1 || map[x][y] == 1)  
  254.     { // 撞到边界或者自己撞到自己.   
  255.         map[x][y] = 3;  
  256.         gameover = 1;  
  257.     }  
  258.     if (map[x][y] == 2)  
  259.     { // 说明已经吃到事物.   
  260.         map[squence[len].x][squence[len].y] = 1;  
  261.         len++;  
  262.         squence[len].x = x;  
  263.         squence[len].y = y;  
  264.         map[x][y] = 4;  
  265.         eat = 1;  
  266.     }  
  267.     else  
  268.     {  
  269.         map[squence[1].x][squence[1].y] = 0;  
  270.         for (i = 1; i <= len - 1; i++)  
  271.         {  
  272.             squence[i].x = squence[i + 1].x;  
  273.             squence[i].y = squence[i + 1].y;  
  274.             map[squence[i + 1].x][squence[i + 1].y] = 1;  
  275.         }  
  276.         squence[len].x = squence[len].x + 1;  
  277.         if (gameover)  
  278.         {  
  279.             map[squence[len].x][squence[len].y] = 3;  
  280.         }  
  281.         else  
  282.         {  
  283.             map[squence[len].x][squence[len].y] = 4;  
  284.         }  
  285.     }  
  286. }  
  287.   
  288. void Move_left(void)  
  289. {  
  290.     int i;  
  291.     int x, y;  
  292.     if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y + 1)  
  293.     { // 不能移动,则按原来的移动.   
  294.         direction = 4; // 按原来的向右移动.   
  295.         Move(direction);  
  296.         return ;  
  297.     }  
  298.     // 开始移动.   
  299.     x = squence[len].x;  
  300.     y = squence[len].y - 1;  
  301.     if (y == 0 || map[x][y] == 1)  
  302.     { // 撞到边界或者自己撞到自己.   
  303.         map[x][y] = 3;  
  304.         gameover = 1;  
  305.     }  
  306.     if (map[x][y] == 2)  
  307.     { // 说明已经吃到事物.   
  308.         map[squence[len].x][squence[len].y] = 1;  
  309.         len++;  
  310.         squence[len].x = x;  
  311.         squence[len].y = y;  
  312.         map[x][y] = 4;  
  313.         eat = 1;  
  314.     }  
  315.     else  
  316.     {  
  317.         map[squence[1].x][squence[1].y] = 0;  
  318.         for (i = 1; i <= len - 1; i++)  
  319.         {  
  320.             squence[i].x = squence[i + 1].x;  
  321.             squence[i].y = squence[i + 1].y;  
  322.             map[squence[i + 1].x][squence[i + 1].y] = 1;  
  323.         }  
  324.         squence[len].y = squence[len].y - 1;  
  325.         if (gameover)  
  326.         {  
  327.             map[squence[len].x][squence[len].y] = 3;  
  328.         }  
  329.         else  
  330.         {  
  331.             map[squence[len].x][squence[len].y] = 4;  
  332.         }  
  333.     }  
  334. }  
  335.   
  336. void Move_right(void)  
  337. {  
  338.     int i;  
  339.     int x, y;  
  340.     if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y - 1)  
  341.     { // 不能移动,则按原来的移动.   
  342.         direction = 3; // 按原来的向左移动.   
  343.         Move(direction);  
  344.         return ;  
  345.     }  
  346.     // 开始移动.   
  347.     x = squence[len].x;  
  348.     y = squence[len].y + 1;  
  349.     if (y == n + 1 || map[x][y] == 1)  
  350.     { // 撞到边界或者自己撞到自己.   
  351.         map[x][y] = 3;  
  352.         gameover = 1;  
  353.     }  
  354.     if (map[x][y] == 2)  
  355.     { // 说明已经吃到事物.   
  356.         map[squence[len].x][squence[len].y] = 1;  
  357.         len++;  
  358.         squence[len].x = x;  
  359.         squence[len].y = y;  
  360.         map[x][y] = 4;  
  361.         eat = 1;  
  362.     }  
  363.     else  
  364.     {  
  365.         map[squence[1].x][squence[1].y] = 0;  
  366.         for (i = 1; i <= len - 1; i++)  
  367.         {  
  368.             squence[i].x = squence[i + 1].x;  
  369.             squence[i].y = squence[i + 1].y;  
  370.             map[squence[i + 1].x][squence[i + 1].y] = 1;  
  371.         }  
  372.         squence[len].y = squence[len].y + 1;  
  373.         if (gameover)  
  374.         {  
  375.             map[squence[len].x][squence[len].y] = 3;  
  376.         }  
  377.         else  
  378.         {  
  379.             map[squence[len].x][squence[len].y] = 4;  
  380.         }  
  381.     }  
  382. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值