欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++实现简单迷宫游戏

程序员文章站 2022-06-24 18:21:23
本文实例为大家分享了c++实现简单迷宫游戏的具体代码,供大家参考,具体内容如下问题描述程序开始运行时显示一个迷宫地图,迷宫*有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向健操纵老鼠...

本文实例为大家分享了c++实现简单迷宫游戏的具体代码,供大家参考,具体内容如下

问题描述

程序开始运行时显示一个迷宫地图,迷宫*有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向健操纵老鼠在规定的时间内走到粮仓处。

基本要求

(1)老鼠形象可以辨认,可用键盘操纵老鼠上下左右移动;
(2)迷宫的墙足够结实,老鼠不能穿墙而过;
(3)正确检测结果,若老鼠在规定时间内走到粮仓处,提示 成功,并给出一条路径,否则提示失败;
(4)添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙;

提高要求

(1)增加闯关和计分功能;
(2)找出走出迷宫的所有路径及最短路径。

源代码

1.文件main.cpp

#include<iostream>
#include"maze.h"//自定义的头文件,存放类声明和常量
#include<cstdlib>//各种函数头文件,如rand函数
#include<time.h>//日期和时间的头文件
#include<stdio.h>//标准输入输出
#include<windows.h>//图形界面设计头文件
#include<conio.h>//获取方向键的头文件

using namespace std;

void mainmenu();//显示游戏菜单函数
void meungame(int num);//显示不同关卡函数
void introduce();//游戏介绍

int main()
{
 maze maze_1(11, 11), maze_2(13, 13), maze_3(15, 15), maze_4(17, 17);//定义四个关卡
 int i = 0, choice = 0, choice_2, choice_3;
 char isexit = ' ', choice_1 = ' ';
 bool go_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)
 {
 case 1:
  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)" << endl;
   cin >> choice_1;
   if (choice_1 == 'y' || choice_1 == 'y')
   {
    go_on = true;
   }
   else if (choice_1 == 'n' || choice_1 == 'n')
   {
    go_on = false;
   }
   }
   else
   {
   system("pause");
   break;
   }
  }
  else if (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)" << endl;
   cin >> choice_1;
   if (choice_1 == 'y' || choice_1 == 'y')
   {
    go_on = true;
   }
   else if (choice_1 == 'n' || choice_1 == 'n')
   {
    go_on = false;
   }
   }
   else
   {
   system("pause");
   break;
   }
  }
  else if (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)" << endl;
   cin >> choice_1;
   if (choice_1 == 'y' || choice_1 == 'y')
   {
    go_on = true;
   }
   else if (choice_1 == 'n' || choice_1 == 'n')
   {
    go_on = false;
   }
   }
   else
   {
   system("pause");
   break;
   }
  }
  else if (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★太棒了,恭喜你通过全部关卡★" << endl;
   cout << "\t 是否退出游戏?(y|n)" << endl;
   cin >> choice_1;
   if (choice_1 == 'y' || choice_1 == 'y')
   {
    return 0;
   }
   else if (choice_1 == 'n' || choice_1 == 'n')
   {
    go_on = false;
   }
   }
   else
   {
   system("pause");
   break;
   }
  }
  if (go_on == false)
  {
   break;
  }
  }
  break;
 case 2:
  system("cls");
  cout << "请你输入要编辑的关卡:" << endl;
  cout << "1.第一关" << endl;
  cout << "2.第二关" << endl;
  cout << "3.第三关" << endl;
  cout << "4.第四关" << endl;
  cin >> choice_2;
  if (choice_2 == 1 && judge[0] == true)
  {
  maze_1.edidmap();
  }
  else if (choice_2 == 2 && judge[1] == true)
  {
  maze_2.edidmap();
  }
  else if (choice_2 == 3 && judge[2] == true)
  {
  maze_3.edidmap();
  }
  else if (choice_2 == 4 && judge[3] == true)
  {
  maze_4.edidmap();
  }
  else
  {
  cout << "此关卡未通过,不能编译此关!" << endl;
  system("pause");
  }
  break;
 case 3:
  system("cls");
  cout << "请你输入要查看的关卡" << endl;
  cout << "1.第一关" << endl;
  cout << "2.第二关" << endl;
  cout << "3.第三关" << endl;
  cout << "4.第四关" << endl;
  cin >> choice_3;
  if (choice_3 == 1 && judge[0] == true)
  {
  maze_1.short();
  }
  else if (choice_3 == 2 && judge[1] == true)
  {
  maze_2.short();
  }
  else if (choice_3 == 3 && judge[2] == true)
  {
  maze_3.short();
  }
  else if (choice_3 == 4 && judge[3] == true)
  {
  maze_4.short();
  }
  else
  {
  cout << "此关卡未通过,不能查看此关!" << endl;
  system("pause");
  }
  break;
 case 4:
  system("cls");
  cout << "\n\n\t\t是否确定退出游戏?(y|n)";
  cin >> isexit;
  if (isexit == 'y' || isexit == 'y')
  {
  return 0;
  }
  else if (isexit == 'n' || isexit == 'n')
  {
  break;
  }
 default:
  cout << "\n\t输入选择选择无效,请从新输入:";
  sleep(500);
  break;
 }
 system("cls");
 mainmenu();
 cout << "\n\t请输入输入选择:(1~4)";
 cin >> choice;
 }
}

void mainmenu()//游戏菜单函数
{
 cout << "\n\t\t**************************************************************" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*           *老鼠走迷宫游戏*           *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*            1.开始游戏             *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*            2.编辑地图             *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*            3.破解地图             *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*            4.退出游戏             *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t**************************************************************" << endl;
}

void meungame(int num)//关卡菜单
{
 system("cls");
 cout << "\n\t\t**************************************************************" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t*            ◤ 第" << num << "关 ◢             *" << endl;
 cout << "\t\t*                              *" << endl;
 cout << "\t\t**************************************************************" << endl;
 system("pause");
}

void introduce()//游戏介绍
{
 cout << "\n\t\t********************************************************************" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t*           *老鼠走迷宫游戏介绍*             *" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t*      1.玩家可通过方向键↑↓←→控制老鼠移动        *" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t*      2.在选择编辑地图时,玩家通过wasd编辑         *" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t*      3.在选择破解地图时,会给出坐标路线,原点为迷宫左上角 *" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t*      4.在规定时间内走到粮仓算过关,时间越短所获积分越多  *" << endl;
 cout << "\t\t*                                 *" << endl;
 cout << "\t\t********************************************************************" << endl;
 system("pause");
}

2.maze.cpp文件

#include<iostream>
#include"maze.h"//自定义的头文件,存放类声明和常量
#include<cstdlib>//各种函数头文件,如rand函数
#include<time.h>//日期和时间的头文件
#include<stdio.h>//标准输入输出
#include<windows.h>//图形界面设计头文件
#include<conio.h>//获取方向键的头文件

using namespace std;

maze::maze(int l, int w)//构造函数初始化地图和成员变量
{
 int i, 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 < map_length;i++)
 {
 for (j = 0;j < map_width;j++)
 {
  visited[i][j] = 0;
 }
 }

 front = rear = -1;
 top = -1;
}

void maze::createmap(int x, int y)//创建地图
{
 int direction[4][2] = { {1,0}, {0,1}, {0,-1}, {-1,0} };//定义四个方向
 int i, j, temp;
 for (i = 0;i < 4;i++)//打乱四个方向
 {
 j = rand() % 4;
 temp = direction[i][0];
 direction[i][0] = direction[j][0];
 direction[j][0] = temp;
 temp = direction[i][1];
 direction[i][1] = direction[j][1];
 direction[j][1] = temp;
 }

 map[x][y] = road;//选取[x][y]为路

 for (i = 0;i < 4;i++)
 {
 if (map[x + 2 * direction[i][0]][y + 2 * direction[i][1]] == wall)//任意两点之间有路
 {
  map[x + direction[i][0]][y + direction[i][1]] = road;
  createmap(x + 2 * direction[i][0], y + 2 * direction[i][1]);
 }
 }
}

void maze::show_map()//显示地图
{
 //srand((unsigned)time(null));//种随机粒子
 createmap(2 * (rand() % (map_length / 2 + 1)), 2 * (rand() % (map_width / 2 + 1)));//随机选取x,y坐标
 map[map_length / 2][map_width / 2] = mouse;//定义老鼠的位置
 map[map_length - 1][map_width - 1] = end;//定义粮仓的位置
 display();
}

void maze::display()//查看地图
{
 int i, j;
 for (i = 0;i <= map_length;i++)
 {
 for (j = 0;j <= map_width;j++)
 {
  if (map[i][j] == road)
  {
  cout << " ";
  }
  else if (map[i][j] == wall)
  {
  cout << "■";
  }
  else if (map[i][j] == mouse)
  {
  cout << "♂";
  }
  else if (map[i][j] == end)
  {
  cout << "★";
  }
  else if (map[i][j] == visited)
  {
  cout << " ";
  }
 }
 cout << endl;
 }
}

void maze::keepmap()//显示老鼠走过的路径
{
 int i, j;
 for (i = 0;i <= map_length;i++)
 {
 for (j = 0;j <= map_width;j++)
 {
  if (map[i][j] == road)
  {
  cout << " ";
  }
  else if (map[i][j] == wall)
  {
  cout << "■";
  }
  else if (map[i][j] == mouse)
  {
  cout << "♂";
  }
  else if (map[i][j] == end)
  {
  cout << "★";
  }
  else if (map[i][j] == visited)
  {
  cout << "×";
  }
 }
 cout << endl;
 }
}

bool maze::move()//老鼠移动
{
 int count = 30, m = 0, n = 0;
 bool temp = false;
 char enter = ' ';
 int t = time(null);//获取系统时间
 pos_x = map_length / 2, pos_y = map_width / 2;//老鼠的初始位置
 while (count >= 0)
 {
 if (_kbhit() == 0)
 {

  if (t != time(null))
  {
  system("cls");
  display();
  count--;
  cout << "|---剩余时间:" << count << "---|";
  if (count == 0)
  {
   system("cls");
   cout << "闯关失败" << endl;
   temp = false;
  }
  t = time(null);//获取当前时间
  }
 }
 if (_kbhit() != 0)
 {
  system("cls");
  enter = _getch();
  if (enter == -32)//*****键盘事件*****
  {
  switch (_getch())
  {
  case 72://up
   up();
   display();
   break;
  case 80://down
   dowm();
   display();
   break;
  case 75://left
   left();
   display();
   break;
  case 77://right
   right();
   display();
   break;
  default:
   break;
  }
  }
  if (map[map_length - 1][map_width - 1] != end)
  {
  system("cls");
  cout << "★恭喜你闯关成功★" << endl;
  map[pos_x][pos_y] = visited;
  cout << "★获得" << count << "点积分★" << endl;
  temp = true;
  break;
  }
 }
 }
 return temp;
}

void maze::up()//老鼠向上移动
{
 if (pos_y <= 0)
 {
 return;
 }
 else if (map[pos_x - 1][pos_y] != wall)
 {
 map[pos_x][pos_y] = visited;
 pos_x--;
 map[pos_x][pos_y] = mouse;
 }
}

void maze::dowm()//老鼠向下移动
{
 if (pos_y > map_width - 1)
 {
 return;
 }
 else if (map[pos_x + 1][pos_y] != wall)
 {
 map[pos_x][pos_y] = visited;
 pos_x++;
 map[pos_x][pos_y] = mouse;
 }
}

void maze::left()//老鼠向左移动
{
 if (pos_x <= 0)
 {
 return;
 }
 else if (map[pos_x][pos_y - 1] != wall)
 {
 map[pos_x][pos_y] = visited;
 pos_y--;
 map[pos_x][pos_y] = mouse;
 }
}

void maze::right()//老鼠向右移动
{
 if (pos_x > map_width - 1)
 {
 return;
 }
 else if (map[pos_x][pos_y + 1] != wall)
 {
 map[pos_x][pos_y] = visited;
 pos_y++;
 map[pos_x][pos_y] = mouse;
 }
}

void maze::edidmap()//编辑地图
{
 system("cls");
 char enter = ' ';
 bool iskeep = 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())
  {
  case 72://up
  up();
  break;
  case 80://down
  dowm();
  break;
  case 75://left
  left();
  break;
  case 77://right
  right();
  break;
  default:
  break;
  }
 }
 switch (enter)
 {
 case 119://w键
  if (map[pos_x - 1][pos_y] == wall)
  {
  map[pos_x - 1][pos_y] = road;
  }
  else if (map[pos_x - 1][pos_y] == road || map[pos_x - 1][pos_y] == visited)
  {
  map[pos_x - 1][pos_y] = wall;
  }
  break;
 case 97://a键
  if (map[pos_x][pos_y - 1] == wall)
  {
  map[pos_x][pos_y - 1] = road;
  }
  else if (map[pos_x][pos_y - 1] == road || map[pos_x][pos_y - 1] == visited)
  {
  map[pos_x][pos_y - 1] = wall;
  }
  break;
 case 115://s键
  if (map[pos_x + 1][pos_y] == wall)
  {
  map[pos_x + 1][pos_y] = road;
  }
  else if (map[pos_x + 1][pos_y] == road || map[pos_x + 1][pos_y] == visited)
  {
  map[pos_x + 1][pos_y] = wall;
  }
  break;
 case 100://d键
  if (map[pos_x][pos_y + 1] == wall)
  {
  map[pos_x][pos_y + 1] = road;
  }
  else if (map[pos_x][pos_y + 1] == road || map[pos_x][pos_y + 1] == visited)
  {
  map[pos_x][pos_y + 1] = wall;
  }
  break;
 case 0x0d://回车
  system("cls");
  map[pos_x][pos_y] = road;
  cout << "*****保存成功*****" << endl;
  iskeep = true;
  system("pause");
  break;
 default:
  break;
 }
 if (iskeep == true)
 {
  for (int i = 0;i < map_length;i++)
  {
  for (int j = 0;j < map_width;j++)
  {
   if (map[i][j] == visited)
   {
   map[i][j] = road;
   }
  }
  }
  break;
 }
 system("cls");
 }
}

void maze::short()
{
 rear = front = -1;
 int i, j;
 for (i = 1;i <= map_length;i++)
 {
 for (j = 1;j <= map_width;j++)
 {
  if (map[i][j] == visited)
  {
  map[i][j] = road;//被访问的变成路
  }
 }
 }
 for (i = 0;i <= map_length;i++)
 {
 for (j = 0;j <= map_width;j++)
 {
  visited[i][j] = 0;
 }
 }
 show_map();//显示地图

 system("cls");
 int m = map_length - 1, n = map_width - 1;
 smallroaddisplay(m, n);//找最短路径

 while (top != -1)
 {
 top--;
 }
}

void maze::smallroaddisplay(int x, int y)//最短路径
{
 bool flag = false;
 visited[x - 1][y - 1] = 1;
 map[x][y] = end;
 int i = 0, j = 0, k = 0;
 int direction[4][2] = { {1,0}, {0,1}, {0,-1}, {-1,0} };//定义四个方向
 int arr[100] = { 0 };//存放x坐标的队列
 int brr[100] = { 0 };//存放y坐标的队列
 int record_x[100] = { 0 };//存放x坐标的栈
 int record_y[100] = { 0 };//存放y坐标的栈
 front = rear = -1;
 rear++;
 arr[rear] = x;//当前x坐标入队
 brr[rear] = y;//当前y坐标入队
 while (front != rear)
 {
 front++;
 for (i = 0;i < 4;i++)
 {
  if ((map[arr[front] + direction[i][0]][brr[front] + direction[i][1]] == road || map[arr[front] + direction[i][0]][brr[front] + direction[i][1]] == mouse || map[arr[front] + direction[i][0]][brr[front] + direction[i][1]] == visited) && visited[arr[front] + direction[i][0]][brr[front] + direction[i][1]] == 0 && arr[front] < map_width && arr[front] >= 1 && brr[front] < map_length && 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 << "最短路径如下:" << endl;
 cout << "鼠" << "->";
 for (i = 0;i <= top;i++)
 {
 cout << "(" << record_x[i] << "," << record_y[i] << ")" << "->";
 }
 cout << "仓" << endl;
 system("pause");
}

3.maze.h文件

#ifndef maze_h
#define maze_h

const int maxsize = 100;
const int road = 0;//路
const int wall = 1;//墙
const int mouse = 2;//老鼠
const int end = 3;//终点
const int visited = 4;//被访问的路径
const int maxsmall = 5;//最短路径

class maze
{
private:
 int pos_x, pos_y;//主角老鼠的坐标
 int map_length, map_width;//地图的长宽
 int visited[maxsize][maxsize];//是否被访问数组
 int rear, front;
 int top;
public:
 maze(int l, int w);//构造函数
 int map[maxsize][maxsize];//地图数组
 void createmap(int, int);//创建地图
 void show_map();//显示地图
 void display();//查看地图
 void keepmap();//显示老鼠走过的路径
 bool move();//老鼠移动
 void up();//老鼠向上移动
 void dowm();//老鼠向下移动
 void right();//老鼠向右移动
 void left();//老鼠向左移动
 void edidmap();//编辑地图
 void short();
 void smallroaddisplay(int x, int y);//最短路径
};
#endif
#pragma once

版本提示

所用开发版本为vs2019版,版本不同编译可能存在错误。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C++ 迷宫