自制推箱子代码,有主菜单功能,撤回功能,地图切换功能,经测试无bug

#include <iostream>
#include<conio.h>
#include <windows.h>
#include <stdio.h>
#include< stdlib.h >
using namespace std;



//网上找的颜色输出函数
void cprint(const char* s, int color)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
	printf(s);
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}




//行列下标表示玩家坐标
	int PosX=1, PosY=3;
	//为了保存箱子的坐标,还要为箱子设置一个坐标
int BoxX = 2, BoxY = 3;
//定义二维数组表示游戏的地图
	//这个是游戏进行的地图
	int GameMap[10][10] = {
		{1,1,1,1,1,1,1,1,1,1},
		{1,0,0,2,0,0,0,0,0,1},
		{1,0,0,5,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,3,1},
		{1,1,1,1,1,1,1,1,1,1},
	};
	int  k= 0;//用来切换地图
	//后面这个是储存的可切换地图
	int ReservedMap[3][10][10] = {
		{{1,1,1,1,1,1,1,1,1,1},
		{1,0,0,2,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,1,5,0,0,0,0,0,1},
		{1,0,0,0,0,1,0,1,0,1},
		{1,0,0,1,0,0,0,0,0,1},
		{1,1,0,0,1,0,0,0,0,1},
		{1,0,0,0,0,0,0,1,3,1},
		{1,0,0,0,0,1,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,1,0,0,0,0,3,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,0,1,0,0,0,0,1},
		{1,0,1,1,0,0,0,0,0,1},
		{1,0,0,0,0,5,1,1,1,1},
		{1,1,0,0,0,0,0,0,0,1},
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,0,2,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,2,1,0,0,0,1,1,4,6},
		{1,5,1,0,1,0,0,1,0,0},
		{1,0,1,0,1,1,0,1,1,0},
		{1,0,0,0,0,0,0,0,1,0},
		{1,0,1,0,1,1,0,1,0,0},
		{1,0,1,0,1,0,0,1,0,0},
		{0,0,0,0,1,0,0,0,0,0},
		{0,0,0,0,1,0,0,0,0,0},
		{1,1,1,1,1,1,1,1,1,1}}
	};

//用来保存上一步数据的数组

//储存玩家坐标的数组
int xyplayer[2][20];//最多保存最近操作的20步,两个一维数组一个存x一个存y
//储存箱子坐标的数组
int xybox[2][20];
//用来记录走过几步了,用于撤回,初始为0
int z=0;



//玩家移动函数
void GameMove(int x, int y)
{
	//每次修改之前先将数据存到撤回数组中
	xyplayer[0][z] = PosX;
	xyplayer[1][z] = PosY;
	xybox[0][z] = BoxX;
	xybox[1][z] = BoxY;
	z++;
	if (z == 20)
		z-=20;
	//修改地图数据
	GameMap[PosX][PosY] = 0;
	GameMap[PosX + x][PosY + y]=2;
//修改玩家的坐标
	PosX += x;
	PosY += y;
}







//修改箱子的函数
void BoxMove(int x, int y)
{	//每次修改之前先将数据存到撤回数组中
	xyplayer[0][z] = PosX;
	xyplayer[1][z] = PosY;
	xybox[0][z] = BoxX;
	xybox[1][z] = BoxY;
	z++;
	if (z == 20)
		z -= 20;
	GameMap[PosX][PosY] = 0;
	GameMap[PosX + x][PosY + y] = 2;
	//修改玩家的坐标
	PosX += x;
	PosY += y;
	//修改箱子的坐标
	BoxX += x;
	BoxY += y;
	GameMap[PosX + x][PosY + y] = 5;
}







//切换地图函数
void switchmap()
{
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++)
		{
			GameMap[i][j] = ReservedMap[k][i][j];
		}
	k++;
	switch (k)
	{//这里是用来保证通关后撤回不会出错的
		//将数组所有数据修改成通关后新目标位置的数据就可以了
	case 1:for (int i = 0; i < 20; i++) { xyplayer[0][i] = 1; xyplayer[1][i] = 3; xybox[0][i] = 3; xybox[1][i] = 3; }PosX = 1; PosY = 3; BoxX = 3; BoxY = 3; break;
	case 2:for (int i = 0; i < 20; i++) { xyplayer[0][i] = 8; xyplayer[1][i] = 3; xybox[0][i] = 5; xybox[1][i] = 5; }PosX = 1; PosY = 3; BoxX = 5; BoxY = 5; break;
	case 3:for (int i = 0; i < 20; i++) { xyplayer[0][i] = 1; xyplayer[1][i] = 1; xybox[0][i] = 2; xybox[1][i] = 1; }PosX = 1; PosY = 1; BoxX = 2; BoxY = 1; break;
	}
}


//开始界面
void index()
{
	int s;
	cout << "1、开始游戏" << endl << "2、退出游戏" << endl << "(温馨提示游戏中按esc退出主界面哦)" << endl;
	cin >> s;
	if (s == 1);
	else
		exit(0);
}







//撤回函数
void regret()
{
	if (--z < 0)
		z += 20;
	if (xyplayer[0][z]>0&&xyplayer[0][z]<10)
	{
		GameMap[PosX][PosY] = 0;//玩家这一步位置改空,箱子同理
		GameMap[BoxX][BoxY] = 0;

		PosX = xyplayer[0][z];//将玩家位置调到上一步
		PosY = xyplayer[1][z];

		GameMap[PosX][PosY] = 2;//玩家上一步位置改成玩家,箱子同理
		BoxX = xybox[0][z];
		BoxY = xybox[1][z];
		GameMap[BoxX][BoxY] = 5;
	}
	
}





int main()
{
	index();
	int l = 0;
	while (true)//游戏循环
	{
		//打印地图
		if (!l)
		{
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					switch (GameMap[i][j])
					{
					case 0:
						cout << "  ";
						break;
					case 1:
					case 6:
						cprint("■", 2);
						break;
					case 2:
						cout << "♀";
						break;
					case 3:
						SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
							FOREGROUND_BLUE);
						cout<<"门";
						break;
					case 4:
						SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
							FOREGROUND_BLUE); 
						cout<<"门";
						break;
					case 5:
						cprint("箱", 4);
					}
				}
				cout << endl;
			}





			//按键响应

			//获取键盘中输入的字符:
			int Input = _getch();
			system("CLS");//清屏
			switch (Input)
			{
			case 's':
			case 'S':
				//判断下一个位置是否可以移动
				if (0 == GameMap[PosX + 1][PosY])
				{
					GameMove(1, 0);
				}
				//判断是否满足成功条件,切换地图
				if (GameMap[PosX + 1][PosY] == 5&& GameMap[PosX + 2][PosY] == 3)
					switchmap();
				//判断箱子能否被推动
				if (GameMap[PosX + 1][PosY] == 5 && GameMap[PosX + 2][PosY] == 0)
					BoxMove(1, 0);
				//每次按下按键后
				break;
			case 'a':
			case 'A':
				//判断下一个位置是否可以移动
				if (0 == GameMap[PosX][PosY - 1])
				{
					GameMove(0, -1);
				}
				if (GameMap[PosX][PosY - 1] == 5 && GameMap[PosX][PosY-2] == 3)
					switchmap();
				if (GameMap[PosX][PosY-1] == 5 && GameMap[PosX][PosY-2] == 0)
					BoxMove(0, -1);
				break;

			case 'D':
			case 'd':
				//判断下一个位置是否可以移动
				if (0 == GameMap[PosX][PosY + 1])
				{
					GameMove(0, 1);
				}
				if (GameMap[PosX][PosY + 1] == 5 && GameMap[PosX][PosY+2] == 3)
					switchmap();
				if (GameMap[PosX][PosY+1] == 5 && GameMap[PosX][PosY+2] == 0)
					BoxMove(0, 1);
				break;

			case 'W':
			case 'w':
				//判断下一个位置是否可以移动
				if (0 == GameMap[PosX - 1][PosY])//玩家移动
				{
					GameMove(-1, 0);
				}
				if (GameMap[PosX - 1][PosY] == 5 && GameMap[PosX - 2][PosY] == 3)
					switchmap();
				if (GameMap[PosX - 1][PosY] == 5 && GameMap[PosX - 2][PosY] == 0)//箱子移动
					BoxMove(-1, 0);
				if (GameMap[PosX - 1][PosY] == 5 && GameMap[PosX - 2][PosY] == 6)
				{
					cout << endl << endl << endl << endl << "                ";
					cprint("恭喜通关!", 12);
					cout << endl << endl << endl << endl;
					l = 1;
				}
				break;
			case 27:
				index();
				break;
			case 'z':
			case 'Z':regret(); break;
			}
		}
		else
			break;

	}
	


	return 0;
}

如有问题欢迎提出和修改,实现思路都写在代码里面了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值