[轉] 貪吃蛇遊戲(有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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章