C++实现迷宫小游戏

这篇文章主要为大家详细介绍了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!=NULL){ node* p = head; while(p!=NULL){ cout<<" "<x<<" "<y<<" "<direction<next; } } else cout<<"栈为空,打印失败"<替代此函数 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    成功走出"<以上就是C++实现迷宫小游戏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言