• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C++实现迷宫小游戏

c++ 搞代码 4年前 (2022-01-06) 36次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了C++实现迷宫小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

介绍

本程序是根据广度优先遍历算法的思想设计的一款迷宫游戏,游戏设计了两种模式一种自动游戏模式,一种手动模式。因为项目在 Linux 开发,需要在 Windows 开发的,请查看源代码中需要修改地方的备注。

截图

代码

 #include #include //标准库 #include //延时函数 #include //getchar #include #include //终端设置 #define MAX_X 20 #define MAX_Y 30 bool flag = false; bool slow = false; bool autogame = true; using namespace std; int maze[MAX_X][MAX_Y]; //迷宫 //路线栈 class stack_of_maze{ private: //记录迷宫坐标 struct node { int x; int y; char direction; //上一步路径(如何来的) node* next; }; node* head; public: stack_of_maze(){ head = NULL; } ~stack_of_maze(){ node* p = head; while(head!=NULL){ head = head->next; delete p; p = head; } } //压栈 void push(int xx,int yy,char ddirection){ node* new_node = new node; if(new_node!=NULL){ new_node->x = xx; new_node->y = yy; new_node->direction = ddirection; new_node->next = NULL; if(head==NULL) head = new_node; else{ new_node->next = head; head = new_node; } } else cout<<"内存分配失败"<next; xx = p->x; yy = p->y; delete p; } return head; } void print(){ if(head!

来源gao!%daima.com搞$代*!码$网

=NULL){ node* p = head; while(p!=NULL){ cout<<" "<x<<" "<y<<" "<direction<next; } } else cout<<"栈为空,打印失败"<<endl; } }; //创建迷宫 void createMaze(){ int maxway = MAX_X * MAX_Y; //最大通路 int x,y; for(x=0;x<MAX_X;x++) for(y=0;y<MAX_Y;y++) maze[x][y] = 1; //先填充迷宫 srand((unsigned)time(NULL)); //随机函数种子,以时间为参数 for(int i=0;i<maxway;i++) //随机构建迷宫通路 { x = rand() % (MAX_X-2) + 1; y = rand() % (MAX_Y-2) + 1; maze[x][y] = 0; } maze[1][1] = 0; //入口 maze[MAX_X-2][MAX_Y-2] = 0; //出口 maze[0][1] = 3; maze[MAX_X-1][MAX_Y-2] = 0; } //输出迷宫 void printMaze(){ int x,y; system("clear"); //windows下使用system("cls") //cout<<endl; for(x=0;x<MAX_X;x++) { for(y=0;y<MAX_Y;y++) { if(maze[x][y]==0){cout<<" ";continue;} //通路 if(maze[x][y]==1){cout<<"■";continue;} //墙 if(maze[x][y]==2){cout<<"×";continue;} //死胡同 if(maze[x][y]==3){cout<<"↓";continue;} //向下走 if(maze[x][y]==4){cout<<"→";continue;} if(maze[x][y]==5){cout<<"←";continue;} if(maze[x][y]==6){cout<<"↑";continue;} if(maze[x][y]==7){cout<<"※";continue;} //当前站立位置 } cout<<endl; } if(slow){ sleep(1); //延时函数 } } void check(stack_of_maze &s){ int temp[MAX_X][MAX_Y]; for(int x=0;x<MAX_X;x++) for(int y=0;y<MAX_Y;y++) temp[x][y] = maze[x][y]; int x=1,y=1; //出发点 while(1){ temp[x][y] = 2; //向下 if(temp[x+1][y]==0){ s.push(x,y,'D'); temp[x][y] = 3; //在当前位置做一个向下的标志 x = x + 1; temp[x][y] = 7; //当前位置 if((x==MAX_X-1)&&(y==MAX_Y-2)){ flag = true; return; } else continue; } //向右 if(temp[x][y+1]==0){ s.push(x,y,'R'); temp[x][y] = 4; //在当前位置做一个向右的标志 y = y + 1; temp[x][y] = 7; if((x==MAX_X-1)&&(y==MAX_Y-2)){ flag = true; return; } else continue; } //向上 if(temp[x-1][y]==0){ s.push(x,y,'U'); temp[x][y] = 6; //在当前位置做一个向上的标志 x = x - 1; temp[x][y] = 7; if((x==MAX_X-1)&&(y==MAX_Y-2)){ flag = true; return; } else continue; } //向左 if(temp[x][y-1]==0){ s.push(x,y,'L'); temp[x][y] = 5; //在当前位置做一个向右的标志 y = y - 1; temp[x][y] = 7; if((x==MAX_X-1)&&(y==MAX_Y-2)){ flag = true; return; } else continue; } //上下左右不通,则回退 if(s.pop(x,y)==NULL && temp[x-1][y]!=0 && temp[x][y-1]!=0 && temp[x][y+1]!=0 && temp[x+1][y]!=0){ temp[0][1] = 7; if(temp[1][1]!=1) temp[1][1] = 2; return; } } } //输入,windows下可以使用#incldue替代此函数 char getch(){ char ch; static struct termios oldt, newt; //保存原有终端属性和新设置的终端属性 tcgetattr( STDIN_FILENO, &oldt); //获得终端原有属性并保存在结构体oldflag //设置新的终端属性 newt = oldt; newt.c_lflag &= ~(ICANON); tcsetattr( STDIN_FILENO, TCSANOW, &newt); //取消回显 system("stty -echo"); ch = getchar(); system("stty echo"); tcsetattr( STDIN_FILENO, TCSANOW, &oldt); //让终端恢复为原有的属性 return ch; } void move(){ int x=1,y=1; //出发点 while(1){ switch(getch()){ case 's': if(maze[x+1][y]==0){ maze[x][y] = 0; x = x + 1; maze[x][y] = 7; //当前位置 printMaze(); if((x==MAX_X-1)&&(y==MAX_Y-2)){ cout<<"\n\n 成功走出"<<endl; return; } } break; case 'd': if(maze[x][y+1]==0){ if(maze[x][y+1]==0){ maze[x][y] = 0; y = y + 1; maze[x][y] = 7; printMaze(); if((x==MAX_X-1)&&(y==MAX_Y-2)){ cout<<"\n\n 成功走出"<<endl; return; } } } break; case 'w': if(maze[x-1][y]==0){ maze[x][y] = 0; x = x - 1; maze[x][y] = 7; printMaze(); if((x==MAX_X-1)&&(y==MAX_Y-2)){ cout<<"\n\n 成功走出"<<endl; return; } } break; case 'a': if(maze[x][y-1]==0){ maze[x][y] = 0; y = y - 1; maze[x][y] = 7; printMaze(); if((x==MAX_X-1)&&(y==MAX_Y-2)){ cout<<"\n\n 成功走出"<<endl; return; } } break; } } } void autoMove(stack_of_maze &s){ int x=1,y=1; //出发点 while(1){ maze[x][y] = 2; /

以上就是C++实现迷宫小游戏的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++实现迷宫小游戏
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址