游戏代码:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 10;
int i=0;
int MG(),AC();
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 jiaocheng()
{
read("你想看教程?\n",100);
read("OK\n",100);
cout<<"#是地雷,踩上去会死,.是安全区域,可以通过,E是终点,踩上去就赢了,P是你,S是起点\n";
cout<<"W是向上移动,A是向左移动,D是向右移动,S是向下移动\n";
cout<<"你学会了吗?";
cout<<endl;
cout<<"按一然后按回车开始\n";
int yi;
cin>>yi;
if(yi==1)
MG();
else
jiaocheng();
}
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
maze[i][j] = (rand() % 4 == 0) ? '#' : '.';
}
}
maze[1][1] = 'S';
maze[HEIGHT - 2][WIDTH - 2] = 'E';
}
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");
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;
}
int MG()
{
system("cls");
read("第",100);
cout<<i;
read("关",100);
dt(maze);
cout<<endl;
srand(time(0));
clock_t start_time = clock();
bool game_over_flag = false;
while (!game_over_flag)
{
print_maze();
cout << "请输入移动指令 (WASD): ";
char move;
cin >> move;
clock_t current_time = clock();
if ((current_time - start_time) / (double)CLOCKS_PER_SEC > 35)
{
game_over();
game_over_flag = true;
return 0;
}
switch (move)
{
case 'W':
if (is_valid_move(player.first - 1, player.second))
player.first--;
else
{
ys(4);
cout << "你踩到了地雷,你死了" << endl;
return 0;
}
break;
case 'A':
if (is_valid_move(player.first, player.second - 1))
player.second--;
else
{
ys(4);
cout << "你踩到了地雷,你死了" << endl;
return 0;
}
break;
case 'S':
if (is_valid_move(player.first + 1, player.second))
player.first++;
else
{
ys(4);
cout << "你踩到了地雷,你死了" << endl;
return 0;
}
break;
case 'D':
if (is_valid_move(player.first, player.second + 1))
player.second++;
else
{
ys(4);
cout << "你踩到了地雷,你死了" << endl;
return 0;
}
break;
default:
cout << "无效的输入,请使用WASD进行移动!" << endl;
break;
}
if (maze[player.first][player.second] == 'E')
{
system("cls");
cout<<"\033[1;2m恭喜你,你走出了暗影迷宫!!\033[0m\n";
game_over_flag = true;
}
}
i++;
dt(maze);
print_maze();
MG();
return 0;
}
int bg()
{
cout<<"你需要什么背景?\n";
cout<<"1.蓝色,2.红色,3.绿色,4.不变\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==4)
MG();
}
int AC()
{
read("欢迎来到暗影迷宫!\n",100);
read("是否开始?\n",100);
read("1开始,2退出,3查看教程,4换背景",100);
cout<<endl;
int ks;
cin>>ks;
if(ks==1)
{
for(int i=1;i<=100;i++)
{
cout<<i<<"%";
Sleep(150);
system("cls");
}
MG();
}
else if(ks==2)
return 1;
else if(ks==3)
jiaocheng();
else if(ks==4)
bg();
else
{
read("那算开始哦!\n",100);
for(int i=1;i<=100;i++)
{
cout<<i<<"%";
Sleep(150);
system("cls");
}
MG();
}
}
int main()
{
AC();
return 0;
}