You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
6.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<windows.h>
//变量声明
#define M 100
#define N 100
#define Road 1
#define Wall 0
#define Des 3
#define Player 4
//定义光标位置
COORD coord; /*光标地址变量*/
short map[N][M];/*地图 */
int x,y;/*人物坐标 */
int m=21,n=21;/*实际地图大小 */
int flag; /*
控制界面切换
0主菜单
1游戏界面
2结束游戏
3退出游戏
*/
//函数声明
void mune();
void print();
void Print(char);
void up();
void down();
void right();
void left();
void gotc();
void Map_1();
void xy();
void HideCursor();
void chu();
char des(int,int);
void dig(char,int,int);
void make(int,int);
void CreateMaze();
void win();
void end();
//主函数
int main()
{
HideCursor();
system("title 迷宫游戏");
flag=0;
while(1){
if(flag==0) mune();
if(flag==1){
coord.X = 2;
coord.Y = 1;
xy(); //将屏幕指针位置移向初始位置
if(n==21) system("mode con cols=42 lines=22");
else if(n==31) system("mode con cols=62 lines=32");
else if(n==41) system("mode con cols=82 lines=42");
print(n,m);
gotc();
}
if(flag==2){
win();
flag=0;
}
if(flag==3){
end();
break;
}
}
return 0;
}
//函数定义
void print()/*输出界面*/
{
system("color F5");
system("cls");
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)
{
if(map[i][j]==Road) printf(" "); /*1 路*/
else if(map[i][j]==Wall) printf("");/*0 墙*/
else if(map[i][j]==Des) printf("");/*3 终点*/
else if(map[i][j]==Player) printf("");/*4 人物*/
}
printf("\n");
}
}
/*控制移动*/
void up()
{
if(map[x-1][y]==3) flag=2;
if(map[x-1][y]==1){
xy(x,y);
printf(" ");
map[x-1][y]=4;
map[x][y]=1;
x--;
coord.Y--; xy();
printf("");
}
}
void down()
{
if(map[x+1][y]==3) flag=2;
if(map[x+1][y]==1){
xy(x,y);
printf(" ");
map[x+1][y]=4;
map[x][y]=1;
x++;
coord.Y++; xy();
printf("");
}
}
void right()
{
if(map[x][y+1]==3) flag=2;
if(map[x][y+1]==1){
xy(x,y);
printf(" ");
map[x][y+1]=4;
map[x][y]=1;
y++;
coord.X+=2; xy();
printf("");
}
}
void left()
{
if(map[x][y-1]==3) flag=2;
if(map[x][y-1]==1){
xy(x,y);
printf(" ");
map[x][y-1]=4;
map[x][y]=1;
y--;
coord.X-=2; xy();
printf("");
}
}
/*主菜单*/
void mune()
{
system("color F6");
system("mode con cols=50 lines=15");
x=1;y=1; flag=0;
system("cls");
printf("\n\n\n 迷宫游戏\n\n");
printf(" 主菜单\n");
printf(" 1:更改难度 {难度等级:1(默认)/2/3}\n");
printf(" 2:开始游戏\n");
printf(" 3:退出游戏\n");
printf(" 0:退回菜单\n");
printf("\nTips:\n请使用英文(半角)输入 游戏中回到菜单会断档\n请用WASD控制移动 ◆:玩家 ◎:终点") ;
while(1){
switch(getch())
{
case '3': flag=3;break;
case '2': CreateMaze();flag=1;break;
default: break;
case '1':{
coord.X = 0;
coord.Y = 14;
xy();printf(" ");
xy();
printf("请输入难度:");
switch(getch()){
case '1':n=21; m=21;printf("难度等级1修改成功");break;
case '2':n=31; m=31;printf("难度等级2修改成功");break;
case '3':n=41; m=41;printf("难度等级3修改成功");break; //...
default: printf("输入错误,修改失败!");
}
break;
}
}
if(flag==1||flag==2||flag==3) break;
}
}
/*玩家输入控制*/
void gotc()
{
while(1)
{
flag=1;
switch(getch())
{
case '*': flag=4;break;
case '3': flag=3;break;
case '0': flag=0;break;
case 'w':case 'W': Print('w');break;
case 'a':case 'A': Print('a');break;
case 's':case 'S': Print('s');break;
case 'd':case 'D': Print('d');break;
}
if(flag==0||flag==2||flag==3||flag==4) break;
}
}
/*更新地图*/
void Print(char d)
{
xy(x,y);
switch(d){
case 'W':case 'w':up();break;
case 'S':case 's':down();break;
case 'A':case 'a':left();break;
case 'D':case 'd':right();break;
}
}
/*定位光标*/
void xy(){
//获取控制台缓冲区句柄
HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置光标位置
SetConsoleCursorPosition(ConsoleHandle, coord);
}
/*隐藏屏幕指针 */
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void win()
{
system("cls");
system("color 73");
system("mode con cols=38 lines=13");
printf("\n █ █ █████ █ █ ");
printf("\n █ █ █ █ █ █ ");
printf("\n █ █ █ █ █ ");
printf("\n █ █ █ █ █ ");
printf("\n █ █████ █████ \n");
printf("\n █ █ █ ███ █ █ ");
printf("\n █ █ █ █ ██ █ ");
printf("\n █ █ █ █ █ █ █ ");
printf("\n ██ ██ █ █ ██ ");
printf("\n █ █ ███ █ █ ");
system("pause");
}
void end()
{
system("mode con cols=50 lines=15");
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\n 谢谢使用!\n\n\n\n");
printf(" 计算机1812班\n 184018001207\n 向理\n") ;
}
//以下为地图生成函数
/*准备生成的初始化*/
void chu()
{
int i,j;
for(i=0;i<n;i++)for(j=0;j<m;j++){
if(i%2==1&&j%2==1) map[i][j]=1;
else map[i][j]=0;
}
i=rand()%(n-3);
j=rand()%(m-3);
if(i%2==0) i++;
if(j%2==0) j++;
map[i][j]=2;
make(i,j);
}
//得到方位且可判断是否有有效邻居(可挖掘的)
char des(int x1,int y1)
{
char a[5];
int k=0;
if(x1>1&&map[x1-2][y1]==1) a[k++]='w';
if(x1<n-2&&map[x1+2][y1]==1) a[k++]='s';
if(y1>1&&map[x1][y1-2]==1) a[k++]='a';
if(y1<m-2&&map[x1][y1+2]==1) a[k++]='d';
if(k==0) return 'e';
return a[rand()%k];
}
//根据方向挖掘道路
void dig(char d,int x2,int y2)
{
switch(d){
case 'w':map[x2-1][y2]=2;map[x2-2][y2]=2;break;
case 's':map[x2+1][y2]=2;map[x2+2][y2]=2;break;
case 'a':map[x2][y2-1]=2;map[x2][y2-2]=2;break;
case 'd':map[x2][y2+1]=2;map[x2][y2+2]=2;break;
}
}
//生成地图的递归
void make(int x3,int y3)
{
char d;
while(des(x3,y3)!='e'){
d=des(x3,y3);
dig(d,x3,y3);
switch(d){
case 'w':make(x3-2,y3);break;
case 's':make(x3+2,y3);break;
case 'a':make(x3,y3-2);break;
case 'd':make(x3,y3+2);break;
}
}
}
//生产地图的主要函数
void CreateMaze()
{
int i,j;
srand(time(NULL));
chu();
for(i=0;i<n;i++)for(j=0;j<m;j++){
if(i==0||j==0||i==n-1||j==m-1) map[i][j]=Wall;
if(map[i][j]==2) map[i][j]=Road;
}
map[1][1]=Player;
map[n-2][m-2]=Des;
}