#include <iostream>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
void color(short x) //自定义函根据参数改变颜色
{
if (x >= 0 && x <= 15)//参数在0-15的范围颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色
else//默认的颜色白色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void Gameplay(int roleX);
int Menu();
void Gameplay(int roleX)
{
int roleY = 4, roleZ = 3;
//用三维数组写一个地图
int maps[2][10][10] = { {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,4,0,0,0,0,0,0,1},
{1,0,1,0,3,0,0,3,0,1},
{1,0,1,2,1,4,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,4,1},
{1,0,0,0,3,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}},
{{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,4,1},
{1,0,1,1,0,4,0,0,0,1},
{1,0,1,2,0,0,3,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,3,0,0,4,0,3,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}} };
int box = 3, blank = 3;
while (roleX <2)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
switch (maps[roleX][j][k])
{
case 0: cout << " "; break;//空地
case 1:color(3); cout << "■"; break;//墙壁 为蓝色
case 2:color(6); cout << "★"; break;//人物 人物为黄色
case 3:color(12); cout << "●"; break;//箱子 为红色
case 4:color(12); cout << "○"; break;//目标点 为红色
case 5:color(9); cout << "●"; break;//箱子占据目标点 箱子变成蓝色
case 6:color(4); cout << "★"; break;//人物占据目标点 人物变成红色
}
}
cout << endl;
}
int Input = _getch();
system("CLS");
switch (Input)//按键控制
{
case 's':case 'S':
if (maps[roleX][roleY + 1][roleZ] == 0 || maps[roleX][roleY + 1][roleZ] == 4)//向空白处或目标点移动
{
maps[roleX][roleY][roleZ] -= 2;//原来位置变成空白
maps[roleX][roleY + 1][roleZ] += 2;//下一个位置变成人物
++roleY;
}
else if (maps[roleX][roleY + 1][roleZ] == 3 && maps[roleX][roleY + 2][roleZ] == 0 && maps[roleX][roleY + 2][roleZ] != 3)//移动箱子并且箱子下一个位置为空白点
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY + 1][roleZ] -= 1;//箱子位置变成人物
maps[roleX][roleY + 2][roleZ] += 3;//空白位置变成箱子
++roleY;
}
else if (maps[roleX][roleY + 1][roleZ] == 3 && maps[roleX][roleY + 2][roleZ] == 4)//将箱子移向目标点
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY + 1][roleZ] -= 1;//箱子位置变成人物
maps[roleX][roleY + 2][roleZ] += 1;//目标点位置个位置变成箱子
++roleY;
box--;//进入目标点箱子数量减一
}
else if (5 == maps[roleX][roleY + 1][roleZ] && 1 != maps[roleX][roleY + 2][roleZ])//将箱子移出目标点
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY + 1][roleZ] += 1;//箱子位置变成人物
maps[roleX][roleY + 2][roleZ] += 3;//下个位置变成箱子
++roleY;
box++;//箱子移出后数量加一
}
break;
case 'a':case 'A':
if (maps[roleX][roleY][roleZ - 1] == 0 || maps[roleX][roleY][roleZ - 1] == 4)//向空白处或目标点移动
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ - 1] += 2;
--roleZ;
}
else if (maps[roleX][roleY][roleZ - 1] == 3 && maps[roleX][roleY][roleZ - 2] == 0 && maps[roleX][roleY][roleZ - 2] != 3)//移动箱子并且箱子下一个位置为空白点
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ - 1] -= 1;
maps[roleX][roleY][roleZ - 2] += 3;
--roleZ;
}
else if (maps[roleX][roleY][roleZ - 1] == 3 && maps[roleX][roleY][roleZ - 2] == 4)//将箱子移向目标点
{
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ - 1] -= 1;
maps[roleX][roleY][roleZ - 2] += 1;
--roleZ;
box--;
}
}
else if (5 == maps[roleX][roleY][roleZ - 1] && 1 != maps[roleX][roleY][roleZ - 2])//将箱子移出目标点
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ - 1] += 1;
maps[roleX][roleY][roleZ - 2] += 3;
--roleZ;
box++;
}
break;
case 'w':case 'W':
if (maps[roleX][roleY - 1][roleZ] == 0 || maps[roleX][roleY - 1][roleZ] == 4)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY - 1][roleZ] += 2;
--roleY;
}
else if (maps[roleX][roleY - 1][roleZ] == 3 && maps[roleX][roleY - 2][roleZ] == 0 && maps[roleX][roleY - 2][roleZ] != 3)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY - 1][roleZ] -= 1;
maps[roleX][roleY - 2][roleZ] += 3;
--roleY;
}
else if (maps[roleX][roleY - 1][roleZ] == 3 && maps[roleX][roleY - 2][roleZ] == 4)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY - 1][roleZ] -= 1;
maps[roleX][roleY - 2][roleZ] += 1;
--roleY;
box--;
}
else if (5 == maps[roleX][roleY - 1][roleZ] && 1 != maps[roleX][roleY - 2][roleZ])
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY - 1][roleZ] += 1;
maps[roleX][roleY - 2][roleZ] += 3;
--roleY;
box++;
}
break;
case 'd':case 'D':
if (maps[roleX][roleY][roleZ + 1] == 0 || maps[roleX][roleY][roleZ + 1] == 4)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ + 1] += 2;
++roleZ;
}
else if (maps[roleX][roleY][roleZ + 1] == 3 && maps[roleX][roleY][roleZ + 2] == 0 && maps[roleX][roleY][roleZ + 2] != 3)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ + 1] -= 1;
maps[roleX][roleY][roleZ + 2] += 3;
++roleZ;
}
else if (maps[roleX][roleY][roleZ + 1] == 3 && maps[roleX][roleY][roleZ + 2] == 4)
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ + 1] -= 1;
maps[roleX][roleY][roleZ + 2] += 1;
++roleZ;
box--;
}
else if (5 == maps[roleX][roleY][roleZ + 1] && 1 != maps[roleX][roleY + 2][roleZ])
{
maps[roleX][roleY][roleZ] -= 2;
maps[roleX][roleY][roleZ + 1] += 1;
maps[roleX][roleY][roleZ + 2] += 3;
++roleZ;
box++;
}
break;
case 'r':case 'R'://重置游戏
{
if (roleX==0){roleX = 0; }
else { roleX = 1; }
system("CLS");
Gameplay(roleX);
}break;
case 't':case 'T'://返回主菜单
{
system("CLS");
Menu();
}break;
}
if (box==0)
{
//箱子全部进入目标点,切换到下一张地图
roleY = 4, roleZ = 3;
box = 3;//箱子重新计数
roleX++;
}
}
system("CLS");
color(10);
cout << "恭喜你获得游戏胜利!!" << endl;
cout << endl;
}
int Menu()
{
int S,roleX=0;
system("CLS");
color(9);
cout << "推箱子" << endl;
cout << "1.开始游戏" << endl;
cout << "2.游戏规则" << endl;
cout << "3.退出游戏" << endl;
cin >> S;
if (S == 1)
{
system("CLS");
Gameplay(roleX);
cout << "按'1'返回主菜单" << endl;
cout<<"按'2'退出游戏" << endl;
cin >> S;
if (S == 1)
{
Menu();
}
else if (S == 2)
{
exit(0);
}
}
else if (S == 2)
{
system("CLS");
cout << "推箱子规则:" << endl;
cout << "通过‘W’,'S','A','D'控制星星,将箱子推入目标点即算成功。" << endl;
cout << "成功后自动跳转地图" << endl;
cout << "按T可返回主界面,按R可重新进行游戏。" << endl;
cout << "按'1'返回主菜单" << endl;
cout << "按'2'退出游戏" << endl;
cin >> S;
if (S == 1)
{
Menu();
}
else if (S==2)
{
exit(0);
}
}
else if (S==3)
{
exit(0);
}
}
int main()
{
Menu();
return 0;
}
主菜单:
游戏界面:
游戏胜利:
游戏规则: