C++迷宫游戏(附源码)

这是一个使用C++编写的迷宫游戏,通过控制台显示图形界面。玩家可以控制角色在不同关卡中移动,遇到各种障碍如墙、门、钥匙、护盾等,击败小怪或避开它们以达到终点。游戏包含多个关卡,每个关卡有不同的地图和挑战。
摘要由CSDN通过智能技术生成

话不多说,源码奉上!!

#include<bits/stdc++.h> 
#include<conio.h>
#include<windows.h>
using namespace std;
void Color(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 Setpos(int x,int y){COORD pos;pos.X=y*2,pos.Y=x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}
int R[5];
void SlowDisplay(int x,char *p){while(1){if(*p!=0) printf("%c",*p++);else break;Sleep(x);}}
struct xg{int xgx,xgy;bool fx,sw=0; }xg[21];
int ys=0,hd=0,sj=0,sl=0,mg=0,hp=0,hss=0,hsx=0,jt=0,s=0;
char ch[35][35];
char ch1[35][35]={"", "      #####     #####  ########"," #&##       ###       ###      ","      #### #    ## ####   #### ","  ### ##   # ####!      ##     ","  ###&   ###   +##  ###### ####"," ######&############!        ##"," #  *#      !  ######### ###-##"," # ### #####             #     "," # ###*#   ############### ### "," # ###&###                 ####"," #      #  # ##-############## "," ######   #####!               "," #+     #  ###### ############ ", " ##&########## ### ### ### ##  "," #                   !      ! #","   ############################","  #       ! #       ! #       #","    #     !   #     !   ####   "," #&########################### "," )                             ",}; 
char ch2[35][35]={""," ##############################"," #*                           #"," ########&###################-#"," #                          # #"," # # ################&#####-# #"," # # #               !   +# # #"," # # # ##&############### # # #"," # # # #                # # # #"," # # # # ###########&## # # # #"," # # # # #)- !        # # # & #"," # # # # ###      !   # # # # #", " # # # ##########&####### # # #"," # # #              !       # #"," # # ##################&##### #"," # #       !               *# #"," # #####&####### ############ #"," #     !       # #   !        #"," # ############# ###########  #"," #              !             #"," ##############################", };
char ch3[31][33]={""," ##############################"," #.          +               .#"," #                      !     #"," #      +     !               #"," #                            #"," #          ! +               #"," #     !               +    ! #"," #                  +         #"," #           +          !     #", " #  +                      !  #"," #      !           +         #"," #+           _____           #"," #           |_ _ _|     +    #"," #     +     |_|||_|          #"," #        +  |MMMMM|         !#"," #           |WWWWW|          #"," #!          |_____|          #"," #                      !     #"," #.        +                 .#"," ##############################", }; 
char ch4[35][35]={"","         ###*###       ########","     ###!      # ##### #     *#"," ###&#+####### # #   # # ######"," #          !# # #-# # # #    #"," ###&#######     # #   # # ## #","           ####### ##### # ## #","  ####&###        !  !  !# ## #","  #       ################ ## #","    ###             !!     ##  "," #### ##&##################### "," #                !         !# "," #-############&######### #&## "," #-#ooooooooooo   o     # #*## "," o ooo   o   o  o o ooo # #### "," o   o o   o   oo o   o #      "," ooo o ooooooo oo ooo o #######"," ooo o o   o   oo     o       #"," ooo   o o o*oooooooooooooooo-#"," ooooooo o oo   o   o   o   o #"," )       o    o   o   o   o   #",};
char ch5[35][35]={"","  oooo     oooooooooooooooooooo","       ooo    o   oo+oo   o   o"," ooooooo*oooo !   o   ! o   o o"," o          o  o    o   o   o o"," oooooooooo ooooooooooooooooo o"," o     !       !              o"," oooooooo ooooooooooooooooooooo"," oooooooo ooooooooooooooooooooo", " o              !             o"," o^^^^^^^^^^^^^^^^^^^^^^^^^^^^o"," o    !   !   !   !   !       o"," o^^^^^^^^^^^^^^^^^^^^^^^^^^^^o"," o^*^! !^! !^!  ^^! +^! !^! *^o"," o^^^^^^^^^^^^^^^^^^^^^^^^^^^^o"," o   !   !   !   !   !        o"," #^^^^^^^^^^^^^^^^^^^^^^^^^^^^#"," #                 !          #"," ####oooooooooo--oooooooooooo##"," ########oooooo--ooooooo#######"," ###########ooo))oooo##########", };
char ch6[35][35]={""," oooooooooooooo .oooooooooooooo"," o+oooooooooooo  oooooooooooo+o"," ooooooooooooo#  #ooooooooooooo"," oooooooooooo#^^^^#oooooooooooo"," oooo*oooooo#^    ^#oooooo*oooo"," oooooooooo# ^    ^ #oooooooooo"," ooooooooo#  ^    ^  #ooooooooo"," oooooooo#^^^^^^^^^^^^#oooooooo"," ooooooo#^   ^#--#^   ^#ooooooo"," .       ^   ^-NO-^   ^         ","         ^   ^-IP-^   ^       ."," ooooooo#^   ^#--#^   ^#ooooooo"," oooooooo#^^^^^^^^^^^^#oooooooo"," ooooooooo#  ^    ^  #ooooooooo"," oooooooooo# ^    ^ #oooooooooo"," oooo*oooooo#^    ^#oooooo*oooo"," oooooooooooo#^^^^#oooooooooooo"," ooooooooooooo#  #ooooooooooooo"," o+oooooooooooo  oooooooooooo+o"," oooooooooooooo. oooooooooooooo",};
void map1(int x,int y,int k){
    for(int i=1;i<=20;++i)
    for(int j=1;j<=30;++j){
        bool f=1;
        for (int l=1;l<=k;++l)
        if (xg[l].xgx==i&&xg[l].xgy==j&&xg[l].sw==0) {Setpos(i,j);Color(4),cout<<"▼";f=0;break;}
        if (f==1)
        {Setpos(i,j);
        if (i==x&&j==y) Color(1),cout<<"●";
        else if (ch[i][j]=='#'||ch[i][j]=='&') Color(7),cout<<"■";
        else if (ch[i][j]==')') Color(5),cout<<"☆";
        else if (ch[i][j]=='*') Color(5),cout<<"♂";
        else if (ch[i][j]=='-') Color(3),cout<<"▋"; 
        else if (ch[i][j]=='+') Color(2),cout<<"◆";
        else if (ch[i][j]=='.') Color(1),cout<<"◆";
        else if (ch[i][j]=='o') Color(8),cout<<"~ "; 
        else if (ch[i][j]=='_') Color(3),cout<<"▁";
        else if (ch[i][j]=='^') Color(3),cout<<"▲";
        else if (ch[i][j]=='|') Color(3),cout<<"|";
        else if (ch[i][j]=='M') Color(3),cout<<'M'<<'M';
        else if (ch[i][j]=='W') Color(3),cout<<'W'<<'W';
        else if (ch[i][j]=='N') Color(2),cout<<"NO";
        else if (ch[i][j]=='I') Color(2),cout<<"..";
        else if (ch[i][j]=='O') Color(2),cout<<"I!";
        else if (ch[i][j]=='P') Color(2),cout<<".P";
        else Color(0),cout<<"  ";
        }
    }
    Setpos(21,3);Color(5),cout<<"["<<ys<<" ♂]      ",Color(2),cout<<"["<<hd<<" ◆]";
}
int yx(int a)
{
    int k=0,T=0,x,y,boss=0,win=0;ys=0,hd=0,sj=0;
    system("cls");
if(a==1) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch1[i][j];x=1,y=1;}
if(a==2) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch2[i][j];x=2,y=29;}
if(a==3) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch3[i][j];x=10,y=15;boss=1;}
if(a==4) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch4[i][j];x=1,y=1;}
if(a==5) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch5[i][j];x=1,y=1;}
if(a==6) {for(int i=0;i<35;i++)for(int j=0;j<35;j++) ch[i][j]=ch6[i][j];x=1,y=15;boss=2;}
    if(boss==1){for (int i=1;i<=200;++i) {int xx=rand()%21,yy=rand()%31;if ((xx!=x||yy!=y)&&ch[xx][yy]!='_'&&ch[xx][yy]!='W'&&ch[xx][yy]!='|'&&ch[xx][yy]!='M'&&ch[xx][yy]!='.') ch[xx][yy]='o';}map1(x,y,k);} 
    for(int i=1;i<35;++i)for(int j=1;j<35;++j) if (ch[i][j]=='!')k++,xg[k].xgx=i,xg[k].xgy=j,ch[i][j]=' ';
    map1(x,y,k);

while (1)
{
    T++;
        Setpos(x,y),cout<<"  ";
        if(GetAsyncKeyState(VK_UP)&0x8000&&x!=1&&ch[x-1][y]!='#') x--;
        else if(GetAsyncKeyState(VK_DOWN)&0x8000&&x!=20&&ch[x+1][y]!='#') x++;
        else if(GetAsyncKeyState(VK_LEFT)&0x8000&&y!=1&&ch[x][y-1]!='#') y--;
        else if(GetAsyncKeyState(VK_RIGHT)&0x8000&&y!=30&&ch[x][y+1]!='#') y++;
        Setpos(x,y),Color(1),cout<<"●";

    if(boss!=0&&T%20==5){
        for (int i=1;i<=20;++i)for (int j=1;j<=30;++j)if (ch[i][j]=='o') ch[i][j]=' ';
        for (int i=1;i<=200*boss;++i) {int xx=rand()%21,yy=rand()%31;if ((xx!=x||yy!=y)&&ch[xx][yy]==' ') ch[xx][yy]='o';}
        map1(x,y,k);} 

    if(T%20==0) map1(x,y,k);if(T%20==10){for(int i=1;i<35;++i)for(int j=1;j<35;++j) if(ch[i][j]=='^')Setpos(i,j),cout<<"  ";}
    if(T%4==0&&k!=0)
        for (int i=1;i<=k;i++){
            if (xg[i].fx==0&&(ch[xg[i].xgx][xg[i].xgy+1]==' '||(ch[xg[i].xgx][xg[i].xgy-1]=='^'&&T%20>=10))&&xg[i].xgy!=30)     Setpos(xg[i].xgx,xg[i].xgy),cout<<"  ",xg[i].xgy++,Setpos(xg[i].xgx,xg[i].xgy),Color(4),cout<<"▼";
            else if (xg[i].fx==1&&(ch[xg[i].xgx][xg[i].xgy-1]==' '||(ch[xg[i].xgx][xg[i].xgy-1]=='^'&&T%20>=10))&&xg[i].xgy!=1) Setpos(xg[i].xgx,xg[i].xgy),cout<<"  ",xg[i].xgy--,Setpos(xg[i].xgx,xg[i].xgy),Color(4),cout<<"▼";
            else if (xg[i].fx==1) xg[i].fx=0;else xg[i].fx=1;}
    for (int i=1;i<=k;i++){
        if (xg[i].xgx==x&&xg[i].xgy==y&&xg[i].sw==0&&hd==0) {system("cls");cout<<"怪物啊!!!";Sleep(2000);system("cls");return 0;}
        else if (xg[i].xgx==x&&xg[i].xgy==y&&hd>0&&xg[i].sw==0) {system("cls");cout<<"护盾保护了你!";Sleep(1000);system("cls");map1(x,y,k);hd--;xg[i].sw=1;}}

    if (ch[x][y]==')') {system("cls");cout<<"You Win!";Sleep(2000);system("cls");win=1;}
    if (ch[x][y]=='*') {system("cls");cout<<"你找到了钥匙!";ys++;Sleep(1000);system("cls");map1(x,y,k);ch[x][y]=' ';} 
    if (ch[x][y]=='.') {system("cls");cout<<"你破坏了水晶!";sj++;Sleep(1000);system("cls");map1(x,y,k);ch[x][y]=' ';}
    if (ch[x][y]=='-'&&ys>0) {system("cls");cout<<"门开了!";ys--;Sleep(1000);system("cls");map1(x,y,k);ch[x][y]=' ';}else if (ch[x][y]=='-') {system("cls");cout<<"你没有钥匙!";--x;Sleep(3000);system("cls");map1(x,y,k);}
    if (ch[x][y]=='+') {system("cls");cout<<"你找到了护盾!";hd++;Sleep(1000);system("cls");map1(x,y,k);ch[x][y]=' ';}
    if (ch[x][y]=='o'&&hd==0) {system("cls");cout<<"烫死啦!!!";Sleep(2000);return 0;} 
    else if (hd>0&&ch[x][y]=='o') {system("cls");cout<<"护盾保护了你!";ch[x][y]=' ';hd--;Sleep(1000);system("cls");map1(x,y,k);} 
    if (ch[x][y]=='^'&&T%20<10&&hd==0) {system("cls");cout<<"被刺穿啦!!!";Sleep(2000);return 0;} 
    else if (hd>0&&ch[x][y]=='^'&&T%20<10) {system("cls");cout<<"护盾保护了你!";ch[x][y]=' ';hd--;Sleep(1000);system("cls");map1(x,y,k);} 

    if ((a==3||a==6)&&sj==4)   {system("cls");cout<<"You Win!";Sleep(2000);system("cls");win=1;}

    for (int i=1;i<=k;i++)
        if (xg[i].xgx==x&&xg[i].xgy==y&&xg[i].sw==0&&hd==0) {system("cls");cout<<"怪物啊!!!";Sleep(1000);system("cls");return 0;}
        else if (xg[i].xgx==x&&xg[i].xgy==y&&hd>0&&xg[i].sw==0) {system("cls");cout<<"护盾保护了你!";Sleep(1000);system("cls");map1(x,y,k);map1(x,y,k);hd--;xg[i].sw=1;}
    if(win==1){if(a==1)sl=1;if(a==2)mg=1;if(a==3)hp=1;if(a==4)hss=1;if(a==5)hsx=1;return 0;}
Sleep(100);
}}
void ml(){while(1){
    system("cls");Color(0); 
    cout<<"1、迷雾森林   ";cout<<endl;
    if (sl==0) cout<<"2、 ? ? ? ?   ";else cout<<"2、巨石迷宫(图中如果走可穿透墙,能避开所有的怪)   "; cout<<endl;
    if (mg==0) cout<<"3、 ? ? ? ?   ";else cout<<"3、熔岩喷泉(BOSS,图中随机出现熔岩)   ";cout<<endl;
    if (hp==0) cout<<"4、 ? ? ? ?   ";else cout<<"4、火山口(上)   ";cout<<endl;
    if (hss==0) cout<<"5、 ? ? ? ?   ";else cout<<"5、火山口(下)   ";cout<<endl;
    if (hsx==0) cout<<"6、 ? ? ? ?   ";else cout<<"6、火山祭坛(BOSS)";cout<<endl;
    cout<<"选择关卡(序号)"<<endl;cout<<"退出输入0"<<endl;
    int a;cin>>a;
    if (a==1) {yx(1);}if (a==2&&sl==1){yx(2);}if (a==3&&mg==1) {yx(3);}if (a==4&&hp==1) {yx(4);}if (a==5&&hss==1){yx(5);}if (a==6&&hsx==1){yx(6);}if (a==0) return;
}}
void gz() {
    system("cls");
    cout<<"由上下左右键控制"<<endl;
    cout<<"●是你"<<endl;
    cout<<"■是墙"<<endl;
    cout<<"☆是终点"<<endl;
    cout<<"▼是小怪(只会左右移动)"<<endl;
    cout<<"▋是门"<<endl;
    cout<<"♂是钥匙"<<endl;
    cout<<"◆是护盾(不可杀怪)"<<endl;
    cout<<"■是可穿透墙(每一关几乎都有)"<<endl; 
    cout<<"◆是水晶(BOSS关破坏所有水晶通关)"<<endl;
    cout<<"~ 是熔岩"<<endl; 
    cout<<"▁是尖刺(未升起)"<<endl;
    cout<<"▲是尖刺(升起)"<<endl;
    cout<<"输入1开始!"<<endl;
    int a;
    cin>>a;
    if (a==1) ml();
    else gz(); 
}
int main(){ 
    CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);srand((unsigned)time(NULL));
    cout<<"欢迎来到超级迷宫!"<<endl;cout<<"1、开始!"<<endl;cout<<"2、规则..."<<endl;
    int a;cin>>a;if (a==1){system("cls");ml();}else if (a==2){gz();}
    return 0;
}

按←↑→↓ 方向键控制●移动(注:每一关几乎都有穿透墙),走到☆即胜利。BOSS关破坏所有水晶通关。

下面是代码 #include #include #include #include #define Height 25 //迷宫的高度,必须为奇数 #define Width 25 //迷宫的宽度,必须为奇数 #define Wall 1 #define Road 0 #define Start 2 #define End 3 #define Esc 5 #define Up 1 #define Down 2 #define Left 3 #define Right 4 int map[Height+2][Width+2]; void gotoxy(int x,int y) //移动坐标 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); } void hidden()//隐藏光标 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut,&cci;); cci.bVisible=0;//赋1为显示,赋0为隐藏 SetConsoleCursorInfo(hOut,&cci;); } void create(int x,int y) //随机生成迷宫 { int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向 int i,j,t; //将方向打乱 for(i=0;i<4;i++) { j=rand()%4; t=c[i][0];c[i][0]=c[j][0];c[j][0]=t; t=c[i][1];c[i][1]=c[j][1];c[j][1]=t; } map[x][y]=Road; for(i=0;i<4;i++) if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) { map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]); } } int get_key() //接收按键 { char c; while(c=getch()) { if(c==27) return Esc; //Esc if(c!=-32)continue; c=getch(); if(c==72) return Up; //上 if(c==80) return Down; //下 if(c==75) return Left; //左 if(c==77) return Right; //右 } return 0; } void paint(int x,int y) //画迷宫 { gotoxy(2*y-2,x-1); switch(map[x][y]) { case Start: printf("入");break; //画入口 case End: printf("出");break; //画出口 case Wall: printf("▇");break; //画墙 case Road: printf(" ");break; //画路 } } void game() { int x=2,y=1; //玩家当前位置,刚开始在入口处 int c; //用来接收按键 while(1) { gotoxy(2*y-2,x-1); printf("●"); //画出玩家当前位置 if(map[x][y]==End) //判断是否到达出口 { gotoxy(30,24); printf("到达终点,按任意键结束"); getch(); break; } c=get_key(); if(c==Esc) { gotoxy(0,24); break; } switch(c) { case Up: //向上走 if(map[x-1][y]!=Wall) { paint(x,y); x--; } break; case Down: //向下走 if(map[x+1][y]!=Wall) { paint(x,y); x++; } break; case Left: //向左走 if(map[x][y-1]!=Wall) { paint(x,y); y--; } break; case Right: //向右走 if(map[x][y+1]!=Wall) { paint(x,y); y++; } break; } } } int main() { system("title yourname"); int i,j; srand((unsigned)time(NULL)); //初始化随即种子 hidden(); //隐藏光标 for(i=0;i<=Height+1;i++) for(j=0;j<=Width+1;j++) if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫 map[i][j]=Road; else map[i][j]=Wall; create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数 for(i=0;i<=Height+1;i++) //边界处理 { map[i][0]=Wall; map[i][Width+1]=Wall; } for(j=0;j<=Width+1;j++) //边界处理 { map[0][j]=Wall; map[Height+1][j]=Wall; } map[2][1]=Start; //给定入口 map[Height-1][Width]=End; //给定出口 for(i=1;i<=Height;i++) for(j=1;j<=Width;j++) //画出迷宫 paint(i,j); game(); //开始游戏 getch(); return 0; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值