- 臧启航 的博客
走迷宫程序(2.0)(持续更新)
- @ 2024-9-8 19:26:22
更新
2025年3月8日
添加了简介:在游戏中途(技术原因)t键打开
#include <algorithm>
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <queue>
using namespace std;
constexpr int N=10005;
char map[N][N];
bool vis[N][N];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
string NAME,CCC;
int sx,sy,ex,ey,px,py,mx,my,md,n,score,cnt_C,sc_C,health,mh,mb;
void Slowsay(string a) {
int l=a.size();
for(int i=0;i<l;i++) {
cout<<a[i];
Sleep(20);
}
return;
}
bool bfs(int n,int sx,int sy,int ex,int ey) {
memset(vis,0,sizeof(vis));
queue<pair<int,int>> que;
que.push({sx,sy});
bool flag=false;
while (!que.empty()) {
pair<int,int> a=que.front();
que.pop();
if (a.first==ex and a.second==ey) {
flag=true;
}
for (int k=0;k<4;k++) {
int nx=a.first+dx[k];
int ny=a.second+dy[k];
if (nx<0 or nx>=n or ny<0 or ny>=n) {
continue;
}
if (vis[nx][ny] or map[nx][ny]=='#') {
continue;
}
vis[nx][ny]=true;
que.push({nx,ny});
}
}
if (flag==false) {
return false;
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (map[i][j]=='.' and vis[i][j]!=true) {
return false;
}
}
}
return true;
}
void draw() {
string s="",s1="";
for (int i=1;i<=n*2-1;i++) {
s+="═";
}
for (int i=0;i<n;i++) {
s1+="║";
for (int j=0;j<n;j++) {
if (i==px and j==py) {
s1+="@";
} else if (i==mx and j==my) {
if (mh) {
s1+="\033[34mM\033[0m";
} else {
s1+="M";
}
} else if (map[i][j]=='C') {
s1+="\033[33mC\033[0m";
} else if (map[i][j]=='B') {
s1+="\033[31mB\033[0m";
} else if (map[i][j]=='S') {
s1+="\033[32mS\033[0m";
} else if (map[i][j]=='E') {
if (sc_C<cnt_C) {
s1+="E";
} else {
s1+="\033[32mE\033[0m";
}
} else if (map[i][j]=='Z') {
s1+="\033[34mZ\033[0m";
} else if (map[i][j]=='!') {
s1+="\033[31m!\033[0m";
} else {
s1+=map[i][j];
}
if (j!=n-1) {
s1+=" ";
}
}
s1+="║\n";
}
system("cls");
cout<<"用户:"<<NAME<<"\n";
cout<<"╔"<<s<<"╗\n";
cout<<s1;
cout<<"╚"<<s<<"╝\n";
printf("分数:\033[33m%4d\033[0m ",score);
printf("魔王血量:\033[34m");
for (int i=1;i<=mh;i++) {
printf("◆");
}
printf("\033[0m");
for (int i=mh+1;i<=6;i++) {
printf("◇");
}
printf("\n收集进度:");
double CS=(double)sc_C/cnt_C*100;
if (CS<=20) {
printf("\033[31m%2.2lf%%\033[0m",CS);
} else if (CS>20 and CS<=60) {
printf("\033[33m%2.2lf%%\033[0m",CS);
} else if (CS>60) {
printf("\033[32m%2.2lf%%\033[0m",CS);
}
if (CS<10) {
printf(" ");
} else if (CS>=10 and CS<=99) {
printf(" ");
}
printf(" 已有魔王血:\033[34m%4d\033[0m\n",mb);
printf("血量:\033[31m");
int hth=min(10,health);
for (int i=1;i<=hth;i++) {
printf("◆");
}
printf("\033[0m");
for (int i=health+1;i<=10;i++) {
printf("◇");
}
if (health>10) {
printf("\033[33m");
for (int i=11;i<=health;i++) {
printf("◆");
}
printf("\033[0m");
}
printf("\n");
return;
}
void bomb(int x,int y,int hls) {
map[x][y]='O';
for (int i=max(0,x-2);i<=min(x+2,n-1);i++) {
for (int j=max(0,y-2);j<=min(y+2,n-1);j++) {
if (i==mx and j==my and mh and mh==hls) {
mh--;
if (mh==0) {
for (int k=max(0,i-4);k<=min(i+4,n-1);k++) {
for (int l=max(0,j-4);l<=min(j+4,n-1);l++) {
if (sqrt((k-mx)*(k-mx)+(l-my)*(l-my))<4) {
map[k][l]='Z';
}
}
}
}
}
if (map[i][j]=='B') {
bomb(i,j,hls);
}
if (map[i][j]!='C' and map[i][j]!='Z' and map[i][j]!=' ' and map[i][j]!='!') {
map[i][j]='-';
}
}
}
map[x][y]='O';
return;
}
void change() {
system("cls");
string ps="";
srand(time(NULL));
for (int i=0;i<4;i++) {
ps.push_back(rand()%26+'a');
}
printf("验证码:");
cout<<ps;
Sleep(500);
system("cls");
printf("密码:");
string password;
cin>>password;
if (password==ps) {
system("cls");
printf("查询密码1 修改血量2 修改魔王血量3 修改金币数量4 修改魔王血数量5\n");
int n;
cin>>n;
if (n==1) {
printf("名字:");
string nnnnnnn;
cin>>nnnnnnn;
ifstream fin(nnnnnnn+".txt");
string ppp;
fin>>nnnnnnn>>ppp;
cout<<ppp<<"\n";
Sleep(5000);
} else if (n==2) {
printf("血量:");
int hhhhhhh;
cin>>hhhhhhh;
health=hhhhhhh;
} else if (n==3) {
printf("魔王血量:");
int hhhhhhh;
cin>>hhhhhhh;
mh=hhhhhhh;
} else if (n==4) {
printf("金币数量:");
int ccccccc;
cin>>ccccccc;
score=ccccccc;
} else if (n==5) {
printf("魔王血数量:");
int bbbbbbb;
cin>>bbbbbbb;
mb=bbbbbbb;
}
system("cls");
draw();
} else {
printf("密码错误!");
Sleep(1000);
system("cls");
draw();
}
return;
}
void introduce() {
system("cls");
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ 图标简介 ║\n");
printf("║ ║\n");
printf("║ @ 你自己 ║\n");
printf("║ S/E 起点/终点(绿色开启) ║\n");
printf("║ # 墙,不能走 ║\n");
printf("║ U/D/L/R 走上去时分别向上下左右移动 ║\n");
printf("║ B 炸弹,走上去扣血,可用来炸魔王 ║\n");
printf("║ C 金币,可收集来开启终点 ║\n");
printf("║ M 魔王,玩家靠近时会放炸弹 ║\n");
printf("║ Z 魔王血,可收集,收集会加血,魔王死后出现 ║\n");
printf("║ ! 武器,魔王走上去会死 ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ 按键简介 ║\n");
printf("║ ║\n");
printf("║ t 简介,也就现在你看到的这个 ║\n");
printf("║ WASD 移动(上下左右键也可以) ║\n");
printf("║ 空格 暂停/回到游戏 ║\n");
printf("║ B 商店 ║\n");
printf("║ P 测试功能,直接通关 ║\n");
printf("║ 0 修改器,要密码 ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("╚════════════════════════════════════════════════════════════════╝\n");
printf("按任意键退出...\n");
while (!_kbhit());
_getch();
return;
}
int input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
case 75:
return 0;
case 'w':
case 72:
return 3;
case 'd':
case 77:
return 2;
case 's':
case 80:
return 1;
case 32:
system("cls");
printf("╔═══════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ 游戏暂停 ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("╚═══════════════════════════════════════╝\n");
while (_getch()!=32);
draw();
return -1;
case 'p':
sc_C=cnt_C;
px=ex;py=ey;
return -1;
case 'b':
Shop:
system("cls");
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ 商店 ║\n");
printf("║ ║\n");
printf("║ 0) 退出商店 ║\n");
printf("║ 1) 杀死魔王 35*魔王血 ║\n");
printf("║ 2) 放置武器 5*魔王血 ║\n");
printf("║ 3) 加血 5*金币 ║\n");
printf("║ 4) 原地爆炸 10*金币 ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("╚════════════════════════════════════════════════════════════════╝\n");
printf("已有魔王血:\033[34m%4d\033[0m\n",mb);
printf("已有金币:\033[33m%4d\033[0m",score);
while (true) {
if (_kbhit()) {
switch (_getch()) {
case '0':
draw();
return -1;
case '1':
if (mb>=35 and mh) {
mb-=35;
while (mh) {
bomb(mx,my,mh);
}
}
break;
case '2':
if (mb>=5 and map[px][py]!='!') {
mb-=5;
map[px][py]='!';
}
break;
case '3':
if (score>=5 and health<18) {
score-=5;
health++;
}
break;
case '4':
if (score>=10) {
score-=10;
bomb(px,py,mh);
}
break;
}
break;
}
}
goto Shop;
case '0':
change();
return -1;
case 't':
introduce();
return -1;
}
}
return -1;
}
bool login() {
if (health<=0) {
score-=sc_C;
system("cls");
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ Game Over! ║\n");
return false;
}
if (map[px][py]=='U') {
int nx=px+dx[3];
int ny=py+dy[3];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
px=nx;
py=ny;
}
} else if (map[px][py]=='D') {
int nx=px+dx[1];
int ny=py+dy[1];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
px=nx;
py=ny;
}
} else if (map[px][py]=='L') {
int nx=px+dx[0];
int ny=py+dy[0];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
px=nx;
py=ny;
}
} else if (map[px][py]=='R') {
int nx=px+dx[2];
int ny=py+dy[2];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
px=nx;
py=ny;
}
} else if (map[px][py]=='B') {
bomb(px,py,mh);
px=sx;py=sy;
health--;
} else if (map[px][py]=='C') {
score++;
sc_C++;
map[px][py]=' ';
} else if (map[px][py]=='Z') {
score++;
sc_C++;
map[px][py]=' ';
mb++;
health++;
if (health>18) {
health=18;
}
}
if (map[mx][my]=='U') {
int nx=mx+dx[3];
int ny=my+dy[3];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
mx=nx;
my=ny;
}
} else if (map[mx][my]=='D') {
int nx=mx+dx[1];
int ny=my+dy[1];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
mx=nx;
my=ny;
}
} else if (map[mx][my]=='L') {
int nx=mx+dx[0];
int ny=my+dy[0];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
mx=nx;
my=ny;
}
} else if (map[mx][my]=='R') {
int nx=mx+dx[2];
int ny=my+dy[2];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
mx=nx;
my=ny;
}
} else if (map[mx][my]=='B') {
map[mx][my]=' ';
} else if (map[mx][my]=='!') {
mh=1;
bomb(mx,my,mh);
draw();
}
int k=input();
if (k!=-1) {
int nx=px+dx[k];
int ny=py+dy[k];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
px=nx;
py=ny;
}
}
if (mh) {
if (px==mx and py==my) {
health-=10;
px=sx;py=sy;
}
if (rand()>=25000) {
int nx=mx+dx[md];
int ny=my+dy[md];
if (nx>=0 and nx<n and ny>=0 and ny<n and map[nx][ny]!='#') {
mx=nx;
my=ny;
} else {
md=(md+1)%4;
}
if (rand()>=10000) {
md=(md+1)%4;
}
}
if (rand()>=50000 or ((sqrt((px-mx)*(px-mx)+(py-my)*(py-my))<3 and rand()>=30000)) and mh>1) {
for (int i=max(0,mx-2);i<=min(mx+2,n-1);i++) {
for (int j=max(0,my-2);j<=min(my+2,n-1);j++) {
if (rand()>=20000) {
if (map[i][j]=='!') {
continue;
}
if (map[i][j]=='C') {
cnt_C--;
}
map[i][j]='B';
}
}
}
draw();
}
for (int i=max(0,mx-1);i<=min(mx+1,n-1);i++) {
for (int j=max(0,my-1);j<=min(my+1,n-1);j++) {
if (map[i][j]=='B') {
map[i][j]=' ';
}
}
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (map[i][j]=='O') {
map[i][j]='0';
} else if (map[i][j]=='0') {
map[i][j]='|';
} else if (map[i][j]=='|') {
map[i][j]='+';
} else if (map[i][j]=='+') {
map[i][j]='-';
} else if (map[i][j]=='-') {
map[i][j]=',';
} else if (map[i][j]==',') {
map[i][j]='.';
} else if (map[i][j]=='.') {
map[i][j]=' ';
}
}
}
cnt_C=max(0,cnt_C);
sc_C=min(sc_C,cnt_C);
return true;
}
void menu() {
n=20;
health=max(10,health);
cnt_C=0;sc_C=0;
mx=0;my=n-1;md=0;mh=6;
sx=0;sy=0;ex=n-1;ey=n-1;
int cnt=0,precnt=INT_MAX;
if (rand()>=50000) {
memset(map,'C',sizeof(map));
map[sx][sy]='S';
map[ex][ey]='E';
mh=0;
} else {
do {
cnt=(cnt%75)+1;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (rand()>=10000) {
map[i][j]='.';
} else {
map[i][j]='#';
}
}
}
map[sx][sy]='S';
map[ex][ey]='E';
map[mx][my]=' ';
if (precnt!=cnt/5) {
precnt=cnt/5;
string s="═══════════════\n";
for (int i=1;i<=cnt/5;i++) {
s+="/";
}
s+="\n═══════════════\nloading...\n";
system("cls");
cout<<s;
}
} while (!bfs(n,sx,sy,ex,ey) or !bfs(n,mx,my,ex,ey));
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (map[i][j]=='S' or map[i][j]=='E') {
continue;
}
if (map[i][j]=='.' or (map[i][j]>='0' and map[i][j]<='9')) {
map[i][j]=' ';
if (rand()>=25000) {
map[i][j]='C';
cnt_C++;
}
}
if (map[i][j]!='U' and map[i][j]!='D' and map[i][j]!='L' and map[i][j]!='R' and map[i][j]!='C' and map[i][j]!='B' and map[i][j]!=' ') {
map[i][j]='#';
if (rand()>=20000) {
map[i][j]='B';
}
if (rand()>=20000) {
if (rand()>=10000) {
map[i][j]='U';
}
if (rand()>=10000) {
map[i][j]='R';
}
if (rand()>=10000) {
map[i][j]='D';
}
if (rand()>=10000) {
map[i][j]='L';
}
}
if (rand()>=35000) {
map[i][j]='!';
}
}
}
}
system("cls");
px=sx;
py=sy;
return;
}
int main() {
Slowsay("走迷宫(2.0正式版)\n");
Slowsay("作者:臧启航\n");
Slowsay("游戏途中按t键打开简介\n");
Slowsay("输入start开始游戏\n");
srand(time(NULL));
string s;
cin>>s;
if (s!="start") {
return 0;
}
system("cls");
BS:
system("cls");
Slowsay("登录1 注册2:");
int x;
cin>>x;
if (x==1) {
Begin:
system("cls");
Slowsay("输入账号:");
cin>>NAME;
Slowsay("输入密码:");
cin>>CCC;
ifstream fin(NAME+".txt");
string nmae,ccccc;
int hedhh=-1,scscsc=-1,mmbbb=-1;
while (fin>>nmae>>ccccc>>hedhh>>scscsc>>mmbbb) {
if (nmae==NAME) {
if (ccccc==CCC) {
health=hedhh;
score=scscsc;
mb=mmbbb;
} else {
Slowsay("密码错误!");
Sleep(1000);
goto Begin;
}
}
}
if (hedhh!=-1 and scscsc!=-1 and mmbbb!=-1) {
Slowsay("登陆成功!");
Sleep(1000);
} else {
Slowsay("无此用户!");
Sleep(1000);
goto Begin;
}
} else if (x==2) {
Bagin:
system("cls");
Slowsay("输入账号:");
cin>>NAME;
Slowsay("输入密码:");
cin>>CCC;
ifstream fin(NAME+".txt");
string nmae,ccccc;
int hedhh,scscsc,mmbbb;
while (fin>>nmae>>ccccc>>hedhh>>scscsc>>mmbbb) {
Slowsay("该用户已存在!");
Sleep(1000);
goto Bagin;
}
Slowsay("注册成功!");
ofstream outfile(NAME+".txt",ios::binary|ios::app|ios::in|ios::out);
outfile<<NAME<<" "<<CCC<<" "<<10<<" "<<0<<" "<<0<<"\n";
outfile.close();
Sleep(1000);
} else {
Slowsay("不要乱输!");
Sleep(1000);
goto BS;
}
Start:
system("cls");
menu();
int prex=INT_MAX,prey=INT_MAX,prec=INT_MAX,premx=INT_MAX,premy=INT_MAX;
while ((px!=ex or py!=ey) or sc_C<cnt_C) {
if (prex!=px or prey!=py or prec!=sc_C or premx!=mx or premy!=my) {
draw();
}
prex=px;
prey=py;
premx=mx;
premy=my;
prec=sc_C;
if (!login()) {
goto GameOver;
}
map[sx][sy]='S';
map[ex][ey]='E';
Sleep(100);
score=max(0,score);
}
system("cls");
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (map[i][j]=='!') {
mb+=5;
}
}
}
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ You Win! 分数:\033[33m%4d\033[0m ║\n",score);
GameOver:
printf("║ ║\n");
printf("║ 1) 继续游戏 ║\n");
printf("║ 2) 退出游戏 ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("╚════════════════════════════════════════════════════════════════╝\n");
ofstream outfile(NAME+".txt",ios::binary|ios::app|ios::in|ios::out);
outfile<<NAME<<" "<<CCC<<" "<<health<<" "<<score<<" "<<mb<<"\n";
outfile.close();
printf("---->请输入你的选择:");
char c;
while (c=getchar()) {
if (c=='1') {
system("cls");
goto Start;
} else if (c=='2') {
break;
}
}
system("cls");
printf("╔════════════════════════════════════════════════════════════════╗\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ 游戏结束 ║\n");
printf("║ ║\n");
printf("║ 最终分数:\033[33m%4d\033[0m ║\n",score);
printf("║ ║\n");
printf("║ ║\n");
printf("╚════════════════════════════════════════════════════════════════╝\n");
getchar();
getchar();
return 0;
}