老鼠迷宫之《爱我别走》【内附代码和游戏下载地址】

来源:互联网 发布:阿里云建立二级域名 编辑:程序博客网 时间:2024/06/11 01:07

自己制作的迷宫地图,制定了两条路径逃出迷宫,有点意思,就是好玩。

先附上游戏.out文件和代码main.c的网盘地址:http://pan.baidu.com/s/1dD2Mn1f

思路分析:

/1/ 画(更新)迷宫地图;

/2/ 判断老鼠是否成功逃离迷宫:逃离成功,game over,否则,继续游戏;

/3/ 给出上下左右键的提示,让用户输入;

/4/ 如果输入错误, 重新输入,直到输入正确后,老鼠开始移动;

/5/ 让老鼠按照用户指令移动,老鼠移动过后要更新地图,即重复第一步;


本人习惯将重复利用的代码块写成函数,所以对于上述分析,重复进行的代码部分应该有两处:

一是更新地图部分;

二是用户输入部分。

所以对这两部分做了封装,写成函数,便于重复调用。

              下面分享了本人写的老鼠迷宫之《爱我别走》Game,欢迎讨论。

============================华丽分割线=======================

#include

#include

 

#define ROW 10

#define COL 15

 

void printMap(char map[ROW][COL], int rows, int cols);

char inputCharactor(char *range, int length);

 

void printMap(char map[ROW][COL], int rows, int cols) {

  for (int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {

            printf("%c", map[i][j]);

        }

        printf("\n");

    }

}

 

char inputCharactor(char *range, int length) {

    char ch;

    int index = -1;

   

    while(1) {

       

        printf(">>>>> ");

        ch = getchar();

       

        if(ch != '\n') {

            while (getchar() != '\n');

        }

 

        for (int i = 0; i < length; i++) {

            if(range[i] == ch) {

                index = i;

                break;

            }

        }

            if (index != -1) break;

    }

    return ch;

}

 

 

int main(int argc, const char * argv[]) {

    int x = 1,y = 1,temp;

    char input;

    char range[] = {

        'a','d','s','w',

        'W','S','A','D',

        'I','K','J','L',

        'j','k','l','i'

    };

    char map[ROW][COL]={

        {'#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#'},

        {'#','O',' ',' ','#',' ',' ',' ','#','#','#','#','#',' ','#'},

        {'#','#','#',' ','#','#','#',' ',' ','#',' ',' ',' ',' ','#'},

        {'#',' ',' ',' ','#',' ',' ',' ','#','#','#',' ','#','#','#'},

        {'#',' ','#','#','#',' ','#','#',' ',' ',' ',' ','#',' ','#'},

        {'#',' ',' ',' ','#',' ',' ','#',' ','#','#',' ','#',' ',' '},

        {'#','#','#',' ','#','#',' ',' ',' ','#','#',' ','#',' ','#'},

        {'#',' ',' ',' ',' ','#','#','#',' ','#',' ',' ',' ',' ','#'},

        {'#',' ','#','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},

        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},

       

    };

   

    while (1) {

 

        printMap(map,ROW,COL);

       

        if (x == 0 || y == 0 || x == ROW-1 || y == COL-1) {

            printf("爱我,别走!\n");

        }

       

        printf("请输入 wi() sk() aj() dl()\n");

        input = inputCharactor(range,16);

   

        switch (input) {

            case 'w':

            case 'W':

            case 'I':

            case 'i':

                if (map[x-1][y] == ' ') {

                    temp = map[x][y];

                    map[x][y] = map[x-1][y];

                    map[x-1][y] = temp;

                    x--;

                } break;

            case 'a':

            case 'A':

            case 'J':

            case 'j':

                if (map[x][y-1] == ' ') {

                    temp = map[x][y];

                    map[x][y] = map[x][y-1];

                    map[x][y-1] = temp;

                    y--;

                } break;

            case 's':

            case 'S':

            case 'k':

            case 'K':

                if (map[x+1][y] == ' ') {

                    temp = map[x][y];

                    map[x][y] = map[x+1][y];

                    map[x+1][y] = temp;

                    x++;

                } break;

            case 'd':

            case 'D':

            case 'l':

            case 'L':

            case 26:

                if (map[x][y+1] == ' ') {

                    temp = map[x][y];

                    map[x][y] = map[x][y+1];

                    map[x][y+1] = temp;

                    y++;

                } break;

               

        }

            system("clear");

    }

    return 0;

}


0 0
原创粉丝点击