自制迷宫小游戏

· · 个人记录

反正放假了,没事干就写了一点,没有任何技术含量,就是有点麻烦。做的很渣,dalao勿喷QAQ

原理

1.利用随机数生成地图

void Creative(){
    xi=0;//记录箱子总数
    srand((unsigned)time(NULL));
    for(int i=1;i<26;i++){
        for(int j=1;j<26;j++){
            if((rand()%99)<midu)maze[i][j]=1;//生成墙壁
            else if((rand()%99)<3){maze[i][j]=2;xi++;}//生成箱子
            else maze[i][j]=0;
        }
    }
    maze[1][1]=maze[25][25]=0;//剩下的是空白
}

2.判断是否可以到达终点

bool Ismaze(){
    memset(vis,0,sizeof(vis));
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1} ;
    queue<Node> q;
    Node tmp,next;
    tmp.x=1;
    tmp.y=1;
    vis[1][1]=1;
    q.push(tmp);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(tmp.x==25&&tmp.y==25) return 1;
        int xx,yy;
        for(int i=0;i<4;i++){
            xx=tmp.x+dx[i];
            yy=dy[i]+tmp.y;
            if(xx<1||xx>25||yy<1||yy>25) continue;
            if(!maze[xx][yy]&&!vis[xx][yy]){
                next.x=xx;
                next.y=yy;
                q.push(next);
                vis[xx][yy]=1;
            }
        }
    }
    return 0;
} 

3.输出地图

void Printmaze(){
    for(int i=1;i<26;i++){
        for(int j=1;j<26;j++){
            if(i==x&&y==j) cout<<"◎";//角色
            else if(i==25&&j==25) cout<<"※" ; //终点
            else if(maze[i][j]==1) cout<<"■";//墙壁
            else if(maze[i][j]==3) cout <<"☆";//箱子
            else if(maze[i][j]==0 || maze[i][j]==2) cout<<"  "; //路
        }
        cout<<endl;
    }
    cout <<"\n血量:";
    for(int i=1;i<=xue;i++)cout <<"■";
    cout <<"  (" <<xue <<"/10)";
    cout <<"\n宝箱:";
    for(int i=1;i<=xiang;i++)cout <<"☆";
    cout <<"  (" <<xiang <<"/" <<xi <<")";
    cout <<"\n利用 W S A D 来移动\n";
}

Code

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
int x=1,y=1,maze[30][30],zhuang=0,chu=0,bushu=0,midu,xue;
double xiang=0,xi;
bool vis[30][30];
char flag;
struct Node{
    int x,y;
};
void open(){
    cout<<"\n\n\n\n\n\n\n\n\n               按s开始游戏"<<endl;
    cout<<"             按q退出游戏\n"<<endl <<"               ";
    flag=getch();
    cout <<endl;
    while(flag!='s'&&flag!='q') {
        cout<<"             请输入正确的字符\n"<<endl <<"                 ";
        flag=getch();
        cout <<endl;
    }
    if(flag=='q') return ; 
    cout <<"            请输入你想要的难度(0~40之间)\n"<<endl <<"                  ";
    cin >>midu; 
    cout <<endl;
    while(midu<=0 || midu>40){
        cout<<"            请输入合适的难度\n"<<endl <<"                ";
        cin >>midu;
        cout <<endl;
    }
    cout <<"\n              享受游戏吧!\n";
    Sleep(1000);
    system("cls");
}
void Creative(){
    xi=0;
    srand((unsigned)time(NULL));
    for(int i=1;i<26;i++){
        for(int j=1;j<26;j++){
            if((rand()%99)<midu)maze[i][j]=1;
            else if((rand()%99)<3){maze[i][j]=2;xi++;}
            else maze[i][j]=0;
        }
    }
    maze[1][1]=maze[25][25]=0;
}
bool Ismaze(){
    memset(vis,0,sizeof(vis));
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1} ;
    queue<Node> q;
    Node tmp,next;
    tmp.x=1;
    tmp.y=1;
    vis[1][1]=1;
    q.push(tmp);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(tmp.x==25&&tmp.y==25) return 1;
        int xx,yy;
        for(int i=0;i<4;i++){
            xx=tmp.x+dx[i];
            yy=dy[i]+tmp.y;
            if(xx<1||xx>25||yy<1||yy>25) continue;
            if(!maze[xx][yy]&&!vis[xx][yy]){
                next.x=xx;
                next.y=yy;
                q.push(next);
                vis[xx][yy]=1;
            }
        }
    }
    return 0;
} 
void che(int x,int y){
    if(maze[x+1][y]==2)maze[x+1][y]==3;
    if(maze[x+1][y+1]==2)maze[x+1][y+1]==3;
    if(maze[x+1][y-1]==2)maze[x+1][y-1]==3;
    if(maze[x][y-1]==2)maze[x][y-1]==3;
    if(maze[x][y+1]==2)maze[x][y+1]==3;
    if(maze[x-1][y]==2)maze[x-1][y]==3;
    if(maze[x-1][y+1]==2)maze[x-1][y+1]==3;
    if(maze[x-1][y-1]==2)maze[x-1][y-1]==3;
}
void Printmaze(){
    for(int i=1;i<26;i++){
        for(int j=1;j<26;j++){
            if(i==x&&y==j) cout<<"◎";
            else if(i==25&&j==25) cout<<"※" ; 
            else if(maze[i][j]==1) cout<<"■";
            else if(maze[i][j]==3) cout <<"☆";
            else if(maze[i][j]==0 || maze[i][j]==2) cout<<"  "; 
        }
        cout<<endl;
    }
    cout <<"\n血量:";
    for(int i=1;i<=xue;i++)cout <<"■";
    cout <<"  (" <<xue <<"/10)";
    cout <<"\n宝箱:";
    for(int i=1;i<=xiang;i++)cout <<"☆";
    cout <<"  (" <<xiang <<"/" <<xi <<")";
    cout <<"\n利用 W S A D 来移动\n";
}
int main(){
    x=1,y=1,zhuang=0,chu=0,bushu=0,xue=10,xiang=0;
    open();
    if(flag){
        Creative();
        while(!Ismaze()){
            Creative();
            cout <<"\n\n\n\n\n\n\n\n\n              地图生成中……";
            system("cls");
        }
        Printmaze();
        char c;
        while(x!=25||y!=25){
            c=getch();
            bushu++;
            if(c=='W'||c=='w') x--;
            else if(c=='S'||c=='s') x++;
            else if(c=='a'||c=='A') y--;
            else if(c=='d'||c=='D') y++;
            else continue;
            che(x,y);
            system("cls");
            Printmaze();
            if(x>25||x<1||y<1||y>25){
                cout<<"你走出地图了!\n";
                chu++;
                Sleep(500);
                if(x>25||c=='S'||c=='s') x--;
                else if(x<1||c=='W'||c=='w') x++;
                else if(y>25||c=='d'||c=='D') y--;
                else if(y<1||c=='a'||c=='A') y++; 
            }
            else if(maze[x][y]==2){
                cout <<"你发现了一个宝箱!";
                Sleep(500);
                xiang++;
                maze[x][y]=0;
                system("cls");
                Printmaze();
            }
            else if(maze[x][y]==1){
                cout<<"你碰到墙了!\n";
                zhuang++;
                xue--;
                system("cls");
                Printmaze();
                if(xue==0){
                    system("cls");
                    cout <<"你撞墙过多,失败了!\n\n";
                    cout <<"按下“r”重新来一次\n" ;
                    cout <<"按下“q”退出游戏\n";
                    char f;
                    f=getch();
                    if(f=='r'){
                        system("cls");
                        main();
                    }
                    else return 0;
                }
                Sleep(500);
                if(x>25||c=='S'||c=='s') x--;
                else if(x<1||c=='W'||c=='w') x++;
                else if(y>25||c=='d'||c=='D') y--;
                else if(y<1||c=='a'||c=='A') y++; 
            }
            system("cls");
            Printmaze();
        }
        system("cls");
        cout <<"挑战成功!\n";
        cout <<"你一共找到了" <<xiang <<"个箱子!\n";
        cout <<"找到了" <<fixed <<setprecision(2) <<xiang/xi*100.0 <<"%的箱子\n";
        if(xi==xiang)cout <<"全部找齐了!\n\n";
        else {
            cout <<fixed <<setprecision(0) <<"还差" <<xi-xiang <<"个箱子哦!\n\n";
            cout <<"它们分别在:\n";
            for(int i=1;i<=25;i++){
                for(int j=1;j<=25;j++){
                    if(maze[i][j]==2)cout <<i <<"," <<j <<"     "; 
                }
            }
            cout <<endl;
        }
        cout <<"你一共撞了" <<zhuang <<"次墙,\n";
        cout <<"你一共走出地图" <<chu <<"次。\n";
        cout <<"你一共走了" <<bushu <<"步。\n\n";
        cout <<"按下“r”重新来一次\n" ;
        cout <<"按下“q”退出游戏\n";
        char f;
        f=getch();
        if(f=='r'){
            system("cls");
            main();
        }
        else return 0;
        system("pause"); 
    }
    return 0;
}

截图

点击下载