C++推箱子小游戏(绝对成功)建议用VS

源码:

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <Windows.h>
using namespace std;

#define HANG 9 
#define LIE 9 
int x, y;

class MyClass
{
public:

    char map[30][30] = {
        "######### ",
        "#r  # #*#",
        "# OO#  *#",
        "# O     $",
        "###  > *#",
        " ##     #",
        " #   #  #",
        " #   ||||",
        " ########"
    };
    void sm() {
        cout << "welcome to 推箱子" << endl;
        cout << "WASD-----------方向" << endl;
        cout << "r--------------------人" << endl;
        cout << "O------------------箱子" << endl;
        cout << "*------------------目标点" << endl;
        cout << "@@@@@ 作者:可爱的重云@B站 " << endl;
        cout << "本人没写注释,让各位大佬见笑了。" << endl;
        cout << "有一个_getch()的地方如果不是MSVC请自己自己删掉下划线" << endl;
    }
    void xs() {
        int i, j;
        for (i = 0; i < HANG; i++) {
            for (j = 0; j < LIE; j++) {
                cout << map[i][j];
            }
            cout << endl;
        }
    }
    void yd(int x1, int y1, int x2, int y2) {
        if (map[x][y] == 'r') {
            if (map[x1][y1] == 'O')
            {
                if (map[x2][y2] == ' ')
                {
                    map[x][y] = ' ';
                    map[x1][y1] = 'r';
                    map[x2][y2] = 'O';
                }
                if (map[x2][y2] == '*') 
                {
                    map[x][y] = ' ';
                    map[x1][y1] = 'r';
                    map[x2][y2] = '@';
                }
            }
            if (map[x1][y1] == '@') 
            {
                if (map[x2][y2] == ' ')
                {
                    map[x][y] = ' ';
                    map[x1][y1] = 'R';
                    map[x2][y2] = 'O';
                }
                if (map[x2][y2] == '*') {
                    map[x][y] = ' ';
                    map[x1][y1] = 'R';
                    map[x2][y2] = '@';
                }
            }
            if (map[x1][y1] == ' ') {
                map[x][y] = ' ';
                map[x1][y1] = 'r';
            }
            if (map[x1][y1] == '*') {
                map[x][y] = ' ';
                map[x1][y1] = 'R';
            }
        }
        if (map[x][y] == 'R') {
            if (map[x1][y1] == 'O') {
                if (map[x2][y2] == ' ') {
                    map[x][y] = '*';
                    map[x1][y1] = 'r';
                    map[x2][y2] = 'O';
                }
                if (map[x2][y2] == '*') {
                    map[x][y] = '*';
                    map[x1][y1] = 'r';
                    map[x2][y2] = '@';
                }
            }
            if (map[x1][y1] == '@') {
                if (map[x2][y2] == ' ') {
                    map[x][y] = '*';
                    map[x1][y1] = 'R';
                    map[x2][y2] = 'O';
                }
                if (map[x2][y2] == '*') {
                    map[x][y] = '*';
                    map[x1][y1] = 'R';
                    map[x2][y2] = '@';
                }
            }
            if (map[x1][y1] == ' ') {
                map[x][y] = '*';
                map[x1][y1] = 'r';
            }
            if (map[x1][y1] == '*') {
                map[x][y] = '*';
                map[x1][y1] = 'R';
            }
        }
    }
    void find() {
        for (x = 0; x < HANG; x++) {
            for (y = 0; y < LIE; y++) {
                if (map[x][y] == 'r' || map[x][y] == 'R') {
                    return;
                }
            }
        }
    }
    int over() {
        int i, j;
        for (i = 0; i < HANG; i++) {
            for (j = 0; j < LIE; j++) {
                if (map[i][j] == 'O') {
                    return 0;
                }
            }
        }
        return 1;
    }
};

int main(void) {
    MyClass myclass;
    system("color b");
    char fx;
    myclass.sm();
    myclass.xs();
    while (1) 
    {
        myclass.find();
        fx = _getch();
        switch (fx) {
        case 72:
        case 'w':
        case 'W':
            myclass.yd(x - 1, y, x - 2, y); break;
        case 80:
        case 's':
        case 'S':
            myclass.yd(x + 1, y, x + 2, y); break;
        case 75:
        case 'A':
        case 'a':
            myclass.yd(x, y - 1, x, y - 2); break;
        case 77:
        case 'D':
        case 'd':
            myclass.yd(x, y + 1, x, y + 2); break;
        }
        system("CLS");
        myclass.sm();
        myclass.xs();
        if (myclass.over() == 1) {
            MessageBox(NULL, L"你赢了", L"推箱子Game", MB_OK);
            break;
        }
    }
    return 0;
}//完成于2022/8/21

运行结果:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值