天天看点

C++控制台游戏【推箱子】-- 画出箱子,人推箱子,箱子和人不能撞墙;画三个箱子,当箱子为两个,不能进行推动。

实现画出箱子,人推箱子,箱子和人不能撞墙;

做三个箱子,当两个箱子时不能推。

注意将变量换成结构体中的对象属性(如nPlayerRow 换成 player.nRow)

增加箱子的数量为3

注意:当箱子撞墙时,箱子和玩家均返回备份坐标。

当两个箱子相撞时,两个箱子与玩家均返回备份坐标。

// 2021.2.25(推箱子).cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;
#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code)&0x8000?1:0

int _tmain(int argc, _TCHAR* argv[])
{

	int nMenuState = 0;//用于控制箭头
	int nGameState = 0;//

	struct  SPlayer
	{
		int nRow ;
		int nCol ;
		int nRowBk ;
		int nColBk ;
	};

	struct SBox
	{
		int nBoxRow;
		int nBoxCol;
		int nBoxRowBk;
		int nBoxColBk;

		int nBoxRow2 ;
		int nBoxCol2 ;
		int nBoxRowBk2 ;
		int nBoxColBk2 ;

		int nBoxRow3 ;
		int nBoxCol3 ;
		int nBoxRowBk3 ;
		int nBoxColBk3 ;

	};

	//玩家的定义
	SPlayer player;
	SBox box;
	SBox box2;
	SBox box3;

	//玩家对象属性的访问:对象名.属性名
	player.nRow = 7;//定义玩家的行
	player.nCol = 10;//定义玩家的列
	player.nRowBk = 0;
	player.nColBk = 0;


	box.nBoxRow = 3;//定义箱子的行
	box.nBoxCol = 8;
	box.nBoxRowBk = 0;
	box.nBoxColBk = 0;

	box2.nBoxRow = 6;//定义箱子的行
	box2.nBoxCol = 8;
	box2.nBoxRowBk = 0;
	box2.nBoxColBk = 0;

	box3.nBoxRow = 5;//定义箱子的行
	box3.nBoxCol = 9;
	box3.nBoxRowBk = 0;
	box3.nBoxColBk = 0;


	int arrMap[15][20] = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 }
	};


	while (true)//用于进行循环刷新
	{
		//菜单界面
		if (0 == nGameState)//如果界面的值为0,当前为菜单界面
		{

			if (KEY_DOWN(VK_DOWN))//检测是否按下向下箭头
			{
				nMenuState++;//控制箭头的值增加1
				if (nMenuState > 2)//因为菜单一共只有三个选项,所以当控制箭头的数量大于2时,重新赋值为0,返回到箭头的初始位置
				{
					nMenuState = 0;
				}
			}

			else if (KEY_DOWN(VK_UP))//同上
			{
				nMenuState--;
				if (nMenuState < 0)
				{
					nMenuState = 2;
				}
			}

			if (KEY_DOWN(VK_RETURN))//检测是否按下Enter键
			{

				if (0 == nMenuState)//如果当前箭头位置为游戏开始
				{
					nGameState = 1;//设置界面的值为1,从菜单界面切换为地图界面
				}
				else if (1 == nMenuState)//如果当前箭头位置为游戏设置
				{
					nGameState = 2;//设置界面的值为2,从菜单界面切换为游戏设置界面
				}
				else if (2 == nMenuState){
					nGameState = 3;
				}
			}

			//画出菜单
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;//因为放在while(true)里面,所以这里会一直运行
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;

			if (0 == nMenuState)//画箭头位置
			{
				cout << "■■■        ->游戏开始          ■■■" << endl;
				cout << "■■■          游戏设置          ■■■" << endl;
				cout << "■■■          游戏退出          ■■■" << endl;
			}
			else if (1 == nMenuState)
			{
				cout << "■■■          游戏开始          ■■■" << endl;
				cout << "■■■        ->游戏设置          ■■■" << endl;
				cout << "■■■          游戏退出          ■■■" << endl;
			}
			else if (2 == nMenuState){
				cout << "■■■          游戏开始          ■■■" << endl;
				cout << "■■■          游戏设置          ■■■" << endl;
				cout << "■■■        ->游戏退出          ■■■" << endl;
			}
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;

		}




		//地图界面
		if (1 == nGameState)
		{

			//在玩家移动前对坐标进行备份,把坐标赋给值的变量
			player.nRowBk = player.nRow;//nPlayerRow = 3; nPlayerCol = 3
			player.nColBk = player.nCol;
			box.nBoxRowBk = box.nBoxRow;
			box.nBoxColBk = box.nBoxCol;

			box2.nBoxRowBk = box2.nBoxRow;
			box2.nBoxColBk = box2.nBoxCol;
			box3.nBoxRowBk = box3.nBoxRow;
			box3.nBoxColBk = box3.nBoxCol;

			if (KEY_DOWN(VK_SPACE))//按空格键
			{
				nGameState = 0;//返回菜单界面
			}

			//数据更新,要在渲染的前面
			//设置按键让小人移动
			if (KEY_DOWN(VK_UP))
			{
				player.nRow--;//玩家向上移动,nPlayerRow = 2
			}
			else if (KEY_DOWN(VK_DOWN))
			{
				player.nRow++;
			}
			else if (KEY_DOWN(VK_LEFT))
			{
				player.nCol--;
			}
			else if (KEY_DOWN(VK_RIGHT))
			{
				player.nCol++;
			}

			//检测玩家是否撞墙
			if (1 == arrMap[player.nRow][player.nCol]) //当玩家的值为1
			{
				player.nRow = player.nRowBk;//把现在玩家的值赋值给之前的索引,nPlayerRow = 3,位置不变
				player.nCol = player.nColBk;
			}


			//实现玩家推箱子
			if ( box.nBoxRow ==player.nRow && box.nBoxCol ==player.nCol)
			{
				box.nBoxRow += (player.nRow - player.nRowBk);
				box.nBoxCol += (player.nCol - player.nColBk);
			}
			else if (box2.nBoxRow == player.nRow && box2.nBoxCol == player.nCol)
			{
				box2.nBoxRow += (player.nRow - player.nRowBk);
				box2.nBoxCol += (player.nCol - player.nColBk);
			}
			else if (box3.nBoxRow == player.nRow && box3.nBoxCol == player.nCol)
			{
				box3.nBoxRow += (player.nRow - player.nRowBk);
				box3.nBoxCol += (player.nCol - player.nColBk);
			}

			//检测箱子是否撞墙
			if (1 == arrMap[box.nBoxRow][box.nBoxCol])
			{
				box.nBoxRow = box.nBoxRowBk;
				box.nBoxCol = box.nBoxColBk;
				player.nRow = player.nRowBk;
				player.nCol = player.nColBk;
			}
			else if (1 == arrMap[box2.nBoxRow][box2.nBoxCol])
			{
				box2.nBoxRow = box2.nBoxRowBk;
				box2.nBoxCol = box2.nBoxColBk;
				player.nRow = player.nRowBk;
				player.nCol = player.nColBk;
			}
			else if (1 == arrMap[box3.nBoxRow][box3.nBoxCol])
			{
				box3.nBoxRow = box3.nBoxRowBk;
				box3.nBoxCol = box3.nBoxColBk;
				player.nRow = player.nRowBk;
				player.nCol = player.nColBk;
			}

			//检测是否两个箱子相遇
			if (box.nBoxRow == box2.nBoxRow && box.nBoxCol == box2.nBoxCol)
			{
				
				box.nBoxRow = box.nBoxRowBk;
				box.nBoxCol = box.nBoxColBk;
				box2.nBoxRow = box2.nBoxRowBk;
				box2.nBoxCol = box2.nBoxColBk;
				player.nRow = player.nRowBk;
				player.nCol = player.nColBk;
			}
			else if (box.nBoxRow == box3.nBoxRow && box.nBoxCol == box3.nBoxCol)
			{
				box.nBoxRow = box.nBoxRowBk;
				box.nBoxCol = box.nBoxColBk;
				box3.nBoxRow = box3.nBoxRowBk;
				box3.nBoxCol = box3.nBoxColBk;
				player.nRow = player.nRowBk;//玩家位置不变
				player.nCol = player.nColBk;
			}
			else if (box2.nBoxRow == box3.nBoxRow && box2.nBoxCol == box3.nBoxCol)
			{
				box2.nBoxRow = box2.nBoxRowBk;
				box2.nBoxCol = box2.nBoxColBk;
				box3.nBoxRow = box3.nBoxRowBk;
				box3.nBoxCol = box3.nBoxColBk;
				player.nRow = player.nRowBk;//玩家位置不变
				player.nCol = player.nColBk;
			}



			//渲染
			//二维数组的遍历
			for (int i = 0; i < 15; i++) //行数为15
			{
				for (int j = 0; j < 20; j++)//列数为20
				{
					//判断第i行第j列应该画什么
					if (1 == arrMap[i][j])//把数字为1的地方替换为■,作为墙壁
					{
						cout << "■";
					}
					else if (i == player.nRow && j == player.nCol)//如果当前的行为玩家的行,当前的列为玩家的列(当前的位置为玩家的位置)
					{
						cout << "人";
					}
					else if (i== box.nBoxRow && j == box.nBoxCol)
					{
						cout << "箱";
					}
					else if (i == box2.nBoxRow && j == box2.nBoxCol)
					{
						cout << "箱";
					}
					else if (i == box3.nBoxRow && j == box3.nBoxCol)
					{
						cout << "箱";
					}
					else
					{
						cout << "  ";//数字为0的地方设为空格
					}
				}
				cout << endl;//换行,排列成二维数组的形状
			}

		}





		//设置界面

		if (2 == nGameState)
		{
			if (KEY_DOWN(VK_SPACE))
			{
				nGameState = 0;
			}
			cout << "我是设置页面" << endl;
		}

		//退出界面
		else if (3 == nGameState)
		{
			if (KEY_DOWN(VK_SPACE))
			{
				nGameState = 0;
			}
			cout << "我是退出页面" << endl;
		}
		system("cls");//系统清屏



	}
	system("pause");
	return 0;
}

           

代码优化版:优化了重复代码的处理(使用for循环)

注意:bool判断的使用 偏移量的概念

// 推箱子(三个箱子重复代码优化版).cpp : 定义控制台应用程序的入口点。
//
// 2021.2.25(推箱子).cpp : 定义控制台应用程序的入口点。
//实现画出箱子,人推箱子,箱子和人不能撞墙;做三个箱子,当两个箱子时不能推。  注意将变量换成结构体中的对象属性(如nPlayerRow 换成 player.nRow)

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;
#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code)&0x8000?1:0

int _tmain(int argc, _TCHAR* argv[])
{

	int nMenuState = 0;//用于控制箭头
	int nGameState = 0;//

	//定义玩家的结构体
	struct  SPlayer
	{
		int nRow;
		int nCol;
		int nRowBk;
		int nColBk;
	};

	//玩家的定义
	SPlayer player;

	//玩家对象属性的访问:对象名.属性名
	player.nRow = 7;//定义玩家的行
	player.nCol = 10;//定义玩家的列
	player.nRowBk = 0;
	player.nColBk = 0;

	/*struct SBox
	{
		int nBoxRow;
		int nBoxCol;
		int nBoxRowBk;
		int nBoxColBk;

		int nBoxRow2;
		int nBoxCol2;
		int nBoxRowBk2;
		int nBoxColBk2;

		int nBoxRow3;
		int nBoxCol3;
		int nBoxRowBk3;
		int nBoxColBk3;
	};
	
	SBox box;
	SBox box2;
	SBox box3;

	box.nBoxRow = 3;//定义箱子的行
	box.nBoxCol = 8;
	box.nBoxRowBk = 0;
	box.nBoxColBk = 0;

	box2.nBoxRow = 6;//定义箱子的行
	box2.nBoxCol = 8;
	box2.nBoxRowBk = 0;
	box2.nBoxColBk = 0;

	box3.nBoxRow = 5;//定义箱子的行
	box3.nBoxCol = 9;
	box3.nBoxRowBk = 0;
	box3.nBoxColBk = 0;
	*/

	struct SBox
	{
		int nBoxRow;
		int nBoxCol;
		int nBoxRowBk;
		int nBoxColBk;
	};

	//因为箱子的数量有多个,定义为数组
	SBox arrBox[3];
	for (int k = 0; k < 3;k++)
	{
		arrBox[k].nBoxRow = 7;
		arrBox[k].nBoxCol = 7 + k;//不能是arrBox[k].nBoxCol += 7;因为它是一个未定值
	}


	int arrMap[15][20] = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 }
	};


	while (true)//用于进行循环刷新
	{
		//菜单界面
		if (0 == nGameState)//如果界面的值为0,当前为菜单界面
		{

			if (KEY_DOWN(VK_DOWN))//检测是否按下向下箭头
			{
				nMenuState++;//控制箭头的值增加1
				if (nMenuState > 2)//因为菜单一共只有三个选项,所以当控制箭头的数量大于2时,重新赋值为0,返回到箭头的初始位置
				{
					nMenuState = 0;
				}
			}

			else if (KEY_DOWN(VK_UP))//同上
			{
				nMenuState--;
				if (nMenuState < 0)
				{
					nMenuState = 2;
				}
			}

			if (KEY_DOWN(VK_RETURN))//检测是否按下Enter键
			{

				if (0 == nMenuState)//如果当前箭头位置为游戏开始
				{
					nGameState = 1;//设置界面的值为1,从菜单界面切换为地图界面
				}
				else if (1 == nMenuState)//如果当前箭头位置为游戏设置
				{
					nGameState = 2;//设置界面的值为2,从菜单界面切换为游戏设置界面
				}
				else if (2 == nMenuState){
					nGameState = 3;
				}
			}

			//画出菜单
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;//因为放在while(true)里面,所以这里会一直运行
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;

			if (0 == nMenuState)//画箭头位置
			{
				cout << "■■■        ->游戏开始          ■■■" << endl;
				cout << "■■■          游戏设置          ■■■" << endl;
				cout << "■■■          游戏退出          ■■■" << endl;
			}
			else if (1 == nMenuState)
			{
				cout << "■■■          游戏开始          ■■■" << endl;
				cout << "■■■        ->游戏设置          ■■■" << endl;
				cout << "■■■          游戏退出          ■■■" << endl;
			}
			else if (2 == nMenuState){
				cout << "■■■          游戏开始          ■■■" << endl;
				cout << "■■■          游戏设置          ■■■" << endl;
				cout << "■■■        ->游戏退出          ■■■" << endl;
			}
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
			cout << "■■■■■■■■■■■■■■■■■■■■" << endl;

		}




		//地图界面
		if (1 == nGameState)
		{

			//在玩家移动前对坐标进行备份,把坐标赋给值的变量
			player.nRowBk = player.nRow;//nPlayerRow = 3; nPlayerCol = 3
			player.nColBk = player.nCol;

			for (int k = 0; k < 3;k++)
			{
				arrBox[k].nBoxRowBk = arrBox[k].nBoxRow;
				arrBox[k].nBoxColBk = arrBox[k].nBoxCol;
			}
			
			/*
			box2.nBoxRowBk = box2.nBoxRow;
			box2.nBoxColBk = box2.nBoxCol;
			box3.nBoxRowBk = box3.nBoxRow;
			box3.nBoxColBk = box3.nBoxCol;
			*/

			if (KEY_DOWN(VK_SPACE))//按空格键
			{
				nGameState = 0;//返回菜单界面
			}

			//数据更新,要在渲染的前面
			//设置按键让小人移动
			if (KEY_DOWN(VK_UP))
			{
				player.nRow--;//玩家向上移动,nPlayerRow = 2
			}
			else if (KEY_DOWN(VK_DOWN))
			{
				player.nRow++;
			}
			else if (KEY_DOWN(VK_LEFT))
			{
				player.nCol--;
			}
			else if (KEY_DOWN(VK_RIGHT))
			{
				player.nCol++;
			}

			//检测玩家是否撞墙
			if (1 == arrMap[player.nRow][player.nCol]) //当玩家的值为1
			{
				player.nRow = player.nRowBk;//把现在玩家的值赋值给之前的索引,nPlayerRow = 3,位置不变
				player.nCol = player.nColBk;
			}


			//实现玩家推箱子
			/*
			if (box.arrBox[k].nBoxRow == player.nRow && box.nBoxCol == player.nCol)
			{
			box.nBoxRow += (player.nRow - player.nRowBk);
			box.nBoxCol += (player.nCol - player.nColBk);
			}
			else if (box2.nBoxRow == player.nRow && box2.nBoxCol == player.nCol)
			{
			box2.nBoxRow += (player.nRow - player.nRowBk);
			box2.nBoxCol += (player.nCol - player.nColBk);
			}
			else if (box3.nBoxRow == player.nRow && box3.nBoxCol == player.nCol)
			{
			box3.nBoxRow += (player.nRow - player.nRowBk);
			box3.nBoxCol += (player.nCol - player.nColBk);
			}
			*/
			for (int k = 0; k < 3; k++)
			{
				if (arrBox[k].nBoxRow == player.nRow && arrBox[k].nBoxCol == player.nCol)
				{
					arrBox[k].nBoxRow += (player.nRow - player.nRowBk);
					arrBox[k].nBoxCol += (player.nCol - player.nColBk);
				}
			}


			//检测箱子是否撞墙
			/*
			if (1 == arrMap[box.nBoxRow][box.nBoxCol])
			{
			box.nBoxRow = box.nBoxRowBk;
			box.nBoxCol = box.nBoxColBk;
			player.nRow = player.nRowBk;
			player.nCol = player.nColBk;
			}
			else if (1 == arrMap[box2.nBoxRow][box2.nBoxCol])
			{
			box2.nBoxRow = box2.nBoxRowBk;
			box2.nBoxCol = box2.nBoxColBk;
			player.nRow = player.nRowBk;
			player.nCol = player.nColBk;
			}
			else if (1 == arrMap[box3.nBoxRow][box3.nBoxCol])
			{
			box3.nBoxRow = box3.nBoxRowBk;
			box3.nBoxCol = box3.nBoxColBk;
			player.nRow = player.nRowBk;
			player.nCol = player.nColBk;
			}
			*/
			for (int k = 0; k < 3; k++)
			{
				if (1 == arrMap[arrBox[k].nBoxRow][arrBox[k].nBoxCol])
				{
					arrBox[k].nBoxRow = arrBox[k].nBoxRowBk;
					arrBox[k].nBoxCol = arrBox[k].nBoxColBk;
					player.nRow = player.nRowBk;
					player.nCol = player.nColBk;
				}
			}


			//检测是否两个箱子相遇
			/*
			if (box.nBoxRow == box2.nBoxRow && box.nBoxCol == box2.nBoxCol)
			{

			box.nBoxRow = box.nBoxRowBk;
			box.nBoxCol = box.nBoxColBk;
			box2.nBoxRow = box2.nBoxRowBk;
			box2.nBoxCol = box2.nBoxColBk;
			player.nRow = player.nRowBk;
			player.nCol = player.nColBk;
			}
			else if (box.nBoxRow == box3.nBoxRow && box.nBoxCol == box3.nBoxCol)
			{
			box.nBoxRow = box.nBoxRowBk;
			box.nBoxCol = box.nBoxColBk;
			box3.nBoxRow = box3.nBoxRowBk;
			box3.nBoxCol = box3.nBoxColBk;
			player.nRow = player.nRowBk;//玩家位置不变
			player.nCol = player.nColBk;
			}
			else if (box2.nBoxRow == box3.nBoxRow && box2.nBoxCol == box3.nBoxCol)
			{
			box2.nBoxRow = box2.nBoxRowBk;
			box2.nBoxCol = box2.nBoxColBk;
			box3.nBoxRow = box3.nBoxRowBk;
			box3.nBoxCol = box3.nBoxColBk;
			player.nRow = player.nRowBk;//玩家位置不变
			player.nCol = player.nColBk;
			}
			*/
			for (int k = 0; k < 3;k++)
			{
				for (int j = 0; j <3 ; j++)
				{
					if (k!=j)
					{
						if (arrBox[k].nBoxRow == arrBox[j].nBoxRow && arrBox[k].nBoxCol == arrBox[j].nBoxCol)
						{
							arrBox[k].nBoxRow = arrBox[k].nBoxRowBk;
							arrBox[k].nBoxCol = arrBox[k].nBoxColBk;
							arrBox[j].nBoxRow = arrBox[j].nBoxRowBk;
							arrBox[j].nBoxCol = arrBox[j].nBoxColBk;
							player.nRow = player.nRowBk;
							player.nCol = player.nColBk;
						}
					}
				}	
			}
			


			//渲染
			//二维数组的遍历
			for (int i = 0; i < 15; i++) //行数为15
			{
				for (int j = 0; j < 20; j++)//列数为20
				{
					
					//else if中不能有for循环,但是又需要画多个箱子
					//用一个bool值判断是否画箱子
					bool bDrawBox = false;
	
					for (int k = 0; k < 3;k++)
					{
						if (i == arrBox[k].nBoxRow && j == arrBox[k].nBoxCol) //当箱子的行为当前的行,列为当前的列,画箱子
						{
							//由于这里不能直接画箱子,因为画箱子要放在else if的条件中,所以定义一个状态位,把状态位改为true
							bDrawBox = true;
							break;
						}
					}

					//判断第i行第j列应该画什么
					if (1 == arrMap[i][j])//把数字为1的地方替换为■,作为墙壁
					{
						cout << "■";
					}
					else if (i == player.nRow && j == player.nCol)//如果当前的行为玩家的行,当前的列为玩家的列(当前的位置为玩家的位置)
					{
						cout << "人";
					}
					else if (bDrawBox)//状态位为true,执行画箱子的操作
					{
						cout << "箱";
					}
					/*
					else if (i == arrBox[k].nBoxRow && j == arrBox[k].nBoxCol)
					{
					cout << "箱";
					}
					*/
					else
					{
						cout << "  ";//数字为0的地方设为空格
					}
				}
				cout << endl;//换行,排列成二维数组的形状
			}

		}





		//设置界面

		if (2 == nGameState)
		{
			if (KEY_DOWN(VK_SPACE))
			{
				nGameState = 0;
			}
			cout << "我是设置页面" << endl;
		}

		//退出界面
		else if (3 == nGameState)
		{
			if (KEY_DOWN(VK_SPACE))
			{
				nGameState = 0;
			}
			cout << "我是退出页面" << endl;
		}
		system("cls");//系统清屏



	}
	system("pause");
	return 0;
}

           

继续阅读