c语言 命令行走迷宫小游戏

直接上代码

#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define HANG 25
#define LIE 30
#define WALL 2
#define XIANJING 1
#define KONG 0
#define ZHONGDIAN 4
#define HUMAN 3
#define UP 119
#define DOWN 115
#define LEFT 97
#define RIGHT 100
void Run();
void GameOver(int);
int moveHuman(int,int);
void start();
void color(int c);
void xianjingshengcheng();
//隐藏光标
struct Human{
	int x;
	int y;
}human;
void HideCursor()
{
	CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
	curInfo.dwSize = 1; //如果没赋值的话,光标隐藏无效
	curInfo.bVisible = FALSE; //将光标设置为不可见
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
}
//光标跳转
void CursorJump(int x, int y)
{
	COORD pos; //定义光标位置的结构体变量
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorPosition(handle, pos); //设置光标位置
}
//颜色设置
void color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //颜色设置
	//注:SetConsoleTextAttribute是一个API(应用程序编程接口)
}

int map[HANG][LIE];
void xianjingshengcheng(){
	srand(time(0));
	for(int i=1;i<LIE-1;i++){
		for(int j=1;j<HANG-1;j++){
			
			if(rand()%12==1||rand()%12==2||rand()%12==3){
				if(i==LIE-2&&j==1){
					
					continue;
				}	
				if(i<=3&&j>=HANG-3) continue;
				
				map[j][i]=XIANJING;
				CursorJump(i*2,j);
				printf("坑");
			}else{
				map[j][i]=KONG;
			}
		}
	}
}
void start(){
	HideCursor();
		for(int i=0;i<LIE;i++){
			CursorJump(i*2,HANG-1);
			printf("墙");
			map[HANG-1][i]=WALL;
			if(i==LIE-2){
				CursorJump(i*2,0);
				printf("  ");
				map[0][i]=ZHONGDIAN;
				continue;
			}
			CursorJump(i*2,0);
			printf("墙");
			map[0][i]=WALL;	
		}
	for(int i=1;i<HANG-1;i++){
			CursorJump(0,i);
			map[i][0]=WALL;
			printf("墙");
			CursorJump(LIE*2-2,i);
			map[i][LIE-1]=WALL;
			printf("墙");
		}
	CursorJump(2,HANG-2);
	map[HANG-2][1]=HUMAN;
	printf("人");
	human.x=1;
	human.y=HANG-2;
	
}
void Run(){
	
	while(1){
		if(kbhit()){
			int n=getch();
			switch (n) {
				case UP:
					if(moveHuman(0,-1)==0){
						return;
					}
					break;
				case DOWN:
					if(moveHuman(0,1)==0){
						return;
					}
					break;
				case LEFT:
					if(moveHuman(-1,0)==0){
						return;
					}
					break;
				case RIGHT:
					if(moveHuman(1,0)==0){
						return;
					}
					
					break;
			}
		}

	}
}
void GameOver(int m){
	Sleep(1000); //留给玩家反应时间
	system("cls"); //清空屏幕
	color(7); //颜色设置为白色
	CursorJump(2 * (LIE / 3), HANG / 2 - 3);
	if(m){
		color(32);
		printf("蔡菜菜菜蔡");
		return;
	}else{
		color(78);
		printf("成功,v我50解锁下一关");
	}
}
int moveHuman(int x,int y){
	int hang=human.y+y;
	int lie=human.x+x;
	if(map[hang][lie]==WALL) return 1;
	if(map[hang][lie]==XIANJING){
		CursorJump(human.x*2,human.y);
		printf("  ");
		CursorJump(lie*2,hang);
		printf("死");
		GameOver(1);
		return 0;
	}
	if(map[hang][lie]==KONG){
		CursorJump(human.x*2,human.y);
		printf("  ");
		map[human.y][human.x]=KONG;
		human.x=lie;
		human.y=hang;
		CursorJump(human.x*2,human.y);
		printf("人");
		map[hang][lie]=HUMAN;
		return 1;
	}
	if(map[hang][lie]==ZHONGDIAN){
		CursorJump(human.x*2,human.y);
		printf("  ");
		map[human.y][human.x]=KONG;
		human.x=lie;
		human.y=hang;
		CursorJump(human.x*2,human.y);
		printf("人");
		map[hang][lie]=HUMAN;
		GameOver(0);
		return 0;
	}
}
int main(){
	system("title 走迷宫  by 雪寂");
	system("mode con cols=60 lines=25");
	start();
	xianjingshengcheng();
	CursorJump(LIE*2,HANG);
		printf("\n");
	//moveHuman(1,0);
	CursorJump(LIE*2,HANG);
	//printf("\n%d %d %d\n",map[2][HANG-1],map[3][HANG-1],map[3][HANG-4]);
	
	Run();
	CursorJump(2 * (LIE / 3), HANG / 2);
	printf("按Enter键退出");
	getchar();
	/*
	printf("\n");
	for(int i=0;i<HANG;i++){
		for(int j=0;j<LIE;j++){
			printf("%d ",map[i][j]);
		}
		printf("\n");
	}
	printf("%d %d",human.x,human.y);
	getchar();
	*/
	return 0;
}

 隐藏和移动光标需要调用一些windows.h里的函数,这里大家直接抄代码就好了

{
	CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
	curInfo.dwSize = 1; //如果没赋值的话,光标隐藏无效
	curInfo.bVisible = FALSE; //将光标设置为不可见
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
}
//光标跳转
void CursorJump(int x, int y)
{
	COORD pos; //定义光标位置的结构体变量
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorPosition(handle, pos); //设置光标位置
}

剩下的就是靠数组实现地图,这个看代码就可以看懂了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值