专业东莞网站建设报价,做五金的网站,好的产品设计网站,seo流量排名门户一个用c写的黑框框迷宫
适合新手入门学习
也适合大学生小作业
下面附上代码
总体思路
初始化游戏界面#xff1a;设置迷宫的大小#xff08;WIDTH和HEIGH#xff09;#xff0c;生成迷宫地图#xff08;map#xff09;#xff0c;包括墙壁、空地、起点和终点。显示…一个用c写的黑框框迷宫
适合新手入门学习
也适合大学生小作业
下面附上代码
总体思路
初始化游戏界面设置迷宫的大小WIDTH和HEIGH生成迷宫地图map包括墙壁、空地、起点和终点。显示欢迎界面和游戏规则通过Welcome()函数和Rule()函数分别实现。开始计时记录游戏开始的时间。游戏主循环在Play()函数中不断获取用户输入上下左右键根据输入移动角色直到到达出口或超时。判断游戏结果如果角色成功走出迷宫且在规定时间内显示恭喜信息否则提示用户失败并关机。
特点
用随机数来随机生成地图增加了趣味性可以不断切换地图当然可能存在某个地图走不通的情况这时候就要及时切换地图
设置了关机程序如果40秒不能走出迷宫 电脑就会自动关机 超刺激的 欢迎界面 游戏规则说明界面 游戏界面 完整代码
#includestdio.h
#includeconio.h
#includestdlib.h
#includestring.h
#includetime.h
#includeiostream
#includewindows.h
using namespace std;
#define WIDTH 25
#define HEIGH 25int x,y;
int flag 1;int map[HEIGH][WIDTH];void Welcome()
{for(int i0;i10;i){coutendl;}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()
{coutendl;cout 规则 endl;cout 1. wasd控制方向小写 把输入法变成英文 endl;cout 2.如果地图走不了记得用r换图 endl;cout 3.☆是出口 endl; cout 4.走出迷宫的时间一定要在40s内(不信就try try) endl;cout 5.为了营造更紧张的氛围 请自己默数40s endl; coutendl;system(pause);system(cls);
}
void Draw()//创建地图
{int i,j;for ( i0; iHEIGH; i ){for ( j0; jWIDTH; 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[x1][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][y1]!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 ( xHEIGH-2 yWIDTH-2 )//出口 判定成功 {flag 0;}if ( c r )//重新加载地图 {for ( int i1; iHEIGH-1; i ){for ( int j1; jWIDTH-1; j ){map[i][j] 0;}}for ( int i0; iWIDTH; i ){map[0][i] 1;map[HEIGH-1][i] 1;}for ( int i0; iHEIGH; i ){map[i][0] 1;map[i][WIDTH-1] 1;}srand((unsigned)time(NULL));for ( int i0; i200; i ){map[rand()%231][rand()%231] 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 i0; iWIDTH; i )//上下封闭 {map[0][i] 1;map[HEIGH-1][i] 1;}for ( int i0; iHEIGH; i )//左右封闭 {map[i][0] 1;map[i][WIDTH-1] 1;}srand((unsigned)time(NULL));//随机数种子 for ( int i0; i200; i )//生成200个方块 {map[rand()%231][rand()%231] 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(duration40){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); }}