天天看點

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;
}

           

繼續閱讀