迷宫游戏设计思路与功能体会及改进报告
学号:150809333
一、 内容:
1、 本游戏主要实现了人控制键盘方向键使小人(*)走出迷宫。
2、 具有的功能:
1)、在游戏菜单里人可以选择不同难度的游戏进行游戏;
2)、在游戏过程中,可以通过键盘方向键使小人移动,走出迷宫;
3)、在游戏过程中,当人碰到墙壁(#)的时候小人过不去;
4)、当人顺利完成游戏之后,输出“========you arewin!======”字样,30秒钟后自动返回
到游戏菜单;
5)、在游戏过程中,人可以通过按Esc键返回游戏菜单;也可以可以按0直接退出游戏;
6)、在游戏菜单里,按0键可以退出游戏。
3、具体应用:
1)、人主要同过键盘的1,2,3数字键来选择游戏难度;
2)、在游戏中通过Esc键来返回菜单;
3)、同过0键退出游戏。
二、 上机环境
操作系统:windows10
开发工具:C-Free
三、 各函数功能说明
main() 主函数;
menu() 游戏菜单;
roadcake() 消去小人路径;
introduce() 游戏介绍;
system(“cls”) 消屏函数;
exit(0) 退出游戏;
drawmg1() 画简单难度迷宫;
drawmg2() 画中等难度迷宫;
drawmg3() 画困难难度迷宫;
control1() 控制简单难度游戏;
control2() 控制中等难度游戏;
control3() 控制困难难度游戏;
四、 算法流程图
首先定义三个全局数组mg1[20][20]、mg2[30][30]、mg3[30][30]用于画出迷宫的地图;1表示墙(#),0表示空地( );
Introduce( )函数里如果按Enter键,则调用menu()函数,从键盘中输入相应的提示数字,进入难度不同的游戏;游戏的执行在此只初级难度进行描述,其余的难度与其类似;
选了1后调用system(”cls”)进行清屏;drawmg1()函数进行迷宫的地图的绘制,for(i=0;i<20;i++)
{
printf("\t\t\t");
for(j=0;j<20;j++)
{
ch=mg1[i][j];
switch(ch)
{
case4: printf("*");break;
case0: printf(" ");break;
case1: printf("#");break;
}
if(num++%20==0) /*每行只能输出20个字符*/
printf(”\n”);
}
}
之后调用控制函数control1()进行控制小人移动路径,在control1()函数里用do—while()语句进行循环,while(1)进行死循环,再定义四个整型a,b,m,n即:
for(n=0;n<30;n++)
for(m=0;m<30;m++)
if(mg3[n][m]==4)
{
a=n; /*为后面调用roadcake()实现消除小人路径进行初始赋值*/
b=m;break;
}
用switch()语句选择方向键,在调用roadcake()函数进行消除小人路径
voidroadcake(int *i,int *j)
{
int temp;
temp=*i; /*进行值交换,即数组值为0和值为4进行交换
*i=*j;
*j=temp;
}
完成第一个控制之后进行清屏和画新的游戏状态图,若在游戏进行中要退出游戏,只需按0键,调用exit(0)函数结束;若要返回游戏菜单,按Esc键掉用menu()函数即可,重复以上步骤,直达游戏结束,此时掉用menu()函数自动返回菜单。
五、 总结
本次设计改进作业使我对于C语言有了更深的理解,不仅重新回顾了上学期所学的基础内容,还有了进一步的感悟,让我知道了,不管设计多么复杂的程序,“#include<stdio.h>”等基础内容都是必不可少的,int,for循环,printf等更是不可或缺,它们虽然基础,但是却为我们建立了一个良好的程序架构,简单与简单之间相互组合,循环之中套着循环,才能组成更为复杂的程序,同样也才能使得程序的功能更为完善,在这里由衷的感谢王成金老师带领我们走过了这半个学期的学习与生活,接下来就写出此次程序设计的源代码以及简单的改进报告。
六、 程序源代码
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
void drawmg1(void); /*定义画迷宫函数*/
void drawmg2(void);
void drawmg3(void);
void control1(void); /*定义控制函数*/
void control2(void);
void control3(void);
void roadcake(int *i,int *j); /*定义交换函数*/
void menu(void); /*定义菜单函数*/
void introduce(void); /*定义游戏介绍函数*/
int mg1[20][20]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{4,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,1,1,1,0,0,1,0,0,1},
{1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1},
{1,0,0,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
{1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,0,0,1,1},
{1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,4},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int road[20][20];
void main()
{
introduce();
}
/*-----------------------------------------------------------------------------*/
/*初级游戏控制函数*/
void control1(void)
{
int a,b,m,n;
char num;
do /*循环取得空制键*/
{
for(n=0;n<20;n++)
for(m=0;m<20;m++)
if(mg1[n][m]==4)
{
a=n;
b=m;break;
}
num=getch();
switch(num)
{
case 72:if(mg1[a-1][b]!=1)
{
roadcake(&mg1[a][b],&mg1[a-1][b]);
system("cls");
drawmg1();
break;
}
case 80:if(mg1[a+1][b]!=1)
{
roadcake(&mg1[a][b],&mg1[a+1][b]);
system("cls");
drawmg1();
break;
}
case 75:if(mg1[a][b-1]!=1)
{
roadcake(&mg1[a][b],&mg1[a][b-1]);
system("cls");
drawmg1();
break;
}
case 77:if(mg1[a][b+1]!=1)
{
roadcake(&mg1[a][b],&mg1[a][b+1]);
system("cls");
drawmg1();
break;
}
case 27:system("cls");
menu();break;
case 48:exit(0);
}
if(mg1[18][19]==4)
{printf("\n\t------------------you are win!--------------------\n\n\n");
menu();
}
}
while(1); /*直到出口*/
}
/*-----------------------------------------------------------------------------*/
/*中级游戏控制函数*/
void control2(void)
{
int a,b,m,n;
char num;
do /*循环取得空制键*/
{
for(n=0;n<30;n++)
for(m=0;m<30;m++)
if(mg2[n][m]==4)
{
a=n;
b=m;break;
}
num=getch();
switch(num)
{
case 72:if(mg2[a-1][b]!=1)
{
roadcake(&mg2[a][b],&mg2[a-1][b]);
system("cls");
drawmg2();
break;
}
case 80:if(mg2[a+1][b]!=1)
{
roadcake(&mg2[a][b],&mg2[a+1][b]);
system("cls");
drawmg2();
break;
}
case 75:if(mg2[a][b-1]!=1)
{
roadcake(&mg2[a][b],&mg2[a][b-1]);
system("cls");
drawmg2();
break;
}
case 77:if(mg2[a][b+1]!=1)
{
roadcake(&mg2[a][b],&mg2[a][b+1]);
system("cls");
drawmg2();
break;
}
case 27:system("cls");
menu();break;
case 48:exit(0);
}
if(mg2[29][15]==4)
{printf("\n\t------------------you are win!--------------------\n\n\n");
menu();
}
}
while(1); /*直到出口*/
}
/*-----------------------------------------------------------------------------*/
/*高级游戏控制函数*/
void control3(void)
{
int a,b,m,n;
char num;
do /*循环取得空制键*/
{
for(n=0;n<30;n++)
for(m=0;m<30;m++)
if(mg3[n][m]==4)
{
a=n;
b=m;break;
}
num=getch();
switch(num)
{
case 72:if(mg3[a-1][b]!=1)
{
roadcake(&mg3[a][b],&mg3[a-1][b]);
system("cls");
drawmg3();
break;
}
case 80:if(mg3[a+1][b]!=1)
{
roadcake(&mg3[a][b],&mg3[a+1][b]);
system("cls");
drawmg3();
break;
}
case 75:if(mg3[a][b-1]!=1)
{
roadcake(&mg3[a][b],&mg3[a][b-1]);
system("cls");
drawmg3();
break;
}
case 77:if(mg3[a][b+1]!=1)
{
roadcake(&mg3[a][b],&mg3[a][b+1]);
system("cls");
drawmg3();
break;
}
case 27:system("cls");
menu();break;
case 48:exit(0);
}
if(mg3[0][15]==4)
{printf("\n\t------------------you are win!--------------------\n\n\n");
menu();
}
}
while(1); /*直到出口*/
}
/*--------------------------------------------------------------------------------*/
/*画简单谜宫函数*/
void drawmg1(void)
{
int i,j,ch,num=1;
for(i=0;i<20;i++)
{
printf("\t\t\t");
for(j=0;j<20;j++)
{
ch=mg1[i][j];
switch(ch)
{
case 4: printf("*");break;
case 0: printf(" ");break;
case 1: printf("#");break;
}
if(num++%20==0)
printf("\n");
}
}
}
/*--------------------------------------------------------------------------------*/
/*画中等谜宫函数*/
void drawmg2(void)
{
int i,j,ch,num=1;
for(i=0;i<30;i++)
{
printf("\t\t\t");
for(j=0;j<30;j++)
{
ch=mg2[i][j];
switch(ch)
{
case 4: printf("*");break;
case 0: printf(" ");break;
case 1: printf("#");break;
}
if(num++%30==0)
printf("\n");
}
}
}
/*--------------------------------------------------------------------------------*/
/*画困难谜宫函数*/
void drawmg3(void)
{
int i,j,ch,num=1;
for(i=0;i<30;i++)
{
printf("\t\t\t");
for(j=0;j<30;j++)
{
ch=mg3[i][j];
switch(ch)
{
case 4: printf("*");break;
case 0: printf(" ");break;
case 1: printf("#");break;
}
if(num++%30==0)
printf("\n");
}
}
}
/*---------------------------------------------------------------------------*/
/*交换函数*/
void roadcake(int *i,int *j)
{
int temp;
temp=*i;
*i=*j;
*j=temp;
}
/*---------------------------------------------------------------------------*/
/*游戏介绍函数*/
void introduce(void)
{ char ch;
printf("\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>welcome<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");
printf("\t这是一个迷宫小游戏,你可以选择不同的难度进行游戏\n\n");
printf("\t具体游戏操作如下:\n\n");
printf("\t\t通过键盘方向↑ → ← ↓来控制小人(*)上、右、左、下移动;\n");
printf("\t\t在游戏过程中可以按Esc键返回菜单栏,按0键结束游戏!\n\n");
printf("\t\t\t\t\t\t\t请按Enter键进入游戏\n");
ch=getch();
if(ch==13)
system("cls");
menu();
}
/*----------------------------------------------------------------------------*/
/*游戏菜单函数*/
void menu(void)
{
char ch;
printf("\n\n\n请选择游戏难度:");
printf("\t\t【1:简单】\n\n");
printf("\t\t\t【2:中等】\n\n");
printf("\t\t\t【3:困难】\n\n");
printf("\t\t\t【0:退出】\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1:system("cls");
drawmg1();
/*简单游戏*/
control1();
break;
case 2:system("cls");
drawmg2(); /*中等游戏*/
control2();break;
case 3:system("cls");
drawmg3(); /*困难游戏*/
control3();break;
case 0:exit(0);
}
}
/*------------------------------------------------------------------------------*/
七、 程序简单改进报告
我们可以对程序进行一些简单的改进,例如在开始界面或选择难度的页面中添加一些欢迎语言,或者对迷宫本身进行难度上的调整,首先,我们对迷宫的欢迎界面设置一个简单的欢迎语:
在源程序中的
“printf("\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>welcome<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");”
与
“printf("\t这是一个迷宫小游戏,你可以选择不同的难度进行游戏\n\n");”
之间添加一行代码:
“printf("\t欢迎使用由【计科2班-张翼国】设计的迷宫游戏,您可以自行与同伴进行限时比赛^-^\n\n");”
上面便是对程序界面进行的简单改进,通过这种改进,我们可以完善自己对使用者进行的程序介绍,以及添加一些广告信息等等,也可以通过修改使程序界面更加好看,总之妙用无穷。
接下来,我们还可以对程序的核心内容——迷宫本身,进行一些修改,例如:
将简单迷宫的倒数第三行:“{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1},”
修改为: “{1,0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,1,1,1,1},”
类似的修改不仅可以对迷宫的BUG进行修复,还可以根据使用者的建议对迷宫本身的难度进行一定的调整。
当然了,C语言是非常奇妙的,还有其他各种各样的修改方式,这就需要开动我们的大脑去思考与设计了,修改程序不仅可以使我们对C语言的代码更加熟悉,更可以强化我们的逻辑思维,就如同“天下作文一大抄”这句俗语一样,我们的C语言一样可以汲取别人的精华之处,并对一些糟粕之处予以改进。
以上便是我对本次程序设作业的功能体会与简单的改进报告了,通过此次作业,我确实受益匪浅,查找资料的过程更是从网上的一些小程序代码之中学到了很多,再一次感谢王成金老师的指导与陪伴。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。