不多说,直接上源码:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <Windows.h>
#include <bits/stdc++.h>
using namespace std;int m = 0;//关卡/*推箱子游戏*/#define HANG 20 //地图的行数
#define LIE 20//列数
int x, y;class MyClass
{
public:char mapN[20][20][20] = {/*一个三维数组*/{"##### ","#r # ","# OO# ###","# O # #*#","### ###*#"," ## *#"," # #"," # #","#########"," #C++####"," #####"},{" #### #####","## # # #","# ####OO #","# rO****O #","## # ##"," ########## ",},{" ###### "," ### # ","##* O## ##","#**O O r#","#** O O ##","###### # "," #### ",},{"#######","# r #","# OOO #","##***##","# *@* #","# O O #","# # #","#######",},{" ########"," # ###"," # O #","### O ## #","#*** O #","#***O#O ##","#### # O #"," # r #"," #######"},{" #####"," # #"," #O #"," ### O##"," # O O #","### # ## # ######","# # ## ##### **#","# O O **#","##### ### #r## **#"," # #########"," #######",},{"############","#** # ###","#** # O O #","#** #O#### #","#** r ## #","#** # # O ##","###### ##O O #"," # O O O O #"," # # #"," ############",},{" ########"," # r#"," # O#O ##"," # O O#"," ##O O #","######### O # ###","#**** ## O O #","##*** O O #","#**** ##########","########",}};char map[20][20];/*用于*/void sm() {cout << "###########################" << endl;cout << "welcome to 推箱子 #" << endl;cout << "WASD-----------方向 #" << endl;cout << "♀--------------------人 #" << endl;cout << "■------------------箱子 #" << endl;cout << "☆------------------目标点 #" << endl;cout << " 作者:可爱的重云 @B站 @CSDN BLOG #" << endl;cout << "本人没写多少注释,让各位大佬见笑了。#" << endl;cout << "有一个_getch()的地方如果不是MSVC请自己自己删掉下划线 #" << endl;cout << "bits/stdc++.h从MINGW复制bits文件夹就可以#" << endl;cout << " 盗程序卒老亩 特别是某音的营销狗 # " << endl;cout << " 欢迎私信! #" << endl;cout << " 第" << m + 1 << "关 #" << endl;cout << " 共 8 关 #" << endl;cout << " 按 L 键 退 出 #" << endl;cout << "#######################################" << endl;}void xs() {int i, j;for (i = 0; i < HANG; i++) {for (j = 0; j < LIE; j++) {if (map[i][j] == '#') {printf("##"); }if (map[i][j] == 'r') {printf("♀"); }if (map[i][j] == 'O') {printf("■"); }if (map[i][j] == '*') {printf("☆"); }if (map[i][j] == ' ') {printf(" "); /*注意是两个空格,因为emoji占两个字节*/}if (map[i][j] == '@') {printf("★"); }if (map[i][j] == 'R') {printf("♀");}else if (map[i][j]!='#'&& map[i][j] != 'R'&& map[i][j] != '*'&& map[i][j] != '@'&& map[i][j] != 'r'&& map[i][j] != ' '&& map[i][j] != 'O'){cout << map[i][j];}}printf("\n");}}/*如出现UTF-8渲染问题,请删除渲染部分。但是效果会差一点*/void copy(int n) {for (int i = 0; i < 20; i++) {for (int j = 0; j < 20; j++) {map[i][j] = mapN[n][i][j];}}}/*直接用三维数组会很麻烦。所以用刚刚定义的map二维数组*/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) {system("title THE GAME FOR CPP");//title是给这个控制台取名int bs = 0;MyClass myclass;//在MYCLASS这个类中为Main取一个对象名int color;cout << "请选择颜色(1 2 3 4 5)0不选(CMD的color函数):" << endl;//system ("color ??") ;cin >> color;switch (color){case 0:break;case 1:system("color a"); break;case 2:system("color b"); break;case 3:system("color c"); break;case 4:system("color d"); break;case 5:system("color e"); break;default:system("color a"); break;}char fx;myclass.sm();myclass.copy(m);//必须拷贝到关卡上,否则十六进制欢迎您myclass.xs();while (1)//死循环不结束不退出{myclass.find();fx = _getch();//MSVC的写法,其他IDE自己删下划线switch (fx) {case 'L':case 'l':MessageBoxA(NULL, "再见", "SYSTEM", MB_OK);return 0;break;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;}bs++;system("CLS");//更新地图myclass.sm();//重新显示说明myclass.xs();//重新渲染if (myclass.over() == 1){m++;myclass.copy(m);MessageBox(NULL, L"你赢了", L"推箱子Game", MB_OK);//显示对话框if (m == 8)/*通关所有关卡*/{MessageBoxA(NULL, "Bye!", "SYSTEM", MB_OK);break;}else{MessageBoxA(NULL, "恭喜进入下一关!", "推箱子GAME", MB_OK);cout << "你走了" << bs << "步" << endl;system("pause");}}}MessageBoxA(NULL, "再见!", "GAME",MB_OK);return 0;
}
/*版权声明:
此程序来自可爱的重云,地图来自原版游戏(第一关除外)
未经允许禁止转载
版本:2.0
@CSDN @B站
*/
本人没写多少注释让大佬见笑了……
运行结果: