你可以试试哦23

这篇文章详细描述了一个基于C++编写的控制台推箱子游戏的第二关,涉及字符数组表示地图、键盘输入控制角色移动以及游戏通关条件。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;

char a[25][25] = {"##########",
                  "#       ##",
                  "#  O   O##",
                  "##O####  #",
                  "#       O#",
                  "# **#  O #",
                  "# **#    #",
                  "##########",
                 };

void Prepare() {
	system("color c1");
	HANDLE             hOut;
	CONSOLE_CURSOR_INFO curInfo;

	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	curInfo.dwSize = 1;
	curInfo.bVisible = 0;
	SetConsoleCursorInfo(hOut, &curInfo);
	SetConsoleTitle("推箱子(第二关)(照样支持电脑)");
}

void Set() {
	HANDLE hOut;
	COORD pos = {0, 0};
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

void Welcome() {
	printf("\n\n                欢迎游玩:");
	printf("\n\n            推 箱 子 (第 二 关)");
	printf("\n\n              请按任意键开始");
	getch();
	system("cls");
}

int main() {
	Prepare();
	Welcome();
	int x = 3, y = 2;
	char ch;
	int cnt = 0;
	for (int i = 0; i <= 6; i++)
		puts(a[i]);
	while (1) {
		ch = getch();
		if (ch == -32) {
			ch = getch();
			if (ch == 80) {
				if (a[x + 1][y] == ' ') {
					a[x][y] = ' ';
					x++;
					a[x][y] = 'S';
				} else if (a[x + 1][y] == 'O') {
					if (a[x + 2][y] == ' ') {
						a[x][y] = ' ';
						x++;
						a[x][y] = 'S';
						a[x + 1][y] = 'O';
					} else if (a[x + 2][y] == '*') {
						a[x][y] = ' ';
						x++;
						a[x][y] = 'S';
						a[x + 1][y] = ' ';
						cnt++;
					}
				}
			} else if (ch == 72) {
				if (a[x - 1][y] == ' ') {
					a[x][y] = ' ';
					x--;
					a[x][y] = 'S';
				} else if (a[x - 1][y] == 'O') {
					if (a[x - 2][y] == ' ') {
						a[x][y] = ' ';
						x--;
						a[x][y] = 'S';
						a[x - 1][y] = 'O';
					} else if (a[x - 2][y] == '*') {
						a[x][y] = ' ';
						x--;
						a[x][y] = 'S';
						a[x - 1][y] = ' ';
						cnt++;
					}
				}
			} else if (ch == 75) {
				if (a[x][y - 1] == ' ') {
					a[x][y] = ' ';
					y--;
					a[x][y] = 'S';
				} else if (a[x][y - 1] == 'O') {
					if (a[x][y - 2] == ' ') {
						a[x][y] = ' ';
						y--;
						a[x][y] = 'S';
						a[x][y - 1] = 'O';
					} else if (a[x][y - 2] == '*') {
						a[x][y] = ' ';
						y--;
						a[x][y] = 'S';
						a[x][y - 1] = ' ';
						cnt++;
					}
				}
			} else if (ch == 77) {
				if (a[x][y + 1] == ' ') {
					a[x][y] = ' ';
					y++;
					a[x][y] = 'S';
				} else if (a[x][y + 1] == 'O') {
					if (a[x][y + 2] == ' ') {
						a[x][y] = ' ';
						y++;
						a[x][y] = 'S';
						a[x][y + 1] = 'O';
					} else if (a[x][y + 2] == '*') {
						a[x][y] = ' ';
						y++;
						a[x][y] = 'S';
						a[x][y + 1] = ' ';
						cnt++;
					}
				}
			}
		}
		Set();
		for (int i = 0; i <= 6; i++)
			puts(a[i]);
		if (cnt == 5)
			break;
	}
	printf("恭喜通过了第二关!请开启下个程序,进入第三关!!!");
	Sleep(1000);
	return 0;
}


推箱子后续

如果你喜欢的话,记得一键三连哦!求求了,这对我很重要!

  • 17
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值