用C++制作一个走迷宫的游戏

1.自己设计一张至少 9*9 的迷宫地图,并按照课上介绍,完成迷宫游戏的开发

 给迷宫游戏加上步数限制 n,当不超过 n 步到达出口时,显示游戏成功;如果 n 步还未到

达,显示游戏失败并结束游戏

#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
int main()
{
	char a[9][10]={"*********","*o  * * *","*** * * E","***   * *",
	"** *  * *","***  ** *","*** *   *","**      *","*********"};//地图 
	for(int i=0;i<9;i++)
	{
		cout<<a[i]<<endl;
	}//地图显示 
	int x=1,y=1,m=0;
    while(1)
	{
		char d;
		m++;
		if(m>23)//控制步数步数大于23失败 
		{
			cout<<"挑战失败!";
			break;
		}
		else
		d=getch();
		if(d=='w'&&a[x-1][y]!='*')
		{
			a[x][y]=' ';
			a[x-1][y]='o';
			x--;
		}//输入w若不为墙向上移动 
		else if(d=='s'&&a[x+1][y]!='*')
		{
			a[x][y]=' ';
			a[x+1][y]='o';
			x++;
		}//s——下 
		else if(d=='a'&&a[x][y-1]!='*')
		{
			a[x][y]=' ';
			a[x][y-1]='o';
			y--;
		}//a——左 
		else if(d=='d'&&a[x][y+1]!='*')
		{
			a[x][y]=' ';
			a[x][y+1]='o';
			y++;
		}//d——右 
		system("cls");//每次刷新 
		for(int i=0;i<9;i++)
		{
					cout<<a[i]<<endl;
			if(x==2&&y==8)//到达E成功 
			{
				system("cls");
				cout<<"成功到达!";
				break;
			}
		}
	}
	return 0;
}

3、自动走迷宫

能否去掉按键控制,让小人 1 秒走一步,自己慢慢走到出迷宫

前提条件

小人事先不知道出去的路径(不能人为帮它预设)

它需要自己摸索怎么走出去

#include<iostream>
#include<cmath> 
#include<stdlib.h>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<windows.h>
using namespace std;
int main()
{
	char a[9][10]={"*********","*o  * * *","*** * * E","***   * *",
	"** *  * *","***  ** *","*** *   *","**      *","*********"};//地图 
	int d=0;
	int x=1,y=1;
	const int move[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//位移矩阵 
	while (1)
	{
		system("cls");//刷新地图 
		for(int i=0;i<9;i++)
		{
			cout<<a[i]<<endl;
		}
		Sleep(1000);//每秒一下 
		while(a[x+move[d][0]][y+move[d][1]]=='*')//依次判断各个方向是否为墙 
		{
			d++;
			if(d>3)
			d=d-4;//构成循环 
		}
		a[x][y]=' ';
		a[x+move[d][0]][y+move[d][1]]='o';
		x=x+move[d][0];
		y=y+move[d][1];//如果一个方向为空则进行移动 
		d--;
		if(d<0)
			d=d+4;//每次更新方向 
			if(x==2&&y==8)//到达E结束 
			{
				system("cls");
				cout<<"成功到达!";
				break;
			}
		}	
   return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个用C++编写的迷宫游戏: ```c++ #include <iostream> #include <vector> #include <string> using namespace std; // 迷宫地图 const vector<string> maze{ "##########", "#S #", "# #### #", "# # #", "# # ####", "# # #", "# #### #", "# #", "# ######", "# #E #", "##########" }; int main() { // 迷宫地图的行数和列数 const int rows = maze.size(); const int cols = maze[0].size(); // 玩家的起始位置 int x = 1; int y = 1; // 玩家的体力值 int health = 10; // 游戏主循环 while (true) { // 打印当前迷宫地图和玩家状态 cout << endl; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (i == x && j == y) { cout << "O"; } else { cout << maze[i][j]; } } cout << endl; } cout << "Health: " << health << endl; // 检查是否到达终点 if (maze[x][y] == 'E') { cout << "You win!" << endl; break; } // 检查是否体力值耗尽 if (health <= 0) { cout << "You lose!" << endl; break; } // 获取玩家输入 char input; cout << "Enter w, s, a, or d to move: "; cin >> input; // 移动玩家 int new_x = x, new_y = y; if (input == 'w') { new_x--; } else if (input == 's') { new_x++; } else if (input == 'a') { new_y--; } else if (input == 'd') { new_y++; } // 检查移动是否合法 if (new_x < 0 || new_x >= rows || new_y < 0 || new_y >= cols) { cout << "Invalid move!" << endl; } else if (maze[new_x][new_y] == '#') { cout << "You hit a wall!" << endl; } else { // 更新玩家位置和体力值 x = new_x; y = new_y; health--; if (maze[x][y] == 'H') { cout << "You found a health pack, +3 health!" << endl; health += 3; } } } return 0; } ``` 这个程序用了一个简单的二维数组来表示迷宫地图,用变量`x`和`y`表示玩家的位置,用变量`health`表示玩家的体力值。在游戏主循环中,程序打印出当前的迷宫地图和玩家状态,然后等待玩家输入,并根据输入移动玩家。在移动玩家时,程序检查移动是否合法,如果合法则更新玩家位置和体力值。当玩家到达终点或体力值耗尽时,游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值