C++实现推箱子游戏

C++实现推箱子游戏

设计地图

使用二维数组搭建地图,
其中0为虚空、1为墙壁、2为玩家、3为箱子、4为目标点。

int map[HEIGHT][WIDTH] = 
{
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 4, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 3, 0, 3, 4, 1},
	{1, 4, 0, 3, 2, 0, 0, 1},
	{1, 0, 0, 0, 3, 0, 0, 1},
	{1, 0, 0, 0, 4, 0, 0, 1},
	{1, 1, 1, 1, 1, 1, 1, 1}
};

可以根据实际需求改变地图,记得将HEIGHT和WIDTH的值按地图大小改变。

地图实例化

设计出来的地图需要将其可视化,即打印到屏幕上。遍历数组所有元素,使用switch语句将各个元素转化为可视化图形。

void drawMap()
{
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < lenth; j++)
		{
			switch (map[i][j])
			{
			case 1:                     
				cout << "* ";            //墙壁
				break;
			case 2:
				cout << "P ";            //玩家
				break;
			case 3: 
				cout << "+ ";            //箱子
				break;
			case 4:
				cout << "G ";            //目标
				break;
			case 0:
				cout << "  ";            //空气
				break;
			}
		}
		cout << endl;
	}
}

玩家定位

原理同地图实例化,遍历数组,找出玩家所在位置,为玩家建立一个坐标(x,y)

for (int i = 0; i < width; i++)
{
	for (int j = 0; j < lenth; j++)
	{
		if (map[i][j] == 2)
		{
			x = i;
			y = j;
		}
	}
}

注意将x,y定义在全局下。

方向控制

这里以玩家向上移动进行举例。
当玩家向上移动时

上方是空气
if (map[x-1][y] == 0)
	{
		map[x-1][y] = map[x][y];
		map[x][y] = 0;
	}

此时玩家移动到了上方的位置,原来的地方变成了空气。

上方是箱子
if (map[x - 1][y] == 3)
	{
		if (map[x - 2][y] == 0)
		{
			map[x - 2][y] = map[x - 1][y];
			map[x - 1][y] = map[x][y];
			map[x][y] = 0;
		}
		else if (map[x - 2][y] == 4)
		{
			box--;
			map[x - 2][y] = map[x - 1][y];
			map[x - 1][y] = map[x][y];
			map[x][y] = 0;
		}
	}

其中分为箱子的上方是空气和箱子上方是目标点。

记得在该函数结束时更新玩家的坐标

x = x - 1;
y = y;

其他三个方位同理。

控制方式

同样使用switch语句将键盘输入的内容判断,进行操控。在游戏进行当中,需要一个无限循环来防止游戏中断。

while(true)
{	
	system("cls");
	if (box == 0)
	{
		break;
	}
	drawMap();
	char tap;
	cin >> tap;
	switch (tap)
	{
	case 'w':
		moveUP();
		break;
	case 's':
		moveDown();
		break;
	case 'd':
		moveRight();
		break;
	case 'a':
		moveLeft();
		break;
	}
}

判断结束

定义一个整形箱子数量,当未移动到指定位置的箱子数量为零,则跳出死循环,程序结束。

遍历数组,查询箱子的个数

for (int i = 0; i < width; i++)
{
	for (int j = 0; j < lenth; j++)
	{
		if (map[i][j] == 4)
		{
			box++;
		}
	}
}

若箱子为零,程序结束

if (box == 0)
{
	break;
}

源码

#include<iostream>

using namespace std;
const int lenth = 8;
const int width = 8;
int x, y;
int box = 0;

int map[width][lenth] = 
{
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 4, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 3, 0, 3, 4, 1},
	{1, 4, 0, 3, 2, 0, 0, 1},
	{1, 0, 0, 0, 3, 0, 0, 1},
	{1, 0, 0, 0, 4, 0, 0, 1},
	{1, 1, 1, 1, 1, 1, 1, 1}
};

void drawMap()
{
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < lenth; j++)
		{
			switch (map[i][j])
			{
			case 1:                     
				cout << "* ";            //墙壁
				break;
			case 2:
				cout << "P ";            //玩家
				break;
			case 3: 
				cout << "+ ";            //箱子
				break;
			case 4:
				cout << "G ";            //目标
				break;
			case 0:
				cout << "  ";            //空气
				break;
			}
		}
		cout << endl;
	}
}

void findP()
{
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < lenth; j++)
		{
			if (map[i][j] == 2)
			{
				x = i;
				y = j;
			}
			if (map[i][j] == 4)
			{
				box++;
			}
		}
	}
}

void moveUP()
{
	if (map[x-1][y] == 0)
	{
		map[x-1][y] = map[x][y];
		map[x][y] = 0;
	}
	if (map[x - 1][y] == 3)
	{
		if (map[x - 2][y] == 0)
		{
			map[x - 2][y] = map[x - 1][y];
			map[x - 1][y] = map[x][y];
			map[x][y] = 0;
		}
		else if (map[x - 2][y] == 4)
		{
			box--;
			map[x - 2][y] = map[x - 1][y];
			map[x - 1][y] = map[x][y];
			map[x][y] = 0;
		}
	}
	x = x - 1;
	y = y;
}

void moveDown()
{
	if (map[x + 1][y] == 0)
	{
		map[x + 1][y] = map[x][y];
		map[x][y] = 0;
	}
	if (map[x + 1][y] == 3)
	{
		if (map[x + 2][y] == 0)
		{
			map[x + 2][y] = map[x + 1][y];
			map[x + 1][y] = map[x][y];
			map[x][y] = 0;
		}
		else if (map[x + 2][y] == 4)
		{
			map[x + 2][y] = map[x + 1][y];
			map[x + 1][y] = map[x][y];
			map[x][y] = 0;
			box--;
		}
	}
	x = x + 1;
	y = y;
}

void moveRight()
{
	if (map[x][y+1] == 0)
	{
		map[x][y+1] = map[x][y];
		map[x][y] = 0;
	}
	if (map[x][y+1] == 3)
	{
		if (map[x][y+2] == 0)
		{
			map[x ][y+2] = map[x][y+1];
			map[x ][y+1] = map[x][y];
			map[x][y] = 0;
		}
		else if (map[x][y+2] == 4)
		{
			map[x][y + 2] = map[x][y + 1];
			map[x][y + 1] = map[x][y];
			map[x][y] = 0;
			box--;
		}
	}
	x = x;
	y = y + 1;
}

void moveLeft()
{
	if (map[x][y - 1] == 0)
	{
		map[x][y - 1] = map[x][y];
		map[x][y] = 0;
	}
	if (map[x][y - 1] == 3)
	{
		if (map[x][y - 2] == 0)
		{
			map[x][y - 2] = map[x][y - 1];
			map[x][y - 1] = map[x][y];
			map[x][y] = 0;
		}
		else if (map[x][y - 2] == 4)
		{
			map[x][y - 2] = map[x][y - 1];
			map[x][y - 1] = map[x][y];
			map[x][y] = 0;
			box--;
		}
	}
	x = x;
	y = y - 1;
}
int main()
{
	findP();
	while(true)
	{	
		system("cls");
		if (box == 0)
		{
			break;
		}
		drawMap();
		char tap;
		cin >> tap;
		switch (tap)
		{
		case 'w':
			moveUP();
			break;
		case 's':
			moveDown();
			break;
		case 'd':
			moveRight();
			break;
		case 'a':
			moveLeft();
			break;
		}
	}
	cout << "U WIN!!!!!!!!!!" << endl;
	system("pause");
}
简易推箱子 #include "Map.h" #include <conio.h> int XDest = 4; int YDest = 4; void CMap::Init() { int i,j; CPoint cPoint; CSprite *pSphte; CPlayer cPlay; CBox cBox; CDest cDest; int XPlayer = 4; int YPlayer = 6; int XBox = 4; int YBox = 5; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (i == 0 || j == 0 || i == 9 || j == 9) { cPoint.SetPoint(i, j); pSphte = new CWall(cPoint); m_nMap[i][j] = pSphte; } else if (i == XPlayer && j == YPlayer) { cPoint.SetPoint(i,j); pSphte = new CPlayer(cPoint); m_nMap[i][j] = pSphte; } else if (i == XBox && j == YBox) { cPoint.SetPoint(i,j); pSphte = new CBox(cPoint); m_nMap[i][j] = pSphte; } else if (i == XDest && j == YDest) { cPoint.SetPoint(i,j); pSphte = new CDest(cPoint); m_nMap[i][j] = pSphte; } else { cPoint.SetPoint(i, j); pSphte = new CSpace(cPoint); m_nMap[i][j] = pSphte; } } cout<<endl; } } void CMap::Draw() { int i,j; for (i=0; i<10; i++) { for (j=0; j<10; j++) { cout<<m_nMap[i][j]->GetSprite(); } cout<<endl; } } void CMap::Logic() { int n = getch(); //获取输入的数字 char ch = char(n);//将获取到的数字进行强制类型转换 CPlayer *pPlayer = GetPlayer(); int XPlayer = pPlayer->GetX(); int YPlayer = pPlayer->GetY(); CBox *pBox = GetBox(); int XBox = pBox->GetX(); int YBox = pBox->GetY(); CDest *pDest = GetDest(); switch(ch) { case 'a': { int yPlayer = pPlayer->GetY(); YPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer++; YBox++; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'd': { int yPlayer = pPlayer->GetY(); YPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer--; YBox--; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'w': { int xPlayer = pPlayer->GetX(); XPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer++; XBox++; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; case 's': { int xPlayer = pPlayer->GetX(); XPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer--; XBox--; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; default: break; } if ((pPlayer->GetX() == XDest && pPlayer->GetY() == YDest) || (pBox->GetX() == XDest && pBox->GetY() == YDest)) { int i,j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { m_nMap[i][j]->SetSprite(" "); } } } } else if (m_nMap[XDest][YDest]->GetSprite() == " ") { m_nMap[XDest][YDest]->SetSprite("◎"); } if (m_nMap[XDest][YDest]->GetSprite() == "■") { m_nMap[XDest][YDest]->SetSprite("●"); // cout<<"游戏结束"<<endl; } if (pBox->GetX() != XDest || pBox->GetY() != YDest) { pBox->SetSprite("■"); } } CPlayer *CMap::GetPlayer(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "♀") { return (CPlayer *)m_nMap[i][j]; } } } } CBox *CMap::GetBox(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "■" || m_nMap[i][j]->GetSprite() == "●") { return (CBox *)m_nMap[i][j]; } } } } CDest *CMap::GetDest(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { return (CDest *)m_nMap[i][j]; } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值