迷宫游戏

· · 个人记录

1.0版 更新于2025/1/16 14:28

#include<bits/stdc++.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define int long long
#define double long double
const int N=2e6+5;
using namespace std;
int x=1,y=1;
bool f1,f2;
int m1x=1,m1y=8;
string dt1[11 ][11]= {
    {"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
    ,{"墙","  ","墙","  ","  ","墙","  ","  ","门"}
    ,{"墙","  ","墙","  ","墙","墙","  ","墙","墙"}
    ,{"墙","  ","  ","  ","  ","墙","  ","  ","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","墙","  ","墙","墙","  ","墙"}
    ,{"墙","  ","墙","墙","墙","墙","  ","  ","墙"}
    ,{"墙","  ","  ","  ","  ","  ","  ","墙","墙"}
    ,{"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
};
int gx=2,gy=4;
string dt2[25 ][25]= {
    {"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
    ,{"墙","  ","墙","  ","  ","  ","  ","  ","门"}
    ,{"墙","  ","墙","墙","怪","墙","  ","墙","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","墙","墙"}
    ,{"墙","  ","墙","  ","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","墙","  ","墙","墙","  ","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","  ","  ","  ","  ","墙","墙"}
    ,{"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
};
void draw(int g) {
    system("cls");
    if(g==1) {
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++)
{
                if(i==x&&j==y) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf);
                    cout<<"我";
                } else {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),6);
                    cout<<dt2[i][j];
                }
}
            cout<<"\n";
        }
    }
    if(g==2) {
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++)
{
                if(i==gx&&j==gy) 
                {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                    cout<<"怪";
                }
                if(i==x&&j==y) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf);
                    cout<<"我";
                } else {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),6);
                    cout<<dt2[i][j];
                }
}


            cout<<"\n";
        }
    }
}
signed main() {
    draw(1);
    while(1) {
        if(kbhit()) {
            char k = getch();
            if((k=='w'||k=='W')&&dt1[x-1][y]!="墙")
                x--;
            if((k=='a'||k=='A')&&dt1[x][y-1]!="墙")
                y--;
            if((k=='d'||k=='D')&&dt1[x][y+1]!="墙")
                y++;
            if((k=='s'||k=='S')&&dt1[x+1][y]!="墙")
                x++;
            draw(1);
            if(x==m1x&&y==m1y) {
                Sleep(1145);
                system("color 1");
                system("cls");
                cout<<"你赢啦!";
                Sleep(1145);
                break;
            }

            draw(1);
        }
    }
    x=1,y=1;
    while(2) {
        if(kbhit()) {
            char k = getch();
            if((k=='w'||k=='W')&&dt2[x-1][y]!="墙")
                x--;
            if((k=='a'||k=='A')&&dt2[x][y-1]!="墙")
                y--;
            if((k=='d'||k=='D')&&dt2[x][y+1]!="墙")
                y++;
            if((k=='s'||k=='S')&&dt2[x+1][y]!="墙")
                x++;
            draw(2);
            if(x==gx&&y==gy) {
                Sleep(1145);
                system("cls");
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                cout<<"你死啦!";
                Sleep(1145);
                break;
            }
            if(x==m1x&&y==m1y) {
                Sleep(1145);
                system("color 1");
                system("cls");
                cout<<"你赢啦!";
                Sleep(1145);
                break;
            }
            draw(2);
        }
    }
    return 0;
}

//中文显示加-fexec-charset=gbk
//c++14:-std=c++14
/*
0 = 黑色 8 = 灰色 1 = 淡蓝   9 = 蓝色
2 = 淡绿    A = 绿色    3 = 湖蓝      B = 淡浅绿
C = 红色    4 = 淡红    5 = 紫色      D = 淡紫
6 = 黄色    E = 淡黄    7 = 白色      F = 亮白
*/

1.1 update on 2025/1/18 15:33 修改bug

#include<bits/stdc++.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define int long long
#define double long double
const int N=2e6+5;
using namespace std;
int x=1,y=1;
bool f1,f2;
int m1x=1,m1y=8;
string dt1[11 ][11]= {
    {"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
    ,{"墙","  ","墙","  ","  ","墙","  ","  ","门"}
    ,{"墙","  ","墙","  ","墙","墙","  ","墙","墙"}
    ,{"墙","  ","  ","  ","  ","墙","  ","  ","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","墙","  ","墙","墙","  ","墙"}
    ,{"墙","  ","墙","墙","墙","墙","  ","  ","墙"}
    ,{"墙","  ","  ","  ","  ","  ","  ","墙","墙"}
    ,{"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
};
int gx=2,gy=4;
string dt2[25 ][25]= {
    {"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
    ,{"墙","  ","墙","  ","  ","  ","  ","  ","门"}
    ,{"墙","  ","墙","墙","","墙","  ","墙","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","墙","墙"}
    ,{"墙","  ","墙","  ","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","墙","  ","墙","墙","  ","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","  ","  ","  ","  ","墙","墙"}
    ,{"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
};
int gx1=1,gy1=4;
int gx2=4,gy2=6;
string dt3[25 ][25]= {
     {"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
    ,{"墙","  ","  ","  ","怪","  ","  ","  ","门"}
    ,{"墙","墙","墙","墙","  ","墙","  ","墙","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","墙","墙"}
    ,{"墙","  ","墙","  ","  ","墙","怪","  ","墙"}
    ,{"墙","  ","  ","墙","  ","墙","墙","  ","墙"}
    ,{"墙","  ","墙","墙","  ","墙","  ","  ","墙"}
    ,{"墙","  ","  ","  ","  ","  ","  ","墙","墙"}
    ,{"墙","墙","墙","墙","墙","墙","墙","墙","墙"}
};
void draw(int g) {
    system("cls");
    if(g==1) {
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++)
                if(i==x&&j==y) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf);
                    cout<<"我";
                } else {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),6);
                    cout<<dt1[i][j];
                }
            cout<<"\n";
        }
    }
    if(g==2) {
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++){
                if(i==gx&&j==gy) 
                {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                    cout<<"怪";
                }
                if(i==x&&j==y) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf);
                    cout<<"我";
                } else {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),6);
                    cout<<dt2[i][j];
                }
            }
            cout<<"\n";
        }
    }
    if(g==3) {
        for(int i=0; i<9; i++) {
            for(int j=0; j<9; j++)
            {
                if(i==gx1&&j==gy1) 
                {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                    cout<<"怪";
                }
                if(i==x&&j==y) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xf);
                    cout<<"我";
                } else {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),6);
                    cout<<dt3[i][j];
                }
            }
            cout<<"\n";
        }
    }
}
signed main() {
    draw(1);
    while(1) {
        if(kbhit()) {
            char k = getch();
            if((k=='w'||k=='W')&&dt1[x-1][y]!="墙")
                x--;
            if((k=='a'||k=='A')&&dt1[x][y-1]!="墙")
                y--;
            if((k=='d'||k=='D')&&dt1[x][y+1]!="墙")
                y++;
            if((k=='s'||k=='S')&&dt1[x+1][y]!="墙")
                x++;
            draw(1);
            if(x==m1x&&y==m1y) {
                Sleep(1145);
                system("color 1");
                system("cls");
                cout<<"你赢啦!";
                Sleep(1145);
                break;
            }

            draw(1);
        }
    }
    x=1,y=1;
    while(2) {
        if(kbhit()) {
            char k = getch();
            if((k=='w'||k=='W')&&dt2[x-1][y]!="墙")
                x--;
            if((k=='a'||k=='A')&&dt2[x][y-1]!="墙")
                y--;
            if((k=='d'||k=='D')&&dt2[x][y+1]!="墙")
                y++;
            if((k=='s'||k=='S')&&dt2[x+1][y]!="墙")
                x++;
            draw(2);
            if(x==gx&&y==gy) {
                Sleep(1145);
                system("cls");
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                cout<<"你死啦!";
                Sleep(1145);
                return 0;
            }
            if(x==m1x&&y==m1y) {
                Sleep(1145);
                system("color 1");
                system("cls");
                cout<<"你赢啦!";
                Sleep(1145);
                return 0;
            }
            draw(2);
        }
    }
    x=1,y=1;
    int sum=0,sum1=0;
    while(3) {
        if(kbhit()) {
            char k = getch();
            if((k=='w'||k=='W')&&dt2[x-1][y]!="墙")
                x--;
            if((k=='a'||k=='A')&&dt2[x][y-1]!="墙")
                y--;
            if((k=='d'||k=='D')&&dt2[x][y+1]!="墙")
                y++;
            if((k=='s'||k=='S')&&dt2[x+1][y]!="墙")
                x++;
            draw(2);
            if(gx1>=1){
                gx++;
                Sleep(100);
            }
            if(gx1==4)
                sum1=1;
            if(gx1==1&&sum){
                gx++;
                Sleep(100);
                sum1=1;
            }
            else if(gx1>=4)
            {
                sum=0;
                gx--;
            }
            else if(gx1>1&&gx1<=4)
            {
                sum1=0;
                gx2--;
            }
            if(x==gx1&&y==gy1) {
                Sleep(1145);
                system("cls");
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xc);
                cout<<"你死啦!";
                Sleep(1145);
                break;
            }
            if(x==m1x&&y==m1y) {
                Sleep(1145);
                system("color 1");
                system("cls");
                cout<<"你赢啦!";
                Sleep(1145);
                break;
            }
            draw(2);
        }
    }
    return 0;
}