【娱乐大闯关】C语言实现初级迷宫小游戏


一段路

今天我们来看看编程版的小迷宫,本文重在了解C语言中,定位、改换颜色等的操作……

1、头文件

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
#include<time.h>

2、地图编写

我们用“*”表示道路,用“#”表示墙壁!!!

char map[100][100] = {
"*#*********",
"***###*###*",
"###**#****#",
"*#**###**#*",
"***********",
"#####*##*##",
"**#*****#*E",
"***#*###**#",
"*#*********",
};

3、定位操作

实现鼠标的定位,老是把代码放左边都有点厌倦了耶!
注意:每一个函数功能的实现都得定位,才能对齐哟!

int N = 9, M = 11;
int curX = 0, curY = 0, mg_x = 20, mg_y = 10, tips_x = 10, tips_y = 30, intro_x = 10, intro_y = 45;
void gotoxy(int x, int y) {
	COORD pos;
	pos.X = x, pos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

4、定义颜色

让行动的游戏人物拥有有不一样的色彩

void setColor(unsigned int color) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

5、输出提示框

void outtips() {
	int i = 0;
	gotoxy(tips_x, tips_y);
	for (i = 0; i <= 60; i++)printf("*");
	gotoxy(tips_x, tips_y + 1);
	printf("*");
	for (i = 0; i < 59; i++)printf(" ");
	printf("*");
	gotoxy(tips_x, tips_y + 2);
	for (i = 0; i < 60; i++)printf("*");
}

6、输出提示信息

void outtips_text(const char* s) {
	int i = 0;
	gotoxy(tips_x + 1, tips_y + 1);
	for (i = 0; i <= 58; i++)
		printf(" ");
	gotoxy(tips_x + 1, tips_y + 1);
	printf("%s\a", s);
}

7、游戏人物打印

我们用大写字母“A”来表示行动的人

void printPerson(){
	printf("*");
	gotoxy(curX + mg_x + 15, curY + mg_y);
	printf("A");
	gotoxy(curX + mg_x + 15, curY + mg_y);
}

8、游戏地图打印

void printMap() {
	int i = 0;
	for (i = 0; i < N; i++) {
		gotoxy(mg_x + 15, mg_y + i);
		printf("%s\n", map[i]);
	}
}

9、人物移动操作

接下来就是游戏人物——小A的移动的环节啦!!

void myMove(char dir) {
	switch (dir)
	{
	case 'w':
	case 'W':
		curY--;	if (curY < 0) curY = 0;
		if (map[curY][curX] == '#') curY++;
		break;
	case 's':
	case 'S':
		curY++;
		if (curY >= N) curY = N - 1;
		if (map[curY][curX] == '#') curY--;
		break;
	case 'a':
	case 'A':
		curX--;
		if (curX < 0) curX = 0;
		if (map[curY][curX] == '#') curX++;
		break;
	case 'd':
	case 'D':
		curX++;
		if (curX >= M) curX = M - 1;
		if (map[curY][curX] == '#') curX--;
		break;
	}
}

10、主函数

int main() {
	printMap();
	setColor(FOREGROUND_RED);//设置颜色为红色
	outtips();
	printPerson();
	outtips_text("\t\t提示:A——左 D——右 W——上 S——下\n");
	gotoxy(tips_x + 20, tips_y + 2);
	system("pause");
	time_t time(time_t * t);//利用时间戳计算通关时间
	time_t start, end;
	time(&start);
	while (1) {
		char ch;
		ch = _getch();
		gotoxy(curX + mg_x + 15, curY + mg_y);
		myMove(ch);
		printPerson();
		if (map[curY][curX] == 'E') {
			char ss[100];
			time(&end);
			sprintf(ss, "\t通过成功,用时%d秒!", (end - start));
			outtips_text(ss);
			break;
		}
	}
    //署名阶段
	time_t timep;
	struct tm* p;
	time(&timep);
	p = localtime(&timep);
	printf("%d年 %d月 %d日", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday);
	printf("\t %d:%d:%d\n", p->tm_hour, p->tm_min, p->tm_sec);
	printf(" \n\n\n\n\b编写人:一段路\n\n");
}

先利用时间戳获取计算机当前时间,再用start和end表示游戏开始和结束的时间,则所用时间就位start-end即可获得。

time_t time(time_t *t);
	time_t start,end;
	time(&start);

署名阶段

time_t timep;
struct tm*p;
time(&timep);
p = localtime(&timep);
printf("%d年 %d月 %d日",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("\t %d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec);
printf(" \n\n\n\n\b编写人:一段路\n\n");

11、执行结果

迷宫
本程序为迷宫最底层版本,仅供欣赏……

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一段路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值