• 贪吃蛇(C)


      

    缺点:

      代码复用性不太高,重复或相似代码太多

     

    学完数据结构

      考虑使用 链表做,更加简单 

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <time.h>
      4 #include <conio.h>
      5 #include <windows.h>
      6 
      7 #define snakeBirthLength 3 //最开始时刻蛇的长度
      8 #define MatrixSize        12 //游戏背景大小
      9 
     10 #define PAINTL        printf("|");printf("    ");
     11 #define PAINTR        (printf("    |"));
     12 #define PAINTTAIL    (paintTail())
     13 
     14 #define LEVEL_1 50
     15 #define LEVEL_2 200
     16 #define LEVEL_3 500
     17 
     18 
     19 int matrix[MatrixSize][MatrixSize];//-1 0(head) !0(body) -5(food)
     20 int level;
     21                                  
     22 
     23 void gameRun();
     24 void reGame(int *go);
     25 void gameOver();
     26 
     27 void gameInit();//随机设置贪吃蛇的初始位置
     28 void showMode(int num);
     29 void paintHead(int num);
     30 void paintTail();
     31 void giveBirth(int *drt);
     32 void makeFood();
     33 int getRandNum(int max);//[0,max]随机数
     34 void reaction(int *drt);//规定时间内是否响应
     35 int moveOn(int drt, int *len);
     36 
     37 int main()
     38 {
     39     system("color 0B");
     40     gameRun();
     41     return 0;
     42 }
     43 
     44 void gameInit()
     45 {
     46     int i, j;
     47     for (i = 0; i<MatrixSize; i++)
     48         for (j = 0; j<MatrixSize; j++)
     49             matrix[i][j] = -1;//
     50 }
     51 void gameRun()
     52 {
     53     int drt;//direction
     54     int go = 1;
     55 
     56     while (go)
     57     {
     58         gameInit();
     59         giveBirth(&drt);
     60         makeFood();
     61         int snakeLen = snakeBirthLength, t;
     62         t = snakeLen;
     63 
     64         while (1) {
     65             if (t != snakeLen) {
     66                 makeFood();
     67                 t = snakeLen;
     68             }
     69             showMode(t - 3);
     70             reaction(&drt);//得到前进方向
     71             if (!moveOn(drt, &snakeLen))
     72                 break;
     73             system("cls");
     74         }
     75         reGame(&go);
     76     }
     77     gameOver();
     78 }
     79 void reGame(int *go)
     80 {
     81     printf("
    
    
    ----------------再开一局否?y/n _");
     82     char ch;
     83     while (1)
     84     {
     85         Sleep(10);
     86         if (!_kbhit())
     87             continue;
     88         else {
     89             ch = _getch();
     90             if (ch == 'y' || ch == 'Y')
     91             {
     92                 Sleep(800);
     93                 system("cls");
     94                 return;
     95             }
     96             else if (ch == 'n' || ch == 'N')
     97             {
     98                 Sleep(500);
     99                 *go = 0;
    100                 return;
    101             }
    102             else
    103                 continue;
    104         }
    105     }
    106 }
    107 void gameOver()
    108 {
    109     system("cls");
    110     printf("
    
    
    	----------------输怕了吧 哈哈!------------------
    ");
    111     Sleep(3000);
    112 }
    113 void showMode(int num)
    114 {
    115     int i, j;
    116     paintHead(num);
    117     for (i = 0; i<MatrixSize; i++) {
    118         printf("");
    119         PAINTL;
    120         for (j = 0; j < MatrixSize; j++)
    121             if (matrix[i][j] == -1)
    122                 printf(".   ");
    123             else if (matrix[i][j] == 0)
    124                 printf("4   ");
    125             else if (matrix[i][j] == -5)
    126                 printf("#   ");
    127             else if (matrix[i][j] == -2)
    128                 printf("!   ");
    129             else
    130                 printf("0   ");
    131         PAINTR;
    132         printf("
    
    ");
    133     }
    134     PAINTTAIL;
    135 }
    136 void paintHead(int num)
    137 {
    138     printf("
    ");
    139     int i;
    140     //第一行
    141     printf("+");
    142     printf("    ");
    143     for (i = 0; i<MatrixSize; i++)
    144         printf("=   ");
    145     printf("    ");
    146     printf("+
    ");
    147     printf("
    ");
    148     int len = 10 + MatrixSize * 4;
    149     for (int i = 0; i < len; i++)
    150         printf(" ");
    151     printf("	当前分数:%d
    ", num*10);
    152 }
    153 void paintTail()
    154 {
    155     int i;
    156     //第一行
    157     printf("
    
    ");
    158     printf("+");
    159     printf("    ");
    160     for (i = 0; i<MatrixSize; i++)
    161         printf("=   ");
    162     printf("    ");
    163     printf("+
    ");
    164 }
    165 void giveBirth(int *drt)
    166 {
    167     printf("(温馨提示:请将输入法改为英文输入法)
    ");
    168     printf("
    
    
    	----------------游戏即将开始,请选择难易程度
    ");
    169     printf("
    
    
    	----------------     高难度  输入1
    ");
    170     printf("
    
    
    	----------------    一般难度 输入2
    ");
    171     printf("
    
    
    	----------------     低难度  输入3
    ");
    172     printf("
    
    
    	----------------请输入: _");
    173     char ch;
    174     ch = getchar();
    175     if (ch == '1')level = LEVEL_1;
    176     if (ch == '3') level = LEVEL_3;
    177     else level = LEVEL_2;
    178     while (getchar() != '
    ');
    179 
    180     system("cls");
    181     int i, j, k;
    182     int direction = getRandNum(3);//0上1下2左3右
    183     while (1)
    184     {
    185         i = getRandNum(MatrixSize - 1);//row
    186         j = getRandNum(MatrixSize - 1);//col
    187         if (direction == 0 && i - snakeBirthLength + 1 >= 0)
    188         {
    189             for (k = 0; k<snakeBirthLength; k++)
    190                 if (k)
    191                     matrix[i - k][j] = k;
    192                 else
    193                     matrix[i][j] = 0;//
    194             *drt = 1;
    195             break;
    196         }
    197         if (direction == 1 && i + snakeBirthLength - MatrixSize <= 0)
    198         {
    199             for (k = 0; k<snakeBirthLength; k++)
    200                 if (k)
    201                     matrix[i + k][j] = k;
    202                 else
    203                     matrix[i][j] = 0;
    204             *drt = 0;
    205             break;
    206         }
    207         if (direction == 2 && j - snakeBirthLength + 1 >= 0)
    208         {
    209             for (k = 0; k<snakeBirthLength; k++)
    210                 if (k)
    211                     matrix[i][j - k] = k;
    212                 else
    213                     matrix[i][j] = 0;
    214             *drt = 3;
    215             break;
    216         }
    217         if (direction == 3 && j + snakeBirthLength - MatrixSize <= 0)
    218         {
    219             for (k = 0; k<snakeBirthLength; k++)
    220                 if (k)
    221                     matrix[i][j + k] = k;
    222                 else
    223                     matrix[i][j] = 0;
    224             *drt = 2;
    225             break;
    226         }
    227     }
    228 }
    229 
    230 void makeFood()
    231 {
    232     int i, j;
    233     while (1)
    234     {
    235         i = getRandNum(MatrixSize - 1);
    236         j = getRandNum(MatrixSize - 1);
    237         if (matrix[i][j] == -1)
    238         {
    239             matrix[i][j] = -5;
    240             break;
    241         }
    242     }
    243 }
    244 
    245 int getRandNum(int max)
    246 {
    247     srand((unsigned)time(NULL));
    248     return rand() % (max + 1);//[0,max]
    249 }
    250 
    251 void reaction(int * drt)
    252 {
    253     char ch;
    254     Sleep(level);
    255     if (!_kbhit())
    256         return;
    257     else
    258     {
    259         ch = _getch();
    260         if (ch == 'w') *drt = 0;
    261         if (ch == 's') *drt = 1;
    262         if (ch == 'a') *drt = 2;
    263         if (ch == 'd') *drt = 3;
    264         return;
    265     }
    266 }
    267 
    268 int moveOn(int drt, int * len)
    269 {
    270     int i, j, m, n;
    271     const int l = *len;
    272     int hr, hc, tr, tc;
    273     for (i = 0; i<MatrixSize; i++)
    274         for (j = 0; j<MatrixSize; j++)
    275             if (matrix[i][j] == 0)
    276             {
    277                 hr = i, hc = j;
    278                 break;
    279             }
    280     for (i = 0; i < MatrixSize; i++)
    281         for (j = 0; j < MatrixSize; j++)
    282             if (matrix[i][j] == l-1)
    283             {
    284                 tr = i, tc = j;
    285                 break;
    286             }
    287 
    288     i = hr, j = hc;
    289     //撞墙事故
    290     if (drt == 0 && i == 0) {
    291         system("cls");
    292         showMode(l - 3);
    293         printf("
    
    
    	----------------头撞墙是也!------------------
    ");
    294         return 0;
    295     }
    296     if (drt == 1 && i == MatrixSize - 1) {
    297         system("cls");
    298         showMode(l - 3);
    299         printf("
    
    
    	----------------头撞墙是也!------------------
    ");
    300         return 0;
    301     }
    302     if (drt == 2 && j == 0) {
    303         system("cls");
    304         showMode(l - 3);
    305         printf("
    
    
    	----------------头撞墙是也!------------------
    ");
    306         return 0;
    307     }
    308     if (drt == 3 && j == MatrixSize - 1) {
    309         system("cls");
    310         showMode(l - 3);
    311         printf("
    
    
    	----------------头撞墙是也!------------------
    ");
    312         return 0;
    313     }
    314     //咬身事故
    315     if (drt == 0 && matrix[i - 1][j]>0) {
    316         matrix[tr][tc] = -1;//头与身体重合
    317         matrix[i - 1][j] = -2;
    318         matrix[i][j] = 1;
    319         system("cls");
    320         showMode(l - 3);
    321         printf("
    
    
    	----------------咬身事故也!------------------
    ");
    322         return 0;
    323     }
    324     if (drt == 1 && matrix[i + 1][j]>0) {
    325         matrix[tr][tc] = -1;
    326         matrix[i][j] = 1;
    327         matrix[i + 1][j] = -2;
    328         system("cls");
    329         showMode(l - 3);
    330         printf("
    
    
    	----------------咬身事故也!------------------
    ");
    331         return 0;
    332     }
    333     if (drt == 2 && matrix[i][j - 1]>0) {
    334         matrix[tr][tc] = -1;
    335         matrix[i][j] = 1;
    336         matrix[i][j - 1] = -2;
    337         system("cls");
    338         showMode(l - 3);
    339         printf("
    
    
    	----------------咬身事故也!------------------
    ");
    340         return 0;
    341     }
    342     if (drt == 3 && matrix[i][j + 1]>0) {
    343         matrix[tr][tc] = -1;
    344         matrix[i][j] = 1;
    345         matrix[i][j + 1] = -2;
    346         system("cls");
    347         showMode(l - 3);
    348         printf("
    
    
    	----------------咬身事故也!------------------
    ");
    349         return 0;
    350     }
    351     //食食
    352     if (drt == 0 && matrix[i - 1][j] == -5)
    353     {
    354         *len = l + 1;
    355         matrix[i - 1][j] = -3;//头部预留
    356         for (m = 0; m < MatrixSize; m++)
    357             for (n = 0; n < MatrixSize; n++)
    358                 if (matrix[m][n] >= 0)
    359                     matrix[m][n]++;//新身++
    360         for (m = 0; m < MatrixSize; m++)
    361             for (n = 0; n < MatrixSize; n++)
    362                 if (matrix[m][n] == -3) {
    363                     matrix[m][n] = 0;//找头
    364                     break;
    365                 }
    366         return 1;
    367     }
    368     if (drt == 1 && matrix[i + 1][j] == -5)
    369     {
    370         *len = l + 1;
    371         matrix[i + 1][j] = -3;//头部预留
    372         for (m = 0; m < MatrixSize; m++)
    373             for (n = 0; n < MatrixSize; n++)
    374                 if (matrix[m][n] >= 0)
    375                     matrix[m][n]++;//新身++
    376         for (m = 0; m < MatrixSize; m++)
    377             for (n = 0; n < MatrixSize; n++)
    378                 if (matrix[m][n] == -3) {
    379                     matrix[m][n] = 0;//找头
    380                     break;
    381                 }
    382         return 1;
    383     }
    384     if (drt == 2 && matrix[i][j - 1] == -5)
    385     {
    386         *len = l + 1;
    387         matrix[i][j - 1] = -3;//头部预留
    388         for (m = 0; m < MatrixSize; m++)
    389             for (n = 0; n < MatrixSize; n++)
    390                 if (matrix[m][n] >= 0)
    391                     matrix[m][n]++;//新身++
    392         for (m = 0; m < MatrixSize; m++)
    393             for (n = 0; n < MatrixSize; n++)
    394                 if (matrix[m][n] == -3) {
    395                     matrix[m][n] = 0;//找头
    396                     break;
    397                 }
    398         return 1;
    399     }
    400     if (drt == 3 && matrix[i][j + 1] == -5)
    401     {
    402         *len = l + 1;
    403         matrix[i][j + 1] = -3;//头部预留
    404         for (m = 0; m < MatrixSize; m++)
    405             for (n = 0; n < MatrixSize; n++)
    406                 if (matrix[m][n] >= 0)
    407                     matrix[m][n]++;//新身++
    408         for (m = 0; m < MatrixSize; m++)
    409             for (n = 0; n < MatrixSize; n++)
    410                 if (matrix[m][n] == -3) {
    411                     matrix[m][n] = 0;//找头
    412                     break;
    413                 }
    414         return 1;
    415     }
    416     //正常走
    417     if (drt == 0 && matrix[i - 1][j] == -1)
    418     {
    419         matrix[i - 1][j] = -2;
    420         matrix[tr][tc] = -1;
    421         for (m = 0; m < MatrixSize; m++)
    422             for (n = 0; n < MatrixSize; n++)
    423                 if (matrix[m][n] >= 0)
    424                     matrix[m][n]++;
    425         for (m = 0; m < MatrixSize; m++)
    426             for (n = 0; n < MatrixSize; n++)
    427                 if (matrix[m][n] == -2)
    428                 {
    429                     matrix[m][n] = 0;
    430                     break;
    431                 }
    432         return 1;
    433     }
    434     if (drt == 1 && matrix[i + 1][j] == -1)
    435     {
    436         matrix[i + 1][j] = -2;
    437         matrix[tr][tc] = -1;
    438         for (m = 0; m < MatrixSize; m++)
    439             for (n = 0; n < MatrixSize; n++)
    440                 if (matrix[m][n] >= 0)
    441                     matrix[m][n]++;
    442         for (m = 0; m < MatrixSize; m++)
    443             for (n = 0; n < MatrixSize; n++)
    444                 if (matrix[m][n] == -2)
    445                 {
    446                     matrix[m][n] = 0;
    447                     break;
    448                 }
    449         return 1;
    450     }
    451     if (drt == 2 && matrix[i][j - 1] == -1)
    452     {
    453         matrix[i][j - 1] = -2;
    454         matrix[tr][tc] = -1;
    455         for (m = 0; m < MatrixSize; m++)
    456             for (n = 0; n < MatrixSize; n++)
    457                 if (matrix[m][n] >= 0)
    458                     matrix[m][n]++;
    459         for (m = 0; m < MatrixSize; m++)
    460             for (n = 0; n < MatrixSize; n++)
    461                 if (matrix[m][n] == -2)
    462                 {
    463                     matrix[m][n] = 0;
    464                     break;
    465                 }
    466         return 1;
    467     }
    468     if (drt == 3 && matrix[i][j + 1] == -1)
    469     {
    470         matrix[i][j + 1] = -2;
    471         matrix[tr][tc] = -1;
    472         for (m = 0; m < MatrixSize; m++)
    473             for (n = 0; n < MatrixSize; n++)
    474                 if (matrix[m][n] >= 0)
    475                     matrix[m][n]++;
    476         for (m = 0; m < MatrixSize; m++)
    477             for (n = 0; n < MatrixSize; n++)
    478                 if (matrix[m][n] == -2)
    479                 {
    480                     matrix[m][n] = 0;
    481                     break;
    482                 }
    483         return 1;
    484     }
    485     return 1;
    486 }
    View Code
  • 相关阅读:
    Cyclic Nacklace HDU
    Oulipo HDU
    Period HDU
    Blue Jeans POJ
    剪花布条 HDU
    最长公共前缀 CSU
    Clock Pictures CSU
    Number Sequence HDU
    Arrange the Bulls POJ
    Traveling by Stagecoach POJ
  • 原文地址:https://www.cnblogs.com/guoyujiang/p/12111350.html
Copyright © 2020-2023  润新知