纯c++简易的迷宫小游戏

发布时间:2024-01-15 11:43:58

一个用c++写的黑框框迷宫

适合新手入门学习

也适合大学生小作业

下面附上代码

总体思路

  1. 初始化游戏界面:设置迷宫的大小(WIDTH和HEIGH),生成迷宫地图(map),包括墙壁、空地、起点和终点。
  2. 显示欢迎界面和游戏规则:通过Welcome()函数和Rule()函数分别实现。
  3. 开始计时:记录游戏开始的时间。
  4. 游戏主循环:在Play()函数中,不断获取用户输入(上下左右键),根据输入移动角色,直到到达出口或超时。
  5. 判断游戏结果:如果角色成功走出迷宫且在规定时间内,显示恭喜信息;否则,提示用户失败并关机。

特点

用随机数来随机生成地图,增加了趣味性,可以不断切换地图(当然可能存在某个地图走不通的情况,这时候就要及时切换地图)

设置了关机程序(如果40秒不能走出迷宫?电脑就会自动关机?超刺激的

欢迎界面

游戏规则说明界面

游戏界面

完整代码

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<iostream>
#include<windows.h>
using namespace std;
#define WIDTH 25
#define HEIGH 25

int x,y;
int flag = 1;

int map[HEIGH][WIDTH];


void Welcome()
{
	for(int i=0;i<10;i++)
	{
		cout<<endl;
	}
	cout<<"			====================================================================="<<endl;
	cout<<"			=                                                                   ="<<endl;
	cout<<"			=                                                                   ="<<endl;
	cout<<"			=                                                                   ="<<endl;
	cout<<"			=                                走迷宫                             ="<<endl;
	cout<<"			=                                                                   ="<<endl;
	cout<<"			=                 在规定时间内走出迷宫                              ="<<endl;
	cout<<"			=                 否则会发生很恐怖的事                              ="<<endl;
	cout<<"			=                                                                   ="<<endl;
	cout<<"			====================================================================="<<endl;
	system("pause");
	system("cls");//清屏 用以换页 
}
void Rule()
{
	cout<<"================================================================"<<endl;
	cout<<"=                             规则                             ="<<endl;
	cout<<"=                1. wasd控制方向(小写 把输入法变成英文)      ="<<endl;
	cout<<"=                2.如果地图走不了记得用r换图                   ="<<endl;
	cout<<"=                3.☆是出口                                    ="<<endl; 
	cout<<"=                4.走出迷宫的时间一定要在40s内(不信就try try)  ="<<endl;
	cout<<"=                5.为了营造更紧张的氛围 请自己默数40s          ="<<endl; 
	cout<<"================================================================"<<endl;
	system("pause");
	system("cls"); 
}
void Draw()//创建地图  
{
	int i,j;

	for ( i=0; i<HEIGH; i++ )
	{
		for ( j=0; j<WIDTH; j++ )
		{
			if ( map[i][j] == 0 )
			{
				printf("  ");
			}
			if ( map[i][j] == 1 )
			{
				printf("■");
			}
			if ( map[i][j] == 2 )
			{
				printf("●");
			}
			if ( map[i][j] == 3 )
			{
				printf("☆");
			} 
		}
		printf("\n");
	}
}

void moveW()
{
		
	if(map[x-1][y]!=1)
	{
		map[x][y]=0;
		x--;
		map[x][y]=2;
	}
}
void moveS()
{

	if(map[x+1][y]!=1)
	{
		map[x][y]=0;
		x++;
		map[x][y]=2;
	}
}
void moveA()
{
		
	if(map[x][y-1]!=1)
	{
		map[x][y]=0;
		y--;
		map[x][y]=2;
	}
}
void moveD()
{
		
	if(map[x][y+1]!=1)
	{
		map[x][y]=0;
		y++;
		map[x][y]=2;
	}
}

void Play()
{
	char c;
	while ( flag )
	{
		system("cls");
		cout<<"◆输入r重新载入新地图◆"<<endl;
		Draw();
		
		
		c = getch();//判断是否输入 
		if ( x==HEIGH-2 && y==WIDTH-2 )//出口 判定成功 
		{
			flag = 0;
		}
		if ( c == 'r' )//重新加载地图 
		{
			for ( int i=1; i<HEIGH-1; i++ )
			{
				for ( int j=1; j<WIDTH-1; j++ )
				{
					map[i][j] = 0;
				}
			}
			for ( int i=0; i<WIDTH; i++ )
			{
				map[0][i] = 1;
				map[HEIGH-1][i] = 1;
			}
			for ( int i=0; i<HEIGH; i++ )
			{
				map[i][0] = 1;
				map[i][WIDTH-1] = 1;
			}
			srand((unsigned)time(NULL));
			for ( int i=0; i<200; i++ )
			{
				map[rand()%23+1][rand()%23+1] = 1;
			}
			map[1][1] = 2;
			map[HEIGH-2][WIDTH-2] = 3;
			x = 1;
			y = 1;
		}
	
		
		switch (c)
		{
			case 'w':
				moveW();
				break;
			case 's':
				moveS();
				break;
			case 'a':
				moveA();				
				break;
			case 'd':
				moveD();				
				break;
		}
	}
	
	
}

	
int main()
{
	Welcome();
	Rule();
	clock_t start,finish;
	double duration;
	start = clock();
	for ( int i=0; i<WIDTH; i++ )//上下封闭 
	{
		map[0][i] = 1;
		map[HEIGH-1][i] = 1;
	}
	for ( int i=0; i<HEIGH; i++ )//左右封闭 
	{
		map[i][0] = 1;
		map[i][WIDTH-1] = 1;
	}
	srand((unsigned)time(NULL));//随机数种子 
	for ( int i=0; i<200; i++ )//生成200个方块 
	{
		map[rand()%23+1][rand()%23+1] = 1;//随机在某个位置生成方块 
	}
	map[1][1] = 2;//初始位置 
	map[HEIGH-2][WIDTH-2] = 3;//出口 
	x = 1;
	y = 1;
	Play();
	finish = clock();
	duration = (double)((finish-start)/CLOCKS_PER_SEC);//记录总时间 
	
	if(duration>40)
	{
		system("cls");
		cout<<"你用了"<<duration<<"秒"<<endl; 
		cout<<"你的电脑将在30秒内关机!"<<endl;
		cout<<"你的电脑将在30秒内关机!"<<endl;
		cout<<"你的电脑将在30秒内关机!"<<endl;
		cout<<"重要的事情说三遍!!!(︶︿︶) "<<endl;
		system("shutdown -s -t 30");
		system("pause"); 
	}
	else
	{
		
		system("cls");
		printf("			   恭喜通过\n");
		system("pause"); 
	}
	
}

文章来源:https://blog.csdn.net/X_StarX/article/details/135595450
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。