#include #include #include #include #include #define Height 25 //迷宫的高度,必须为奇数 #define Width 25 //迷宫的宽度,必须为奇数 #define Wall 1 #define Road 0 #define Start 2 #define End 3 #define Esc 5 #define Up 1 #define Down 2 #define Left 3 #define Right 4 #define TS 6 #define N1 532 #define N2 588 #define N3 660 #define N4 698 #define N5 784 #define N6 880 #define N7 988 #define C 400 //一拍 #define D 200 #define E 100 int map[Height+2][Width+2]; void sung() { Beep(N1,C);Beep(N1,C);Beep(N5,C);Beep(N5,C);Beep(N6,C);Beep(N6,C);Beep(N5,C);Beep(0,C); Beep(N4,C);Beep(N4,C);Beep(N3,C);Beep(N3,C);Beep(N2,C);Beep(N2,C);Beep(N1,C);Beep(0,C); Beep(N5,C);Beep(N5,C);Beep(N4,C);Beep(N4,C);Beep(N3,C);Beep(N3,C);Beep(N2,C);Beep(0,C); Beep(N5,C);Beep(N5,C);Beep(N4,C);Beep(N4,C);Beep(N3,C);Beep(N3,C);Beep(N2,C);Beep(0,C); Beep(N1,C);Beep(N1,C);Beep(N5,C);Beep(N5,C);Beep(N6,C);Beep(N6,C);Beep(N5,C);Beep(0,C); Beep(N4,C);Beep(N4,C);Beep(N3,C);Beep(N3,C);Beep(N2,C);Beep(N2,C);Beep(N1,C);Beep(0,C); //小星星 Beep(N5,C);Beep(N4,C);Beep(N3,C);Beep(0,C);Beep(N3,C);Beep(N4,C);Beep(N3,C);Beep(0,C); Beep(N3,C);Beep(N4,C);Beep(N3,C);Beep(N4,C);Beep(N3,C);Beep(N2,C);Beep(N1,C);Beep(0,C); Beep(N1,C);Beep(N3,C);Beep(N5,C);Beep(N6,C);Beep(0,C);Beep(N6,C);Beep(N6,C);Beep(N5,C);Beep(N2,C);Beep(N2,C);Beep(N4,C);Beep(N3,C);Beep(0,C); } void gotoxy(int x,int y) //移动坐标 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); } void hidden()//隐藏光标 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut,&cci); cci.bVisible=0;//赋1为显示,赋0为隐藏 SetConsoleCursorInfo(hOut,&cci); } int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);//更改文字颜色 return 0; } void welcome(); void yxjm(); void game(); //生成进入界面 void title() { color(14); gotoxy(30,7); printf("欢迎来到"); color(5); gotoxy(36,8); printf("迷宫游戏"); color(12); printf("<-biubiu-?(`ω′∩)"); } void ajsm() { int n; color(8); gotoxy(30,9); printf("键盘‘↑’、‘↓’、‘←’、‘→’控制"); gotoxy(30,10); printf("按1继续,按2退出:[ ](按回车进入)\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); scanf("%d",&n); switch(n) { case 1: system("cls"); welcome(); break; case 2: exit(0); } } void yxgz() { int n; color(2); gotoxy(30,9); printf("控制你的小点来避开所有障碍,并到达最终终点吧!"); gotoxy(30,10); printf("按1继续,按2退出:[ ](按回车进入)\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); scanf("%d",&n); switch(n) { case 1: system("cls"); welcome(); break; case 2: exit(0); } } void welcome() { title(); int i,j=1; color(9); for(i=9;i<=20;i++) { for(j=15;j<=60;j++) { gotoxy(j,i); if(i==9||i==20) printf("="); else if(j==15||j==59) printf("::"); } } int n; color(12); gotoxy(25,12); printf("1.开始游戏"); gotoxy(40,12); printf("2.按键说明"); gotoxy(25,17); printf("3.游戏规则"); gotoxy(40,17); printf("4.退出"); gotoxy(21,22); printf("请选择[1,2,3,4]:[ ](按回车进入)\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); scanf("%d",&n); switch(n) { case 1: system("cls"); yxjm(); game(); //开始游戏 break; case 2: system("cls"); ajsm(); break; case 3: system("cls"); yxgz(); break; case 4: exit(0); break; } } void create(int x,int y) //随机生成迷宫 { int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向 int i,j,t; //将方向打乱 for(i=0;i<4;i++) { j=rand()%4; t=c[i][0]; c[i][0]=c[j][0]; c[j][0]=t; t=c[i][1]; c[i][1]=c[j][1]; c[j][1]=t; } map[x][y]=Road; for(i=0;i<4;i++) if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) { map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]); } } int get_key() //接收按键 { char c; while(c=getch()) { if(c==32) return TS; //提示 if(c==27) return Esc; //Esc if(c!=-32) continue; c=getch(); if(c==72) return Up; //上 if(c==80) return Down; //下 if(c==75) return Left; //左 if(c==77) return Right; //右 } return 0; } void paint(int x,int y) //画迷宫 { gotoxy(2*y-2,x-1); switch(map[x][y]) { case Start: printf("入");break; //画入口 case End: printf("出");break; //画出口 case Wall: printf("▇");break; //画墙 case Road: printf(" ");break; //画路 } } void game() { int x=2,y=1; //玩家当前位置,刚开始在入口处 int c; //用来接收按键 while(1) { gotoxy(2*y-2,x-1); printf("●"); //画出玩家当前位置 if(map[x][y]==End) //判断是否到达出口 { system("cls"); gotoxy(10,5); printf("请欣赏半分钟音乐(~ ̄▽ ̄)~ "); sung(); system("cls"); gotoxy(10,5); color(12); printf("胜利!ヾ(●゜ⅴ゜)/棒棒哒"); gotoxy(10,7); printf("按1返回大厅,按Esc退出游戏: (按回车进入)\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); scanf("%d",&c); switch(c) { case 1: system("cls"); welcome(); break; case 2: exit(0); break; } break; } c=get_key(); if(c==Esc) { exit(0); break; } if(c==TS) { break; } switch(c) { case Up: //向上走 if(map[x-1][y]!=Wall) { paint(x,y); x--; } else Beep(523,400); break; case Down: //向下走 if(map[x+1][y]!=Wall) { paint(x,y); x++; } else Beep(523,400); break; case Left: //向左走 if(map[x][y-1]!=Wall) { paint(x,y); y--; } else Beep(523,400); break; case Right: //向右走 if(map[x][y+1]!=Wall) { paint(x,y); y++; } else Beep(523,400); break; } } } void yxjm() { color(14); system("title 很棒的迷宫游戏吖(〃'▽'〃"); int i,j; srand((unsigned)time(NULL)); //初始化随即种子 hidden(); //隐藏光标 for(i=0;i<=Height+1;i++) for(j=0;j<=Width+1;j++) if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫 map[i][j]=Road; else map[i][j]=Wall; create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数 for(i=0;i<=Height+1;i++) //边界处理 { map[i][0]=Wall; map[i][Width+1]=Wall; } for(j=0;j<=Width+1;j++) //边界处理 { map[0][j]=Wall; map[Height+1][j]=Wall; } map[2][1]=Start; //给定入口 map[Height-1][Width]=End; //给定出口 for(i=1;i<=Height;i++) for(j=1;j<=Width;j++) //画出迷宫 paint(i,j); color(12); gotoxy(52,10); printf("按Esc退出游戏"); gotoxy(52,11); printf("按空格获得提示"); } int main(void) { welcome(); return 0; }