跳到正文

个人记录

【已完成】【新品上市】阴影迷宫

#SUSPENDED#约 13 分钟
原文
文章信息与版本

文章信息

发布
2024年4月25日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 13 分钟
获赞
4

版本

  1. v1当前

更新了豪华版本,感谢@BGM114514

  • 如过发现地图无解,请输入u重新生成地图
  • 新增查看成就功能
  • 撞倒地雷了不会return 0;会返回主页面,但是会统计你一共通过的关卡数
  • 超时了会直接死
  • 增加了存档功能
  • 增加了显示时间功能

@2345A搭的框架,@BGM114514改良编“正经”游戏!
耗时一周,终于编好了!发现bug一定要说啊!

公开一下代码,大家可以学习!

不断更新中。

C++
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 10;
int i = 0;
int u = 0;
int yy;
clock_t current_time, start_time;
int MG(), AC();
void jc();
string maze[HEIGHT] = {
	"####################",
	"#..S...#......#.#",
	"#.##..#..#..#.##.",
	"#.#...#...#...#.#.",
	"#.#.E.#...#..#.#.",
	"#.#...#...#...#.#.",
	"#.#..#..#..#.##.",
	"#.#...#...#...#.#.",
	"#..S...#......#.#",
	"####################"
};

void read(string a, int b) {
	for (int i = 0; i < a.size(); i++) {
		cout << a[i];
		Sleep(b);
	}
}
void save_game(const string& filename) {
	ofstream outfile(filename.c_str());
	if (!outfile) {
		cerr << "无法打开存档文件。" << endl;
		return;
	}
	outfile << i << endl;
	outfile.close();
	cout << "游戏已保存。" << endl;
}
void load_game(const string& filename) {
	ifstream infile(filename.c_str());
	if (!infile) {
		cerr << "无法打开存档文件。" << endl;
		return;
	}
	infile >> i;
	infile.close();
	cout << "游戏已加载。" << endl;
	jc();
}
void jiaocheng() {
	read("你想看教程?\n", 100);
	read("OK\n", 100);
	cout << "#是地雷,踩上去会死,.是安全区域,可以通过,E是终点,踩上去就赢了,P是你,S是起点\n";
	cout << "W是向上移动,A是向左移动,D是向右移动,S是向下移动\n";
	cout << "由于有bug,如果你发现迷宫无解,请输入u,系统会重新生成迷宫\n";
	cout << "你学会了吗?";
	cout << endl;
	cout << "按一然后按回车开始\n";
	int yi;
	cin >> yi;
	if (yi == 1)
		MG();
	else
		jiaocheng();
}
void jc() {
	read("你一共通过了", 100);
	cout << i;
	read("关", 100);
	Sleep(500);
	system("cls");
	AC();
}
void ys(int a) {
	if (a == 0)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	if (a == 1)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
	if (a == 2)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
	if (a == 3)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
	if (a == 4)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
	if (a == 5)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
	if (a == 7)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
	if (a == 8)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_RED);
}
void dt(string maze[HEIGHT]) {
	srand(time(0));
	for (int i = 0; i < HEIGHT; ++i) {
		for (int j = 0; j < WIDTH; ++j) {
			if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1)
				maze[i][j] = '#';
			else {
				if (i == 1 && j == 1)
					maze[i][j] = 'S';
				else if (i == HEIGHT - 2 && j == WIDTH - 2)
					maze[i][j] = 'E';
				else
					maze[i][j] = (rand() % 4 == 0) ? '#' : '.';
			}
		}
	}
	if (maze[1][1] == '#' || maze[HEIGHT - 2][WIDTH - 2] == '#') {
		dt(maze);
	}
}
pair<int, int> player = make_pair(1, 1);
bool is_valid_move(int x, int y) {
	if (x < 0 || x >= HEIGHT || y < 0 || y >= WIDTH)
		return false;
	if (maze[x][y] == '#')
		return false;

	return true;
}
void print_maze() {
	system("cls");

	cout << "剩余时间:" << 35 - (int)((current_time - start_time) / (double)CLOCKS_PER_SEC) << endl;
	for (int i = 0; i < HEIGHT; ++i) {
		for (int j = 0; j < WIDTH; ++j) {
			if (make_pair(i, j) == player)
				cout << "P";
			else
				cout << maze[i][j];
		}
		cout << endl;
	}
}
void game_over() {
	cout << "很遗憾,你未能在规定时间内找到出口。你死了" << endl;
	return;
}
void fh() {
	Sleep(500);
	system("cls");
	AC();
}
int MG() {
	system("cls");
	read("第", 100);
	cout << i;
	read("关", 100);
	Sleep(500);
	dt(maze);
	cout << endl;
	srand(time(0));
	start_time = clock();
	bool game_over_flag = false;
	pair<int, int> new_player = make_pair(1, 1);
	while (!game_over_flag) {
		current_time = clock();
		if ((current_time - start_time) / (double)CLOCKS_PER_SEC > 35) {
			game_over();
			cout << "请按下换行:";
			getchar();
			getchar();
			game_over_flag = true;
			return 0;
		}
		print_maze();
		cout << "请输入移动指令 (WASD): ";
		char move;
		cin >> move;
		if (move == 'U'){
			player.first=1;
			player.second=1;
			MG();
		}
			

		switch (move) {
			case 'W':
				if (is_valid_move(player.first - 1, player.second))
					player.first--;
				else {
					ys(4);
					system("cls");
					cout << "你踩到了地雷,你死了" << endl;
					fh();
				}
				break;
			case 'A':
				if (is_valid_move(player.first, player.second - 1))
					player.second--;
				else {
					ys(4);
					system("cls");
					cout << "你踩到了地雷,你死了" << endl;
					fh();
				}
				break;
			case 'S':
				if (is_valid_move(player.first + 1, player.second))
					player.first++;
				else {
					ys(4);
					system("cls");
					cout << "你踩到了地雷,你死了" << endl;
					fh();
				}
				break;
			case 'D':
				if (is_valid_move(player.first, player.second + 1))
					player.second++;
				else {
					ys(4);
					system("cls");
					cout << "你踩到了地雷,你死了" << endl;
					fh();
				}
				break;
			default:
				cout << "无效的输入,请使用WASD进行移动!" << endl;
				break;
		}
		if (maze[player.first][player.second] == 'E') {
			print_maze(); 
			system("cls");
			cout << "恭喜你,你走出了暗影迷宫!!\n";
			cout << "请按下换行:";
			getchar();
			getchar();
			i++;
			player = new_player;
			system("cls");
			MG();
		}
	}
	return 0;
}
void bg() {
	cout << "你需要什么背景?\n";
	cout << "1.蓝色,2.红色,3.绿色,4.金色,5.不变\n";
	int bj;
	cin >> bj;
	if (bj == 1)
		ys(1), MG();
	else if (bj == 2)
		ys(4), MG();
	else if (bj == 3)
		ys(2), MG();
	else if (bj == 5)
		MG();
	else if(bj == 4){
		ys(5),MG();
	} 
}
int AC() {
	read("欢迎来到暗影迷宫!\n", 100);
	read("是否开始?\n", 100);
	cout << "1开始,2退出,3查看教程,4换背景,5查看成就,6存档,7读档";
	cout << endl;
	int ks;
	cin >> ks;
	if (ks == 1) {
		for (int i = 1; i <= 74; i++) {
			cout << i << "%";
			system("cls");
		}
		cout << "75%";
		Sleep(3000);
		system("cls");
		for (int i = 76; i <= 100; i++) {
			cout << i << "%";
			Sleep(30);
			system("cls");
		}
		MG();
	} else if (ks == 2)
		return 1;
	else if (ks == 3)
		jiaocheng();
	else if (ks == 4)
		bg();
	else if (ks == 5)
		jc();
	else if (ks == 6)
		save_game("savegame.txt");
	else if (ks == 7)
		load_game("savegame.txt");
	else {
		read("那算开始哦!\n", 100);
		for (int i = 1; i <= 100; i++) {
			cout << i << "%";
			Sleep(150);
			system("cls");
		}
		MG();
	}
}
int main() {
	AC();
	cout << "游戏结束!\n";
	return 0;
}
原文
存档评论10 条抓取于