C语言迷宫游戏

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>

using namespace std;

void init();//初始化函数 
void createMap();//创建地图函数 
int execute(char in,char map[][200]);//执行移动逻辑函数 
void eggs();//彩蛋 
void love();//彩蛋

char input='\0';//输入的指令 ,全局变量 
int x=1,y=2;//初始开始坐标 ,全局变量 

int main(){
	
	system("title 迷宫小游戏");//标题 
	system("mode con cols=60 lines=37");//设置窗口大小
//	system("color 13"); 
	while(input!='E'){
		init();
	}

	return 0;
}

void init(){
	
	cout<<"          欢迎进入迷宫游戏\n"; 
	cout<<"按 K 开始,按 E 结束,按 H 查看帮助文档\n";
	
	cin>>input;
	
	switch(input){
		case 'K':
		case 'k': createMap();break;
		case 'h':
		case 'H': cout<<"请联系六六老师\n\n";break;
		case 'e':
		case 'E': cout<<"hei!,下次再来玩啊!\n";exit(0);
		default:
			cout<<"请输入正确的指令!\n\n";
			
	}
	
}
void createMap(){
	//每次从新开始游戏的时候,清除全局变量x,y的值 
	x=1,y=2;
	system("cls");
	cout<<"                开始游戏(o是退出)\n"; 
	char map[200][200]={//地图
				"##################################################",
				"##*             ##              ##              ##",
				"##  ##########  ##  ######  ######  ######  ######",
				"##      ##      ##      ##  ##      ##  ##      ##",
				"##########  ##########  ##  ##  ######  ######  ##",
				"##      ##          ##  ##      ##          ##  ##",
				"##  ##  ##########  ##  ##########  ##########  ##",
				"##  ##              ##  ##          ##          ##",
				"##  ##########  ######  ######  ##  ##  ######  ##",
				"##          ##  ##              ##  ##      ##  ##",
				"##########  ######  ##############  ######  ##  ##",
				"##          ##      ##          ##      ##  ##  ##",
				"##  ##  ######  ######  ######  ##########  ##  ##",
				"##  ##  ##      ##      ##  ##          ##  ##  ##",
				"##  ######  ######  ######  ##########  ##  ##  ##",
				"##              ##  ##          ##      ##  ##  ##",
				"##################  ##  ##########  ######  ######",
				"##          ##      ##      ##      ##          ##",
				"##  ######  ##  ######  ##  ##  ##############  ##",
				"##  ##          ##      ##              ##      ##",
				"##  ##############  ##################  ##  ######",
				"##          ##      ##              ##  ##      ##",
				"##  ######  ##########  ##############  ######  ##",
				"##      ##              ##                      00",//23,47
				"##################################################",
			};
		for(int i=0;i<=24;i++){
			puts(map[i]);//将地图一行一行输出 
		}
		cout<<"\nwsad,分别代表着上下左右移动,o是退出(请输入小写字母!)\n";
		
		char in='\0';
		int result=0;
		
		while(result!=1&&in!='o'){
			
			in=getch();
			//具体执行逻辑 
			result = execute(in,map);
			system("cls");
			cout<<"                开始游戏(o是退出)\n";
			for(int i=0;i<=24;i++)
				puts(map[i]);
		}	
			
	
}
int execute(char in,char map[][200]){
	
	if(map[x][y+1]=='0'){
		//成功
		system("cls");
		cout<<"\n\n\n\n        恭喜,通关!"; 
		Sleep(5000);
		return 1;
	}else if(in=='a'){ //左移
		//判断移动的点是不是墙,也就是字符'#' 
		if(map[x][y-1]!='#'){
			//不是,则将原位置赋值为空,移动的位置赋值为'*' 
			map[x][y]=' ';
			y--;
			map[x][y]='*';
		}
		if(x==1&&y==3){
			system("cls");
			eggs();
			Sleep(2000);
		}		
	}else if(in=='d'){
		if(map[x][y+1]!='#'){
			map[x][y]=' ';
			y++;
			map[x][y]='*';
		}
	}else if(in=='w'){
		if(map[x-1][y]!='#'){
			map[x][y]=' ';
			x--;
			map[x][y]='*';
		}
		if(x==22&&(y==46||y==45)){
			system("cls");
			love();
			Sleep(2000);
		}
	}else if(in=='s'){
		if(map[x+1][y]!='#'){
			map[x][y]=' ';
			x++;
			map[x][y]='*';
		}
	}else if(in=='o'){
		return 1;
	}else{
		cout<<"请输入正确的指令\n";
		Sleep(2000);
	}	
	return 0;
}
void eggs(){
	char egg[][100]={
	"#########################",
	"#########################",
	"######  COME ON!  #######",
	"######   加油!   #######",
	"#########################",
	"#########################",
	};
	
	for(int i=0;i<sizeof(egg)/sizeof(*egg);i++)
		puts(egg[i]); 
}
void love(){
	double x,y,a;
    for(y=1.5;y>-1.5;y-=0.1214)  
    {
        for(x=-1.5;x<1.5;x+=0.05)
        {
            a=x*x+y*y-1;
            if(a*a*a-x*x*y*y*y<=0)    
               {
                printf("*");
                Sleep(1);       
               }
            else
                printf(" ");
        }
        printf("\n");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值