Untitled

 avatar
unknown
c_cpp
4 years ago
2.9 kB
22
Indexable
#include "ConsoleApplication1.h"

char map[100][100] = { 0 };
// 全局初始化地图



void printmap(char map[100][100], int high)
{
    for (int i = 0; i < high; i++) {
        puts(map[i]);
    }
}

extern void moveChest(int tmpx, int tmpx2, int tmpy, int tmpy2);

extern void movePlayer(int* x, int tmpx, int* y, int tmpy);
extern void class1(char map[100][100], int* h, int* x, int* y);

void move(char map[100][100], int* x, int* y)
{
    char ch;
    ch = getch();
    // 读取操作
    int tmpx, tmpy;
    tmpx = *x;
    tmpy = *y;
    switch (ch) {
    case 'w':
        tmpx -= 1;
        break;
    case 'a':
        tmpy -= 1;
        break;
    case 's':
        tmpx += 1;
        break;
    case 'd':
        tmpy += 1;
        break;
    default:
        break;
    }
    // 知道了下一步预计会走到的位置的坐标

    int tmpx2, tmpy2;
    tmpx2 = tmpx + tmpx - *x;
    tmpy2 = tmpy + tmpy - *y;
    //定义下一步的后面是什么

    // 这个地方是墙吗?
    if (map[tmpx][tmpy] != '#') {
        //这个地方是空气吗?
        if (map[tmpx][tmpy] != ' ') {
            // 这个地方是箱子吗?
            if (map[tmpx][tmpy] == 'O' || map[tmpx][tmpy] == '@') {
                // 箱子后面有空地方吗
                if (map[tmpx2][tmpy2] == ' '
                    || map[tmpx2][tmpy2] == '*') {
                    //既然有!那么就人和箱子一起移动!
                    
                    moveChest(tmpx,tmpx2,tmpy, tmpy2);

                    movePlayer(x, tmpx, y, tmpy);
                }

            }
        } else {
        //既然是空气,那么就走!
            movePlayer(x,tmpx,y, tmpy);
        }
    }
}

void movePlayer(int *x, int tmpx, int *y, int tmpy) {
    if (map[*x][*y] == 'S') {
        map[*x][*y] = ' ';
    } else {
        map[*x][*y] = '*';    
    }

    if (map[tmpx][tmpy] == ' ') {
        map[tmpx][tmpy] = 'S';
    } else {
        map[tmpx][tmpy] = 'A';
    }

}

void moveChest(int tmpx ,int tmpx2, int tmpy ,int tmpy2) {
    if (map[tmpx][tmpy] == 'O') {
        map[tmpx][tmpy] = ' ';
    } else {
        map[tmpx][tmpy] = '*';
    }

    if (map[tmpx2][tmpy2] == ' ') {
        map[tmpx2][tmpy2] = 'O';
    } else {
        map[tmpx2][tmpy2] = '@';
    }


}


int main()
{
    

    int high = 1; // 地图高度

    int x = 1, y = 1; // 人的坐标

    //载入地图 , 包括地图数组,高度,人物坐标
    class1(map, &high, &x, &y);

    // 首次打印地图
    printmap(map, high);

    //操作与反馈
    while (1) {
        move(map, &x, &y);
        system("cls");
        printf("人的坐标 : %d %d \n\n", x, y);
        printmap(map, high);
    }

    printf("人物坐标为 %d %d", x, y);

    system("pause");
    return 0;
}
Editor is loading...