推箱子小游戏c++

游戏名称

推箱子小游戏

游戏玩法

请见游戏进行是提示

不说了,上代码~~~

源码

#include<bits/stdc++.h>
#include<conio.h>
#include <windows.h>

using namespace std;

void op(HANDLE hout);
void tishi(HANDLE hout);
void gotoxy(HANDLE hout, int x, int y);
int newmp(HANDLE hout);
void Move(HANDLE hout);
int finish();
void setmap(int n);
void color(int m);
bool flag = true;
int pass = 1;

#define R 10
#define C 10
#define framex 20
#define framey 14

int mp[R][C];

int map1[R][C] = { //图1
	{ 0, 0, 1, 1, 1, 0, 0, 0 },
	{ 0, 0, 1, 3, 1, 0, 0, 0 },
	{ 0, 0, 1, 0, 1, 1, 1, 1 },
	{ 1, 1, 1, 0, 0, 4, 3, 1 },
	{ 1, 3, 4, 4, 0, 1, 1, 1 },
	{ 1, 1, 1, 5, 4, 1, 0, 0 },
	{ 0, 0, 0, 1, 3, 1, 0, 0 },
	{ 0, 0, 0, 1, 1, 1, 0, 0 }
};

int map2[R][C] = { //图2
	{1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
	{1, 5, 0, 0, 1, 0, 0, 0, 0, 0},
	{1, 0, 4, 4, 1, 0, 1, 1, 1, 0},
	{1, 0, 4, 0, 1, 0, 1, 3, 1, 0},
	{1, 1, 1, 0, 1, 1, 1, 3, 1, 0},
	{0, 1, 1, 0, 0, 0, 0, 3, 1, 0},
	{0, 1, 0, 0, 0, 1, 0, 0, 1, 0},
	{0, 1, 0, 0, 0, 1, 1, 1, 1, 0},
	{0, 1, 1, 1, 1, 1, 0, 0, 0, 0}
};

int map3[R][C] = { //图3
	{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
	{ 0, 0, 1, 1, 0, 0, 1, 0, 5, 1 },
	{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
	{ 0, 0, 1, 4, 0, 4, 0, 4, 0, 1 },
	{ 0, 0, 1, 0, 4, 1, 1, 0, 0, 1 },
	{ 1, 1, 1, 0, 4, 0, 1, 0, 1, 1 },
	{ 1, 3, 3, 3, 3, 3, 0, 0, 1, 0 },
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
};

void op(HANDLE hout) { //初始菜单
	system("cls");//任意键输入

	gotoxy(hout, framex, 1);
	cout << "*********************";

	gotoxy(hout, framex, 3);
	cout << "   推箱子    ";

	gotoxy(hout, framex, 5);
	cout << "   按s开始   ";

	gotoxy(hout, framex, 7);
	cout << "  按q或Q退出   ";

	gotoxy(hout, framex, 9);
	cout << "游戏前请开启英文输入 ";

	gotoxy(hout, framex, 11);
	cout << "**********************";

	_getch();
};

void tishi(HANDLE hout) { //提示操作
	system("cls");

	gotoxy(hout, framex, 1);
	cout << "*****************************";

	gotoxy(hout, framex, 3);
	cout << "   按方向键上下左右移动  ";

	gotoxy(hout, framex, 5);
	cout << " 所有箱子到达目的地游戏胜利 ";

	gotoxy(hout, framex, 7);
	cout << "     按q退出     ";

	gotoxy(hout, framex, 9);
	cout << "*****************************";
};

void gotoxy(HANDLE hout, int x, int y) { //移动
	COORD pos;

	pos.X = x;
	pos.Y = y;

	SetConsoleCursorPosition(hout, pos);
};

void print(int i) {
	switch (i) {
		case 0:
			color(0x7);
			cout << " ";
			break;

		case 1:
			color(8);
			cout << "■";
			break;

		case 3:
			color(0xE);
			cout << "◇";
			break;

		case 4:
			color(4);
			cout << "□";
			break;

		case 5:
			color(3);
			cout << "♀";
			break;

		case 7:
			color(6);
			cout << "◆";
			break;

		case 8:
			color(3);
			cout << "♀";
			break;

		default:
			break;
	}
}

int newmp(HANDLE hout) { //加载新地图
	gotoxy(hout, framex + C, framey - 3);

	color(0xF);

	cout << "第" << pass << "关";

	for (int i = 0; i < R; i++) {
		gotoxy(hout, framex, framey + i);

		for (int j = 0; j < C; j++) {
			print(mp[i][j]);
		}
	}

	return 0;
};

void Move(HANDLE hout) { //移动自己
	int x = 0, y = 0;

	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) {
			if (mp[i][j] == 5 || mp[i][j] == 8) {
				x = i;
				y = j;

				break;
			}
		}
	}

	gotoxy(hout, framex, framey + R);

	color(0xF);

	printf("当前位置:(%d, %d)", x, y);

	int ch = _getch();

	switch (ch) {
		case 'w':
		case 72:

			if (mp[x - 1][y] == 0 || mp[x - 1][y] == 3) {
				mp[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(mp[x][y]);
				mp[x - 1][y] += 5;
				gotoxy(hout, framex + 2 * y, framey + x - 1);
				print(mp[x - 1][y]);
			} else if (mp[x - 1][y] == 4 || mp[x - 1][y] == 7) {
				if (mp[x - 2][y] == 0 || mp[x - 2][y] == 3) {
					mp[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(mp[x][y]);
					mp[x - 1][y] += 1;
					gotoxy(hout, framex + 2 * y, framey + x - 1);
					print(mp[x - 1][y]);
					mp[x - 2][y] += 4;
					gotoxy(hout, framex + 2 * y, framey + x - 2);
					print(mp[x - 2][y]);
				}
			}
			break;

		case 's':
		case 80:
			if (mp[x + 1][y] == 0 || mp[x + 1][y] == 3) {
				mp[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(mp[x][y]);
				mp[x + 1][y] += 5;
				gotoxy(hout, framex + 2 * y, framey + x + 1);
				print(mp[x + 1][y]);
			} else if (mp[x + 1][y] == 4 || mp[x + 1][y] == 7) {
				if (mp[x + 2][y] == 0 || mp[x + 2][y] == 3) {
					mp[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(mp[x][y]);
					mp[x + 1][y] += 1;
					gotoxy(hout, framex + 2 * y, framey + x + 1);
					print(mp[x + 1][y]);
					mp[x + 2][y] += 4;
					gotoxy(hout, framex + 2 * y, framey + x + 2);
					print(mp[x + 2][y]);
				}
			}
			break;

		case 'a':
		case 75:
			if (mp[x][y - 1] == 0 || mp[x][y - 1] == 3) {
				mp[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(mp[x][y]);
				mp[x][y - 1] += 5;
				gotoxy(hout, framex + 2 * y - 2, framey + x);
				print(mp[x][y - 1]);
			} else if (mp[x][y - 1] == 4 || mp[x][y - 1] == 7) {
				if (mp[x][y - 2] == 0 || mp[x][y - 2] == 3) {
					mp[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(mp[x][y]);
					mp[x][y - 1] += 1;
					gotoxy(hout, framex + 2 * y - 2, framey + x);
					print(mp[x][y - 1]);
					mp[x][y - 2] += 4;
					gotoxy(hout, framex + 2 * y - 4, framey + x);
					print(mp[x][y - 2]);
				}
			}
			break;

		case 'd':
		case 77:
			if (mp[x][y + 1] == 0 || mp[x][y + 1] == 3) {
				mp[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(mp[x][y]);
				mp[x][y + 1] += 5;
				gotoxy(hout, framex + 2 * y + 2, framey + x);
				print(mp[x][y + 1]);
			} else if (mp[x][y + 1] == 4 || mp[x][y + 1] == 7) {
				if (mp[x][y + 2] == 0 || mp[x][y + 2] == 3) {
					mp[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(mp[x][y]);
					mp[x][y + 1] += 1;
					gotoxy(hout, framex + 2 * y + 2, framey + x);
					print(mp[x][y + 1]);
					mp[x][y + 2] += 4;
					gotoxy(hout, framex + 2 * y + 4, framey + x);
					print(mp[x][y + 2]);
				}
			}
			break;
			
		case 'q':
			flag = false;
			
		default:
			break;
	}
};

int finish() { //结束游戏 
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) {
			if (mp[i][j] == 4) { 
				return 0;
			}  
		}
	}
	
	return 1;
};

void setmap(int n) {
	switch (n) {
		case 1:
			memcpy(mp, map1, sizeof(map1));
			break;
			
		case 2:
			memcpy(mp, map2, sizeof(map2));
			break;
		case 3:
			
			memcpy(mp, map3, sizeof(map3));
			break;
	}
};
void color(int m) 
{
	HANDLE consolehend;
	
	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
	
	SetConsoleTextAttribute(consolehend, m);
};

int main() {
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);

	op(hout);
	
	char ch = getch();
	
	setmap(pass);
	tishi(hout);
	newmp(hout);
	
	if (ch == 'q') {
		return 0;
	}
	
	while (flag==1) {
		Move(hout);
		
		if (finish()) {
			newmp(hout);
			
			gotoxy(hout, framex, framey + R);
			cout << " 恭喜,成功过关!";
			
			gotoxy(hout, framex, framey + R + 2);
			cout << "重玩(R)";
			
			ch = getch();
			
			system("cls");
			
			pass++;
			
			if (ch == 'r' || ch == 'R')pass--;
			if (pass > 3) {
				gotoxy(hout, framex, framey);
				cout << " 您已通过全部关卡!";
				
				getch();
				
				flag = false;
			} else {
				setmap(pass);
				tishi(hout);
				newmp(hout);
			}
		}
	}
	
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的推箱子游戏C++ 代码示例: ```c++ #include <iostream> #include <vector> using namespace std; const int MAXN = 20; int n, m; char map[MAXN][MAXN]; int sx, sy; // 玩家的起始位置 int ex, ey; // 终点的位置 struct Node { int x, y; // 箱子的位置 bool operator==(const Node& other) const { return x == other.x && y == other.y; } }; vector<Node> boxes; // 所有箱子的位置 bool check(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m) return false; if (map[x][y] == '#') return false; for (auto& box : boxes) { if (box.x == x && box.y == y) return false; } return true; } bool dfs(int x, int y, int bx, int by) { if (bx == ex && by == ey) return true; if (!check(bx, by)) return false; boxes.erase(find(boxes.begin(), boxes.end(), Node{bx, by})); bool res = false; if (bx > x && dfs(bx, by, bx + 1, by)) res = true; if (bx < x && dfs(bx, by, bx - 1, by)) res = true; if (by > y && dfs(bx, by, bx, by + 1)) res = true; if (by < y && dfs(bx, by, bx, by - 1)) res = true; boxes.push_back(Node{bx, by}); return res; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> map[i][j]; if (map[i][j] == 'S') { sx = i; sy = j; } else if (map[i][j] == 'E') { ex = i; ey = j; } else if (map[i][j] == 'B') { boxes.push_back(Node{i, j}); } } } bool flag = true; for (auto& box : boxes) { if (!dfs(sx, sy, box.x, box.y)) { flag = false; break; } } if (flag) cout << "Yes" << endl; else cout << "No" << endl; return 0; } ``` 这个代码使用 DFS 搜索所有箱子的位置,判断是否能够推到终点。其中 `check` 函数用于判断一个位置是否可以到达,`dfs` 函数用于搜索箱子的位置。这个代码只是一个简单的示例,实际上还有很多细节需要处理,比如判断是否有多个箱子在同一个位置等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

harmis_yz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值