C++实现简单迷宫游戏
本文实例为大家分享了C++实现简单迷宫游戏的具体代码,供大家参考,具体内容如下
问题描述
程序开始运行时显示一个迷宫地图,迷宫中央有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向健操纵老鼠在规定的时间内走到粮仓处。
基本要求
(1)老鼠形象可以辨认,可用键盘操纵老鼠上下左右移动;
(2)迷宫的墙足够结实,老鼠不能穿墙而过;
(3)正确检测结果,若老鼠在规定时间内走到粮仓处,提示成功,并给出一条路径,否则提示失败;
(4)添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙;
提高要求
(1)增加闯关和计分功能;
(2)找出走出迷宫的所有路径及最短路径。
源代码
1.文件main.cpp
#include#include"maze.h"//自定义的头文件,存放类声明和常量 #include //各种函数头文件,如rand函数 #include //日期和时间的头文件 #include //标准输入输出 #include //图形界面设计头文件 #include //获取方向键的头文件 usingnamespacestd; voidMainMenu();//显示游戏菜单函数 voidMeunGame(intnum);//显示不同关卡函数 voidIntroduce();//游戏介绍 intmain() { Mazemaze_1(11,11),maze_2(13,13),maze_3(15,15),maze_4(17,17);//定义四个关卡 inti=0,choice=0,choice_2,choice_3; charisexit='',choice_1=''; boolGo_on=false,judge[4]={false}; cout<<"\n\t欢迎试玩憨憨老鼠走迷宫游戏"; for(i=0;i<4;i++) { Sleep(500); cout<<"·"; } system("cls"); Introduce();//游戏介绍 system("cls"); MainMenu();//显示游戏菜单 cout<<"\n\t请输入选择:(1~4)"; cin>>choice; while(1) { switch(choice) { case1: for(i=1;i<=4;i++) { if(i==1) { MeunGame(i);//显示关卡菜单 system("cls"); maze_1.Show_Map(); judge[0]=maze_1.Move(); if(judge[0]==true) { cout<<"憨憨鼠所走过的路径"; maze_1.KeepMap(); cout<<"是否继续闯关:(Y|N)"< >choice_1; if(choice_1=='y'||choice_1=='Y') { Go_on=true; } elseif(choice_1=='n'||choice_1=='N') { Go_on=false; } } else { system("pause"); break; } } elseif(i==2) { MeunGame(i); system("cls"); maze_2.Show_Map(); judge[1]=maze_2.Move(); if(judge[1]==true) { cout<<"憨憨鼠所走过的路径"; maze_2.KeepMap(); cout<<"是否继续闯关:(Y|N)"< >choice_1; if(choice_1=='y'||choice_1=='Y') { Go_on=true; } elseif(choice_1=='n'||choice_1=='N') { Go_on=false; } } else { system("pause"); break; } } elseif(i==3) { MeunGame(i); system("cls"); maze_3.Show_Map(); judge[2]=maze_3.Move(); if(judge[2]==true) { cout<<"憨憨鼠所走过的路径"; maze_3.KeepMap(); cout<<"是否继续闯关:(Y|N)"< >choice_1; if(choice_1=='y'||choice_1=='Y') { Go_on=true; } elseif(choice_1=='n'||choice_1=='N') { Go_on=false; } } else { system("pause"); break; } } elseif(i==4) { MeunGame(i); system("cls"); maze_4.Show_Map(); judge[3]=maze_4.Move(); if(judge[3]==true) { cout<<"憨憨鼠所走过的路径"; maze_4.KeepMap(); system("pause"); system("cls"); cout<<"\t★太棒了,恭喜你通过全部关卡★"< >choice_1; if(choice_1=='y'||choice_1=='Y') { return0; } elseif(choice_1=='n'||choice_1=='N') { Go_on=false; } } else { system("pause"); break; } } if(Go_on==false) { break; } } break; case2: system("cls"); cout<<"请你输入要编辑的关卡:"< >choice_2; if(choice_2==1&&judge[0]==true) { maze_1.EdidMap(); } elseif(choice_2==2&&judge[1]==true) { maze_2.EdidMap(); } elseif(choice_2==3&&judge[2]==true) { maze_3.EdidMap(); } elseif(choice_2==4&&judge[3]==true) { maze_4.EdidMap(); } else { cout<<"此关卡未通过,不能编译此关!"< >choice_3; if(choice_3==1&&judge[0]==true) { maze_1.Short(); } elseif(choice_3==2&&judge[1]==true) { maze_2.Short(); } elseif(choice_3==3&&judge[2]==true) { maze_3.Short(); } elseif(choice_3==4&&judge[3]==true) { maze_4.Short(); } else { cout<<"此关卡未通过,不能查看此关!"< >isexit; if(isexit=='Y'||isexit=='y') { return0; } elseif(isexit=='N'||isexit=='n') { break; } default: cout<<"\n\t输入选择选择无效,请从新输入:"; Sleep(500); break; } system("cls"); MainMenu(); cout<<"\n\t请输入输入选择:(1~4)"; cin>>choice; } } voidMainMenu()//游戏菜单函数 { cout<<"\n\t\t**************************************************************"< 2.maze.cpp文件
#include#include"maze.h"//自定义的头文件,存放类声明和常量 #include //各种函数头文件,如rand函数 #include //日期和时间的头文件 #include //标准输入输出 #include //图形界面设计头文件 #include //获取方向键的头文件 usingnamespacestd; Maze::Maze(intl,intw)//构造函数初始化地图和成员变量 { inti,j; Map_Length=l,Map_Width=w; for(i=0;i<=Map_Length;i++) { for(j=0;j<=Map_Width;j++) { if(i==0||j==0) { Map[i][j]=road; } else { Map[i][j]=wall;//默认地图中都是墙 } } } for(i=0;i =0) { if(_kbhit()==0) { if(t!=time(NULL)) { system("cls"); Display(); count--; cout<<"|---剩余时间:"< Map_Width-1) { return; } elseif(Map[pos_x+1][pos_y]!=wall) { Map[pos_x][pos_y]=visited; pos_x++; Map[pos_x][pos_y]=Mouse; } } voidMaze::Left()//老鼠向左移动 { if(pos_x<=0) { return; } elseif(Map[pos_x][pos_y-1]!=wall) { Map[pos_x][pos_y]=visited; pos_y--; Map[pos_x][pos_y]=Mouse; } } voidMaze::Right()//老鼠向右移动 { if(pos_x>Map_Width-1) { return; } elseif(Map[pos_x][pos_y+1]!=wall) { Map[pos_x][pos_y]=visited; pos_y++; Map[pos_x][pos_y]=Mouse; } } voidMaze::EdidMap()//编辑地图 { system("cls"); charEnter=''; boolisKeep=false; pos_x=Map_Length/2,pos_y=Map_Width/2;//确定老鼠坐标 Map[pos_x][pos_y]=Mouse; while(1) { Display(); Enter=_getch(); if(Enter==-32)//*****键盘事件***** { switch(_getch()) { case72://up Up(); break; case80://down Dowm(); break; case75://left Left(); break; case77://right Right(); break; default: break; } } switch(Enter) { case119://W键 if(Map[pos_x-1][pos_y]==wall) { Map[pos_x-1][pos_y]=road; } elseif(Map[pos_x-1][pos_y]==road||Map[pos_x-1][pos_y]==visited) { Map[pos_x-1][pos_y]=wall; } break; case97://A键 if(Map[pos_x][pos_y-1]==wall) { Map[pos_x][pos_y-1]=road; } elseif(Map[pos_x][pos_y-1]==road||Map[pos_x][pos_y-1]==visited) { Map[pos_x][pos_y-1]=wall; } break; case115://S键 if(Map[pos_x+1][pos_y]==wall) { Map[pos_x+1][pos_y]=road; } elseif(Map[pos_x+1][pos_y]==road||Map[pos_x+1][pos_y]==visited) { Map[pos_x+1][pos_y]=wall; } break; case100://D键 if(Map[pos_x][pos_y+1]==wall) { Map[pos_x][pos_y+1]=road; } elseif(Map[pos_x][pos_y+1]==road||Map[pos_x][pos_y+1]==visited) { Map[pos_x][pos_y+1]=wall; } break; case0x0D://回车 system("cls"); Map[pos_x][pos_y]=road; cout<<"*****保存成功*****"< =1&&brr[front] =1) { rear++; arr[rear]=arr[front]+Direction[i][0]; brr[rear]=brr[front]+Direction[i][1]; Visited[arr[front]+Direction[i][0]][brr[front]+Direction[i][1]]=1; if(arr[rear]==(Map_Length/2)&&brr[rear]==(Map_Width/2)) { flag=true; break; } } } if(flag==true) { break; } } front=rear+1; rear=0; top=-1; top++; record_x[top]=arr[front-1]; record_y[top]=brr[front-1]; while(rear!=front) { front--; for(j=0;j<4;j++) { if(record_x[top]+Direction[j][0]==arr[front-1]&&record_y[top]+Direction[j][1]==brr[front-1]) { top++; record_x[top]=arr[front-1]; record_y[top]=brr[front-1]; } } } Display(); cout<<"最短路径如下:"< "; for(i=0;i<=top;i++) { cout<<"("< "; } cout<<"仓"< 3.maze.h文件
#ifndefMAZE_H #defineMAZE_H constintMaxSize=100; constintroad=0;//路 constintwall=1;//墙 constintMouse=2;//老鼠 constintEnd=3;//终点 constintvisited=4;//被访问的路径 constintMaxSmall=5;//最短路径 classMaze { private: intpos_x,pos_y;//主角老鼠的坐标 intMap_Length,Map_Width;//地图的长宽 intVisited[MaxSize][MaxSize];//是否被访问数组 intrear,front; inttop; public: Maze(intl,intw);//构造函数 intMap[MaxSize][MaxSize];//地图数组 voidCreateMap(int,int);//创建地图 voidShow_Map();//显示地图 voidDisplay();//查看地图 voidKeepMap();//显示老鼠走过的路径 boolMove();//老鼠移动 voidUp();//老鼠向上移动 voidDowm();//老鼠向下移动 voidRight();//老鼠向右移动 voidLeft();//老鼠向左移动 voidEdidMap();//编辑地图 voidShort(); voidSmallRoadDisplay(intx,inty);//最短路径 }; #endif #pragmaonce版本提示
所用开发版本为vs2019版,版本不同编译可能存在错误。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。