最简单的c语言迷宫游戏实验报告
更新时间:2024-01-27 05:47:01 阅读量: 教育文库 文档下载
- c语言简单迷宫游戏代码推荐度:
- 相关推荐
一、 内容:
1、 本游戏主要实现了人控制键盘方向键使小人(*)走出迷宫。 2、 具有的功能:
1)、在游戏菜单里人可以选择不同难度的游戏进行游戏; 2)、在游戏过程中,可以通过键盘方向键使小人移动,走出迷宫; 3)、在游戏过程中,当人碰到墙壁(#)的时候小人过不去; 4)、当人顺利完成游戏之后,输出“========you are win!======”字样,30秒钟
后自动返回到游戏菜单; 5)、在游戏过程中,人可以通过按Esc键返回游戏菜单;也可以可以按0直接退出
游戏; 6)、在游戏菜单里,按0键可以退出游戏。 3、具体应用: 1)、人主要同过键盘的1,2,3数字键来选择游戏难度;
2)、在游戏中通过Esc键来返回菜单; 3)、同过0键退出游戏。
二、 上机环境
操作系统:windows7 开发工具:VC6.0
三、 函数调用关系图
main() 输入1 introduce() system(\roadcake() Enter menu() 输入0 输入2 system(\ 输入3 system(\ roadcake() roadcake() exit(0);
四、 各函数功能说明
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(\ for(j=0;j<20;j++) { ch=mg1[i][j]; switch(ch) { case 4: printf(\ case 0: printf(\ case 1: printf(\ } if(num++ ==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()函数进行消除小人路径 void roadcake(int *i,int *j) {
int temp;
temp=*i; /*进行值交换,即数组值为0和值为4进行交换 *i=*j; *j=temp; }
完成第一个控制之后进行清屏和画新的游戏状态图,若在游戏进行中要退出游戏,只需按0键,调用exit(0)函数结束;若要返回游戏菜单,按Esc键掉用menu()函数即可,重复以上步骤,直达游戏结束,此时掉用menu()函数自动返回菜单。
六、 程序运行效果图
开始执行程序:
按Enter键后:
选择2后进入中级难度游戏:
按键盘方向键进行游戏,当顺利结束后:
与游戏菜单相同,选择不同的级别进行游戏。
七、 总结
本次课设在设计算法和运用语言在一定程度上遇到困难,同时有些以前不知道的函数和算法,在这次实习中通过查找资料学到了许多;觉得紧紧学好语言是不够的,得要知道怎样运用,即算法很重要;
八、 参考文献
李春葆 曾平 喻丹丹 《c语言程序设计(第2版)》 清华大学出版社 2011
九、 程序清单
#include
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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,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{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(\ drawmg1(); break; } case 80:if(mg1[a+1][b]!=1)
{
roadcake(&mg1[a][b],&mg1[a+1][b]);
system(\ drawmg1(); break;
}
case 75:if(mg1[a][b-1]!=1)
{
roadcake(&mg1[a][b],&mg1[a][b-1]);
system(\ drawmg1(); break; } case 77:if(mg1[a][b+1]!=1)
{
roadcake(&mg1[a][b],&mg1[a][b+1]);
system(\
drawmg1(); break;
} case 27:system(\
menu();break;
case 48:exit(0); }
if(mg1[18][19]==4)
{printf(\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(\ drawmg2();
break;
} case 80:if(mg2[a+1][b]!=1)
{
roadcake(&mg2[a][b],&mg2[a+1][b]);
system(\ drawmg2();
break;
}
case 75:if(mg2[a][b-1]!=1)
{
roadcake(&mg2[a][b],&mg2[a][b-1]);
system(\ drawmg2(); break; } case 77:if(mg2[a][b+1]!=1)
{
roadcake(&mg2[a][b],&mg2[a][b+1]);
system(\
drawmg2(); break; }
case 27:system(\
menu();break; case 48:exit(0); }
if(mg2[29][15]==4)
{printf(\
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(\ drawmg3();
break;
are
}
case 80:if(mg3[a+1][b]!=1) {
roadcake(&mg3[a][b],&mg3[a+1][b]);
system(\ drawmg3(); break;
}
case 75:if(mg3[a][b-1]!=1)
{
roadcake(&mg3[a][b],&mg3[a][b-1]);
system(\ drawmg3(); break; } case 77:if(mg3[a][b+1]!=1)
{
roadcake(&mg3[a][b],&mg3[a][b+1]);
system(\
drawmg3(); break;
} case 27:system(\
menu();break;
case 48:exit(0);
}
if(mg3[0][15]==4)
{printf(\win!--------------------\\n\\n\\n\
menu(); }
}
while(1); /*直到出口*/
}
/*--------------------------------------------------------------------------------*/
/*画初级谜宫函数*/ void drawmg1(void) { int i,j,ch,num=1;
for(i=0;i<20;i++) { printf(\ for(j=0;j<20;j++) {
ch=mg1[i][j]; switch(ch) {
case 4: printf(\ case 0: printf(\ case 1: printf(\ }
if(num++ ==0) printf(\
}
}
}
/*--------------------------------------------------------------------------------*/ are
/*画中级谜宫函数*/ void drawmg2(void) { int i,j,ch,num=1;
for(i=0;i<30;i++) { printf(\ for(j=0;j<30;j++) {
ch=mg2[i][j]; switch(ch) {
case 4: printf(\ case 0: printf(\ case 1: printf(\ }
if(num++0==0) printf(\
}
}
}
/*--------------------------------------------------------------------------------*/
/*画高级谜宫函数*/ void drawmg3(void) { int i,j,ch,num=1;
for(i=0;i<30;i++) { printf(\ for(j=0;j<30;j++) {
ch=mg3[i][j]; switch(ch) {
case 4: printf(\ case 0: printf(\ case 1: printf(\ }
if(num++0==0)
printf(\ }
}
}
/*---------------------------------------------------------------------------*/ /*交换函数*/
void roadcake(int *i,int *j) { int temp; temp=*i; *i=*j; *j=temp;
}
/*---------------------------------------------------------------------------*/ /*游戏介绍函数*/
void introduce(void) { char ch;
printf(\>>welcome<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\n\\n\ printf(\这是一个迷宫小游戏,你可以选择不同的难度进行游戏\\n\\n\
printf(\具体游戏操作如下:\\n\\n\ printf(\通过键盘方向↑ → ← ↓来控制小人(*)上、右、左、下移动;\\n\ printf(\在游戏过程中可以按Esc键返回菜单栏,按0键结束游戏!\\n\\n\
printf(\请按Enter键进入游戏\\n\ ch=getch(); if(ch==13) system(\
menu();
}
/*----------------------------------------------------------------------------*/
/*游戏菜单函数*/
void menu(void) { }
/*------------------------------------------------------------------------------*/
还有很多功能么有实现,惭愧啊??
case 3:system(\ }
drawmg3();
/*
printf(\请选择游戏难度:\printf(\初级\\n\\n\printf(\中级\\n\\n\printf(\高级\\n\\n\printf(\退出\\n\\n\scanf(\switch(ch) {
case 1:system(\
case 2:system(\
drawmg2();
/*
drawmg1();
/*初级游戏*/
control1(); break; char ch;
中级游戏*/
control2();break;
高级游戏*/
control3();break; case 0:exit(0);
正在阅读:
最简单的c语言迷宫游戏实验报告01-27
共青团建团100周年征文精选10篇04-04
我的教育叙事04-02
教师招聘结构化面试试题全集90页04-29
健康评估题12-01
人教版小学三年级语文上册多音字复习资料05-25
亲情安全寄语手册10-12
增值税实验报告02-29
成人考试工程测量考试答案04-23
教师党员思想汇报10篇08-01
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 迷宫
- 语言
- 实验
- 简单
- 报告
- 游戏
- Oracle 11g R2+RAC+ASM+OracleLinux6.4安装详解
- 数字逻辑实验报告书 - 图文
- 微观第八章习题及答案(DOC)
- 51单片机考试常见简答题
- 金沙五中教师职称聘任的量化考核方案修改后
- 安全目视化管理规定 - 图文
- 第二节 动物与人类生活的关系(点睛) - 图文
- 2015上海松江广播电视台招聘派遣制编辑记者公告
- 化工设备机械基础习题解答
- 除氧煤仓间上部脚手架搭设施工方案08 - 图文
- 杜冠宇讲义--GRE词汇
- 一种数字化列车广播系统及实现方法
- 悬挂外开窗五金系统配置方案 - 图文
- 案例式教学法在“互换性与测量技术基础”教学中的探索与应用
- IATF16949质量体系外审准备计划清单(20180613) - 图文
- 客户至尊 - 金牌客户服务技巧 - 图文
- 蛋白质含量测定
- 壮阳绝技(不花钱自己锻炼天天做处男)
- 影像基本知识点复习题
- 2014年全国会计职称考试初级会计实务模拟题(三)