C语言综合大作业之走迷宫

项目名称:迷宫之逃出生天

1、开发背景
迷宫这个游戏风靡已久,便想尝试设计一个迷宫,网络上设计的迷宫大多是通过已有模板生成的,不能按照自己的想法来进行创造设计,于是我们设想设计一个能通过自己设计TXT文档的迷宫程序,游玩者可自己设计地图,通过程序将地图绘制处理进行游戏,于是便有了这个游戏的来源。
主角、墙壁、道路、逃生标志、死亡标志、装饰符以及文字构成。自建一个txt文档储存数据,用数字1代替墙壁"▓",2代表角色"♀" ,3代表道路" “,4代表文字"破败圣迹之逃出生天”、5代表装饰符"卐"、6代表装饰符"卍"、7代表逃生标志"○"、*代表死亡标志"◎"来构成图形化界面。

2、.系统功能设计
2.1系统功能模块设计
分为游戏界面绘制,走迷宫的基本操作,游戏控制模块三个模块,其中游戏界面绘制包括了导入TXT文件,输出地图,走迷宫的操作包括移动,到达终点,游戏控制包括作者介绍,游戏帮助,进入游戏与退出游戏按键。
系统功能模块图

										图1 系统功能模块图

2.2 系统业务流程设计
• 用户进入界面后,会出现四个选项,进入游戏,游戏帮助,开发小组,退出游戏,选择进入游戏,生成地图,用户控制小人移动,按esc键可退出游戏。
在这里插入图片描述

图2 业务流程图
3.项目创建
3.1系统开发环境要求
本项目的开发及运行环境要求:
操作系统:win10(2019)
开发工具:Visual Studio 2019
开发语言:c语言
3.2 项目创建过程
1、打开Visual Studio 2019,选择创建新项目
在这里插入图片描述

2、选择控制台应用,并命名,选择保存地址
在这里插入图片描述

3、创建项目成功

在这里插入图片描述
四、预处理模块设计
1、文件引用

order.h文件
#include "stdio.h"  //标准输入输出库
#include "stdlib.h"//标准库函数头文件stdlib 头文件里包含了C、C++语言的最常用的系统函数该文件包含了C语言标准库函数的定义
#include "string.h"//字符数组的函数定义的头文件
#include "windows.h"//是一个最重要的头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件
#include "time.h"//头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数
#include "conio.h"//其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数

``

``
m.h文件
#include "stdio.h"  //标准输入输出库
#include "stdlib.h"//标准库函数头文件stdlib 头文件里包含了C、C++语言的最常用的系统函数该文件包含了C语言标准库函数的定义
#include "string.h"//字符数组的函数定义的头文件
#include "windows.h"//是一个最重要的头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件
#include "time.h"//头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数
#include "conio.h"//其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数


源码

Main.cpp
#include"m.h"
int main()
{
	srand((unsigned int)time(0));

	while (flag)
	{
		system("cls");//清屏
		flag = Menu();
		switch (flag)
		{
		case GAME:
			Move();
			break;
		case HELP:
			Help();
			break;
		case WORKER:
			Worker();
			break;
		}

	}
	return 0;
}

Order.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"order.h"

void GotoXY(int x, int y)
{
	HANDLE hout;
	COORD cor;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	cor.X = x;
	cor.Y = y;
	SetConsoleCursorPosition(hout, cor);
}
void Hide()
{
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cor_info = { 1, 0 };
	SetConsoleCursorInfo(hout, &cor_info);
}
int Menu()
{
	GotoXY(40, 12); 			     //定位光标位置
	printf("欢迎来到走迷宫小游戏");
	GotoXY(43, 14);
	printf("1.开始游戏");
	GotoXY(43, 16);
	printf("2.帮助");
	GotoXY(43, 18);
	printf("3.小组成员");
	GotoXY(43, 20);
	printf("4.按任意键退出");
	Hide();	                       //隐藏光标
	char ch;
	int result = 0;
	while (_kbhit());
	ch = _getch();   			    //接收用户输入的菜单选项
	switch (ch) //根据选项设置返回结果值
	{

		case '1':result = 1; break;
		case '2':result = 2; break;
		case '3':result = 3; break;
		case '4':result = 4; break;
	}
	system("cls");  				//调用系统命令cls完成清屏操作
	return result;
}
void Help()
{
	char ch;
	GotoXY(40, 12);
	printf("w 上");
	GotoXY(40, 14);
	printf("s 下");
	GotoXY(40, 16);
	printf("a 左");
	GotoXY(40, 18);
	printf("d 右");
	GotoXY(40, 20);
	printf("当人走完时游戏结束");
	GotoXY(40, 22);
	printf("按esc返回上级菜单");
	Hide();		//隐藏光标
	while (1)
	{
		while (!_kbhit());
		ch = _getch();
		if (ch == 27) break;
	}
	//	system("cls");
}
void Worker()
{
	char ch = 0;
	system("cls");//清屏

	GotoXY(40, 16);
	printf("小组成员:姜习武,林成栋");
	while (1)
	{
		while (!_kbhit());
		ch = _getch();
		if (ch == 27) break;
	}


	//	system("cls");
}
void Move()
{
	void shuru(char shuju[29][26], int* a, int* b);
	void shuchu(char shuju[29][26], int* a, int* b);
	int a, b;
	char ch, c;
	char shuju[29][26] = { "\0" };
	shuru(shuju, &a, &b);
	shuchu(shuju, &a, &b);
	ch = _getch();
	while (1)
	{
		switch (ch)
		{
		case 'w':
		case 'W':
		{
			if (shuju[a - 1][b] == '&' || shuju[a - 1][b] == '*')
			{
				c = shuju[a - 1][b];
				shuju[a - 1][b] = shuju[a][b];
				shuju[a][b] = c;
				shuchu(shuju, &a, &b);
			}
			break;
		}
		case 'a':
		case 'A':
		{
			if (shuju[a][b - 1] == '&' ||shuju[a][b - 1] == '*')
			{
				c = shuju[a][b - 1];
				shuju[a][b - 1] = shuju[a][b];
				shuju[a][b] = c;
				shuchu(shuju, &a, &b);
			}
			break;
		}
		case 's':
		case 'S':
		{
			if ((shuju[a+1][b] == '&') ||(shuju[a+1][b] == '*')||(shuju[a ][b+1] == '*'))
			{
				c = shuju[a+1][b];
				shuju[a+1][b] = shuju[a][b];
				shuju[a][b] = c;
				shuchu(shuju, &a, &b);
			}
			break;
		}
		case 'd':
		case 'D':
		{
			if (shuju[a][b + 1] == '&' ||shuju[a][b + 1] == '*')
			{
				c = shuju[a][b + 1];
				shuju[a][b + 1] = shuju[a][b];
				shuju[a][b] = c;
				shuchu(shuju, &a, &b);
			}
			break;
		}
		}
		if (shuju[22][23] == '2')
		{
			printf("你已逃出生天!\n");
			break;
		}
		if (shuju[3][13] == '2' || shuju[3][19] == '2' || shuju[9][5] == '2' || shuju[7][20] == '2' || shuju[12][19] == '2' || shuju[17][13] == '2' || shuju[23][19] == '2')
		{
			printf("你已死亡!\n");
			break;
		}
		ch = _getch();
	}
}
void shuru(char shuju[29][26], int* a, int* b)
{
	int i = 0, j = 0;
	FILE* fp;
	char ch;
	if ((fp = fopen("走迷宫.txt", "r")) == NULL)
	{
		printf("文件路径不正确!");
		exit(1);
	}
	ch = fgetc(fp);
	while (ch != EOF)
	{
		if (ch == '1')
		{

			shuju[i][j] = ch;
			j++;
			if (j == 26)
			{
				i++;
				j = 0;
			}
		}
		else if (ch == '2')
		{

			shuju[i][j] = ch;
			(*a) = i;
			(*b) = j;
			j++;
			if (j == 26)
			{
				i++;
				j = 0;
			}
		}
		else if (ch == '&')
		{
			shuju[i][j] = ch;
			j++;
			if (j == 26)
			{
				i++;
				j = 0;
			}
		}
		else
		{
			shuju[i][j] = ch;
			j++;
			if (j == 26)
			{
				i++;
				j = 0;
			}
		}
		ch = fgetc(fp);
	}
	fclose(fp);
}
void shuchu(char shuju[29][26], int* a, int* b)
{
	int i, j;
	system("cls");
	for (i = 0; i < 29; i++)
		for (j = 0; j < 26; j++)
		{
			if (shuju[i][j] == '1')
				printf("●");
			else if (shuju[i][j] == '2')
			{
				printf("♀");
				(*a) = i;
				(*b) = j;
			}
			else if (shuju[i][j] == '&')
			{
				printf("  ");
			}
			else if (shuju[i][j] == '4')
			{
				printf("快逃啊");
			}
			else if (shuju[i][j] == '5')
			{
				printf("卐");
			}
			else if (shuju[i][j] == '6')
			{
				printf("卍");
			}
			else if (shuju[i][j] == '7')
			{
				printf("○");
			}
			else if (shuju[i][j] == '*')
			{
				printf("◎");
			}
			else if (shuju[i][j] == '8')
			{
				printf("W:向上移动\tS:向下移动");
			}
			else if (shuju[i][j] == '9')
			{
				printf("A:向左移动\tD:向右移动");
			}
			else if (shuju[i][j] == 'k')
			{
				printf("向○处逃生!触碰◎则死亡!\0");
			}
			else
			{
				putchar(shuju[i][j]);
			}
		}
	printf("\n");
}

Order.h
#pragma once
#ifndef  _IMGUI_H
#define _IMGUI_H
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#include "time.h"
#include "conio.h"
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'

void GotoXY(int x, int y);	//光标定位函数
/*隐藏光标*/
void Hide();

//开始菜单
int Menu();
//帮助界面
void Help();
//小组成员界面
void Worker();
void Move();
void shuru(char shuju[38][26], int* a, int* b);
void shuchu(char shuju[38][26], int* a, int* b);
#endif // !_IMGUI_H

m.h
#pragma once
#ifndef  _M_H
#define  _M_H
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#include "time.h"
#include "conio.h"
#include "order.h"

#define GAME 1
#define HELP 2
#define WORKER  3
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'

static int flag = 1;
static int end_flag = 1;
#endif // !


  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值