C语言实例教程:一个迷宫游戏(2)
我也来发一个
也是迷宫的
/********************************************迷宫V1.9*****************************************/
/*下面两个二维数组是迷宫的一,二层。*/
char str[22][28]={{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
{'x',' ','B','x',' ','s',' ',' ','B',' ','e','x','t',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B',' ','f','x'},
{'x','t','B','x',' ','x','x','x','x',' ','x','x','e','x','x','x','x','x','x','x',' ','x',' ',' ','B',' ','f','x'},
{'x',' ','x',' ',' ',' ',' ','x',' ',' ','x','x','x','x','x','x','x','x','x','x',' ','x',' ',' ','B','B','f','x'},
{'x',' ','x',' ','x','x',' ','x',' ','x',' ','t','B','x','x','x','x','x','x','e',' ','x',' ',' ','B',' ','f','x'},
{'x',' ','x',' ','x',' ','s','x',' ','x','x','x','B','t',' ',' ',' ',' ','x','x',' ','x','x','x','x',' ','f','x'},
{'x',' ','x',' ','x',' ','x','x','f','x',' ','t','B','x','x','x','x',' ','x','x',' ','x','x','x','x','x','x','x'},
{'x',' ','x',' ','x',' ','x','x',' ','x',' ','x','x','x','e',' ','x','s',' ','x',' ','x','x','x','x','f','f','x'},
{'x',' ',' ',' ','x',' ',' ','x',' ','x',' ',' ',' ','e','x',' ','x',' ','x','x',' ',' ',' ','t',' ','e','B','x'},
{'x','x','x','x','x','x',' ','x',' ','x',' ','x','x','x','x',' ',' ',' ','x','x',' ','x','x','x',' ','f','f','x'},
{'x',' ',' ','x',' ',' ',' ','x',' ','f','s','x',' ',' ',' ',' ','x','x','x','x',' ','x',' ','x',' ','x','x','x'},
{'x',' ','s','x',' ','x','x','x','x','x','x','x',' ','x','x','x','x','s',' ',' ',' ','x',' ','x',' ','t','t','x'},
{'x',' ','x',' ',' ',' ',' ','x',' ',' ',' ','x','t','e','t',' ','x','x','x','x',' ','x',' ','x',' ','t','t','x'},
{'x',' ','x',' ','x','x',' ','x',' ','x',' ','x',' ','t',' ',' ','x',' ',' ',' ',' ','x',' ','x',' ','s',' ','x'},
{'x',' ','x','t',' ','x',' ','x',' ','x','x','x','x','x','x',' ','x',' ','x','x',' ','x',' ','x','t','s','t','x'},
{'x',' ','x','f','f','x',' ','x',' ','x',' ',' ',' ',' ','x',' ','x',' ','x','x',' ','x',' ','x','s','s','s','x'},
{'x',' ','x',' ','o','x',' ','x',' ','x',' ','x','x',' ','x',' ','x',' ','x',' ',' ','x',' ','x','t','s','s','x'},
{'x',' ','x','x','x','x',' ','x',' ','x',' ',' ','x',' ',' ',' ','x',' ','x','e','t','x',' ','x','x','x','x','x'},
{'x',' ',' ',' ',' ',' ',' ','x',' ','x',' ','t','x','x',' ','x','x',' ',' ','x','f','x',' ',' ',' ',' ',' ',' '},
{'x','s','x','x','x','x','x','x',' ','x','t','f','x','x',' ','x','x','x',' ','x','x','x',' ',' ',' ',' ',' ',' '},
{'x',' ',' ','t',' ',' ','B','x',' ',' ',' ','t','e','t',' ',' ',' ',' ',' ',' ','a','x',' ',' ',' ',' ',' ',' '},
{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x',' ',' ',' ',' ',' ',' '}};
char str2[30][26]={{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
{'x',' ',' ',' ',' ',' ','x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',' ',' ',' ',' ',' ','x'},
{'x',' ',' ',' ',' ','x',' ','x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',' ','x',' ',' ',' ',' ','x'},
{'x',' ',' ',' ','x',' ','x',' ','x',' ',' ',' ',' ',' ',' ',' ',' ','x',' ','x',' ','x',' ',' ',' ','x'},
{'x',' ',' ','x','q','x',' ','x',' ','x','x','x','x','x','x','x','x',' ','x',' ','x',' ','x',' ',' ','x'},
{'x',' ',' ','x',' ',' ',' ',' ','B','B',' ','B',' ','B',' ','B','B',' ',' ',' ',' ',' ','x',' ',' ','x'},
{'x',' ','x',' ',' ',' ',' ',' ','d','B',' ','B','p','B',' ','B','B',' ','x','x','x',' ',' ','x',' ','x'},
{'x','x',' ',' ',' ',' ',' ',' ',' ',' ',' ','B','B','B',' ',' ',' ',' ',' ',' ','x','x',' ','C','x','x'},
{'x','x',' ',' ',' ',' ',' ',' ','s','s','s',' ',' ',' ',' ',' ',' ','x','x','x',' ',' ','f','S','x','x'},
{'x','x','x','t','t','b',' ','s',' ','e',' ','s',' ',' ',' ',' ','x',' ','e',' ','x','x','x','B','x','x'},
{'x','x',' ',' ',' ','x','t','s','e','e','e','t','t',' ',' ','t','x','e','e','e','t',' ',' ','m','x','x'},
{'x','x',' ',' ','x','t',' ','s',' ',' ','e','t',' ','x','!',' ','x','e','o','e','x','x',' ',' ','x','x'},
{'x','x','s','s',' ',' ','t','t',' ','e',' ','t','t',' ',' ','t','x',' ','e',' ','x',' ','x','x','x','x'},
{'x','x','s','s','x','x',' ',' ','t','t','t','t',' ','s','s',' ',' ','x','x','x','t','t','x','x','x','x'},
{'x','x',' ',' ',' ','x',' ',' ',' ',' ',' ',' ','s','s','s','s',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
{'x','x',' ',' ','t',' ',' ',' ','e','e','s',' ',' ','t','t',' ',' ','e','s','s',' ','x',' ','h','x','x'},
{'x',' ','x','x',' ',' ',' ',' ',' ',' ','s',' ','t',' ',' ','t',' ','e',' ',' ',' ',' ','t','x',' ','x'},
{'x',' ',' ','x',' ',' ',' ',' ',' ','s',' ','t','f',' ',' ','c','t',' ','e',' ',' ',' ','x',' ',' ','x'},
{'x',' ',' ',' ','t',' ',' ',' ','s',' ',' ',' ','t',' ',' ','f',' ',' ',' ','s',' ','x',' ',' ',' ','x'},
{'x',' ',' ','t',' ','t',' ',' ',' ',' ',' ',' ',' ','t','f',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
{'x',' ','t',' ',' ',' ','t','x','x','x','x','x','x','x','x','x','x','x','x','x',' ',' ',' ','x','x','x'},
{'x','t',' ',' ',' ',' ',' ','x',' ','x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',' ','x','x'},
{'x','t',' ',' ',' ',' ',' ','x',' ','x',' ','x',' ','x',' ','x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
{'x','t',' ','s','s',' ',' ','x','t','x','t','x','t','x','t','x','t','x','t','t','x',' ',' ',' ','x','x'},
{'x','t',' ','s','s',' ',' ','e','e','e','e','e','e','e','e','e','e','e','e','e',' ',' ',' ','a','x','x'},
{'x','t',' ',' ',' ',' ',' ','x','t','x','t','x','t','x','t','x','t','x','t','t','x',' ',' ',' ','x','x'},
{'x',' ','t',' ',' ',' ',' ','x',' ','x',' ','x',' ','x',' ','x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
{'x',' ',' ','t',' ',' ',' ','x',' ','x',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x',' ','x','x'},
{'x',' ',' ',' ','t','t','t',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','x','x','x'},
{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'}};
int e=20,f=20,p=0,h=0;/*e,f是迷宫中a的坐标*/
int hssl(int o)//判断海市蜃楼的出现。
{
if(o%3==0||f>20)
return 28;
else
return 22;
}
void clean()//清屏并输出第一层迷宫。
{
int j,i;
system("cls");
for(i=0;i<22;i++)
{
if(i>0)
printf("\n");
for(j=0;j<hssl(p);j++)
{
printf("%c ",str[i][j]);
}
}
printf("\nWASD移动,Q查询分数与浮木,E查看游戏规则");
}
void clean2()//清屏并输出第二层迷宫。
{
int j,i;
system("cls");
for(i=0;i<30;i++)
{
if(i>0)
printf("\n");
for(j=0;j<26;j++)
{
printf("%c ",str2[i][j]);
}
}
printf("\nWASD移动,Q查询分数与浮木,E查看游戏规则");
}
void wenzi()
{
system("cls");
printf("这是一个迷宫\na是你自己\no是出口\nx是墙壁\ne,f,B分别加1分,2分,10分\nt是沼泽,走过去的话会沉下去\ns是浮木,对付沼泽用\nS是山崩,C是车祸现场,不能通过\nh是铲土机,q是交警叔叔,他们分别对付山崩和车祸现场\n!是强盗,他会抢光你的分数和浮木\np是警察叔叔,对付强盗\nb是迷路的小孩,送他回家会得到分数\nm是迷路的小孩的家\nc是传送地板入口,d是传送地板出口\n还有一件事,20步内必须吃到分数,以平息那些刚搬来的兔子的愤怒。\n");
getch();
if(h==0)clean();
else clean2();
}
/******************************主函数*********************************/
main()
{
int i,j,r=0,bs=0;
int q=0,w=0,d,x,chan=0,police=0,lup=0,boy=0;
char b[4]={' ',' ',' ',' '};
wenzi();
/************************以每一步作为一个循环*******************************/
while(1)
{b[q]=getch();
/*********************判断输入的键位****************************/
if(h==0)
{
switch(b[q])
{
case 'a':d=str[e][f-1];str[e][f-1]=str[e][f];str[e][f]=d;f=f-1;clean();x=1;bs++;break;
case 's':d=str[e+1][f];str[e+1][f]=str[e][f];str[e][f]=d;e=e+1;clean();x=2;bs++;break;
case 'd':d=str[e][f+1];str[e][f+1]=str[e][f];str[e][f]=d;f=f+1;clean();x=3;bs++;break;
case 'w':d=str[e-1][f];str[e-1][f]=str[e][f];str[e][f]=d;e=e-1;clean();x=4;bs++;break;
case 'A':d=str[e][f-1];str[e][f-1]=str[e][f];str[e][f]=d;f=f-1;clean();x=1;bs++;break;
case 'S':d=str[e+1][f];str[e+1][f]=str[e][f];str[e][f]=d;e=e+1;clean();x=2;bs++;break;
case 'D':d=str[e][f+1];str[e][f+1]=str[e][f];str[e][f]=d;f=f+1;clean();x=3;bs++;break;
case 'W':d=str[e-1][f];str[e-1][f]=str[e][f];str[e][f]=d;e=e-1;clean();x=4;bs++;break;
case 'q':printf("\n你的分数为%d,你的浮木数为%d。\n",w,r);break;
case 'Q':printf("\n你的分数为%d,你的浮木数为%d。\n",w,r);break;
case 'e':wenzi();break;
case 'E':wenzi();break;
default:;
}
}
else if(h==1)
{
switch(b[q])
{
case 'a':d=str2[e][f-1];str2[e][f-1]=str2[e][f];str2[e][f]=d;f=f-1;clean2();x=1;bs++;break; case 's':d=str2[e+1][f];str2[e+1][f]=str2[e][f];str2[e][f]=d;e=e+1;clean2();x=2;bs++;break;
case 'd':d=str2[e][f+1];str2[e][f+1]=str2[e][f];str2[e][f]=d;f=f+1;clean2();x=3;bs++;break;
case 'w':d=str2[e-1][f];str2[e-1][f]=str2[e][f];str2[e][f]=d;e=e-1;clean2();x=4;bs++;break;
case 'A':d=str2[e][f-1];str2[e][f-1]=str2[e][f];str2[e][f]=d;f=f-1;clean2();x=1;bs++;break;
case 'S':d=str2[e+1][f];str2[e+1][f]=str2[e][f];str2[e][f]=d;e=e+1;clean2();x=2;bs++;break;
case 'D':d=str2[e][f+1];str2[e][f+1]=str2[e][f];str2[e][f]=d;f=f+1;clean2();x=3;bs++;break;
case 'W':d=str2[e-1][f];str2[e-1][f]=str2[e][f];str2[e][f]=d;e=e-1;clean2();x=4;bs++;break;
case 'q':printf("\n你的分数为%d,你的浮木数为%d。\n",w,r);break;
case 'Q':printf("\n你的分数为%d,你的浮木数为%d。\n",w,r);break;
case 'e':wenzi();break;
case 'E':wenzi();break;
default:;
}
}
//判断a踩着什么了。
/**************************判断第一层*************************/
if(h==0)
{
if(x==1)
{
if(str[e][f+1]=='x')
{
d=str[e][f+1];
str[e][f+1]=str[e][f];
str[e][f]=d;
f=f+1;
clean();
printf("\n撞到了\n");
}
else if(str[e][f+1]=='e')
{
str[e][f+1]=' ';
clean();
printf("\n加1分\n");
w++;
}
else if(str[e][f+1]=='o')
{
str[e][f+1]=' ';
clean();
printf("\n下一层\n");
e=24;
f=23;
clean2();
h++;
printf("\n请最大化");
}
else if(str[e][f+1]=='f')
{
str[e][f+1]=' ';
clean();
printf("\n加2分\n");
w=w+2;
}
else if(str[e][f+1]=='B')
{
str[e][f+1]=' ';
clean();
printf("\n加10分\n");
w=w+10;
}
else if(str[e][f+1]=='t')
{
str[e][f+1]=' ';
clean();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str[e][f+1]=='s')
{
str[e][f+1]=' ';
clean();
printf("\n捡到浮木!\n");
r++;
}
else;
}
else if(x==2)
{
if(str[e-1][f]=='x')
{
d=str[e-1][f];
str[e-1][f]=str[e][f];
str[e][f]=d;
e=e-1;
clean();
printf("\n撞到了\n");
}
else if(str[e-1][f]=='e')
{
str[e-1][f]=' ';
clean();
printf("\n加1分\n");
w++;
}
else if(str[e-1][f]=='o')
{
str[e-1][f]=' ';
clean();
printf("\n下一层\n");
e=24;
f=23;
clean2();
h++;
printf("\n请最大化");
}
else if(str[e-1][f]=='f')
{
str[e-1][f]=' ';
clean();
printf("\n加2分\n");
w=w+2;
}
else if(str[e-1][f]=='B')
{
str[e-1][f]=' ';
clean();
printf("\n加10分\n");
w=w+10;
}
else if(str[e-1][f]=='t')
{
str[e-1][f]=' ';
clean();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str[e-1][f]=='s')
{
str[e-1][f]=' ';
clean();
printf("\n捡到浮木!\n");
r++;
}
else;
}
else if(x==3)
{
if(str[e][f-1]=='x')
{
d=str[e][f-1];
str[e][f-1]=str[e][f];
str[e][f]=d;
f=f-1;
clean();
printf("\n撞到了\n");
}
else if(str[e][f-1]=='e')
{
str[e][f-1]=' ';
clean();
printf("\n加1分\n");
w++;
}
else if(str[e][f-1]=='o')
{
str[e][f-1]=' ';
clean();
printf("\n下一层\n");
e=24;
f=23;
clean2();
h++;
printf("\n请最大化");
}
else if(str[e][f-1]=='f')
{
str[e][f-1]=' ';
clean();
printf("\n加2分\n");
w=w+2;
}
else if(str[e][f-1]=='B')
{
str[e][f-1]=' ';
clean();
printf("\n加10分\n");
w=w+10;
}
else if(str[e][f-1]=='t')
{
str[e][f-1]=' ';
clean();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str[e][f-1]=='s')
{
str[e][f-1]=' ';
clean();
printf("\n捡到浮木!\n");
r++;
}
else;
}
else if(x==4)
{
if(str[e+1][f]=='x')
{
d=str[e+1][f];
str[e+1][f]=str[e][f];
str[e][f]=d;
e=e+1;
clean();
printf("\n撞到了\n");
}
else if(str[e+1][f]=='e')
{
str[e+1][f]=' ';
clean();
printf("\n加1分\n");
w++;
}
else if(str[e+1][f]=='o')
{
str[e+1][f]=' ';
clean();
printf("\n下一层\n");
e=24;
f=23;
clean2();
h++;
printf("\n请最大化");
}
else if(str[e+1][f]=='f')
{
str[e+1][f]=' ';
clean();
printf("\n加2分\n");
w=w+2;
}
else if(str[e+1][f]=='B')
{
str[e+1][f]=' ';
clean();
printf("\n加10分\n");
w=w+10;
}
else if(str[e+1][f]=='t')
{
str[e+1][f]=' ';
clean();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str[e+1][f]=='s')
{
str[e+1][f]=' ';
clean();
printf("\n捡到浮木!\n");
r++;
}
else;
}
}
/******************************判断第二层*****************************/
else if(h==1)/********************************************************************/
{
if(x==1)
{
if(str2[e][f+1]=='x')
{
d=str2[e][f+1];
str2[e][f+1]=str2[e][f];
str2[e][f]=d;
f=f+1;
clean2();
printf("\n撞到了\n");
}
else if(str2[e][f+1]=='e')
{
str2[e][f+1]=' ';
clean2();
printf("\n加1分\n");
w++;
}
else if(str2[e][f+1]=='o')
{
str2[e][f+1]=' ';
clean2();
printf("\n你赢了\n");
break;
}
else if(str2[e][f+1]=='f')
{
str2[e][f+1]=' ';
clean2();
printf("\n加2分\n");
w=w+2;
}
else if(str2[e][f+1]=='B')
{
str2[e][f+1]=' ';
clean2();
printf("\n加10分\n");
w=w+10;
}
else if(str2[e][f+1]=='t')
{
str2[e][f+1]=' ';
clean2();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str2[e][f+1]=='s')
{
str2[e][f+1]=' ';
clean2();
printf("\n捡到浮木!\n");
r++;
}
else if(str2[e][f+1]=='c')
{
d=str2[e][f];
str2[e][f]=str2[6][8];
str2[6][8]=d;
str2[e][f+1]=' ';
str2[e][f]=' ';
e=6;
f=8;
clean2();
}
else if(str2[e][f+1]=='S')
{
if(chan==0)
{
d=str2[e][f+1];
str2[e][f+1]=str2[e][f];
str2[e][f]=d;
f=f+1;
clean2();
printf("\n山崩地区,此路不通");
}
else if(chan==1)
{
str2[e][f+1]=' ';
clean2();
}
else;
}
else if(str2[e][f+1]=='C')
{
if(lup==0)
{
d=str2[e][f+1];
str2[e][f+1]=str2[e][f];
str2[e][f]=d;
f=f+1;
clean2();
printf("\n车祸现场,此路不通");
}
else if(lup==1)
{
str2[e][f+1]=' ';
clean2();
}
}
else if(str2[e][f+1]=='!')
{
if(police==0)
{
str2[e][f+1]=' ';
clean2();
w=0;
r=0;
printf("\n嗯······要钱还是要命?······要命?好!把钱交出来!那些浮木也拿出来!\n");
}
else if(police==1)
{
str2[e][f+1]=' ';
clean2();
w=w+10;
printf("\n感谢您帮我抓了盗贼,这是回报,就十分,请收下!\n");
}
}
else if(str2[e][f+1]=='h')
{
str2[e][f+1]=' ';
clean2();
printf("\n得到铲土机!\n");
chan++;
}
else if(str2[e][f+1]=='p')
{
str2[e][f+1]=' ';
clean2();
printf("\n有强盗?我会帮助你的。\n");
police++;
}
else if(str2[e][f+1]=='q')
{
str2[e][f+1]=' ';
clean2();
printf("\n车祸了,这年头事儿真多。\n");
lup++;
}
else if(str2[e][f+1]=='b')
{
str2[e][f+1]=' ';
clean2();
printf("\n出现了?!迷路的小孩?送那个小孩回家会得到20分\n");
boy++;
}
else if(str2[e][f+1]=='m')
{
if(boy>0)
{
str2[e][f+1]=' ';
clean2();
printf("\n小孩到家了\n");
boy--;
w=w+20;
}
else
{
d=str2[e][f+1];
str2[e][f+1]=str2[e][f];
str2[e][f]=d;
f=f+1;
clean2();
printf("\n竟敢私闯宅屋,滚!\n");
}
}
else;
}
else if(x==2)/**************************************/
{
if(str2[e-1][f]=='x')
{
d=str2[e-1][f];
str2[e-1][f]=str2[e][f];
str2[e][f]=d;
e=e-1;
clean2();
printf("\n撞到了\n");
}
else if(str2[e-1][f]=='e')
{
str2[e-1][f]=' ';
clean2();
printf("\n加1分\n");
w++;
}
else if(str2[e-1][f]=='o')
{
str2[e-1][f]=' ';
clean2();
printf("\n你赢了\n");
break;
}
else if(str2[e-1][f]=='f')
{
str2[e-1][f]=' ';
clean2();
printf("\n加2分\n");
w=w+2;
}
else if(str2[e-1][f]=='B')
{
str2[e-1][f]=' ';
clean2();
printf("\n加10分\n");
w=w+10;
}
else if(str2[e-1][f]=='t')
{
str2[e-1][f]=' ';
clean2();
if(r>=1)
r--;
else
{
printf("\n掉沼泽里了······\n可怜的人,");
break;
}
}
else if(str2[e-1][f]=='s')
{
str2[e-1][f]=' ';
clean2();
printf("\n捡到浮木!\n");
r++;
}
else if(str2[e-1][f]=='c')
{
d=str2[e][f];
str2[e][f]=str2[6][8];
str2[6][8]=d;
str2[e-1][f]=' ';
str2[e][f]=' ';
e=6;
f=8;
clean2();
}
else if(str2[e-1][f]=='S')
{
if(chan==0)
{
d=str2[e-1][f];
str2[e-1][f]=str2[e][f];
str2[e][f]=d;
e=e-1;
clean2();
printf("\n山崩地区,此路不通");
}
else if(chan==1)
{
str2[e-1][f]=' ';
clean2();
}
else;
}
else if(str2[e-1][f]=='C')
{
if(lup==0)
{
d=str2[e-1][f];
str2[e-1][f]=str2[e][f];
str2[e][f]=d;
e=e-1;
clean2();
printf("\n车祸现场,此路不通");
}
else if(lup==1)
{
str2[e-1][f]=' ';
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |