跳到正文

休闲·娱乐

迷宫小游戏-c++代码

xzaxza约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年10月19日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

我好像才想起来在古老的某个时候我做了个小游戏还没发呀 =(

C++


//	{"##################################################"},\
	{"#   ##   #   # #    #    !      #   #    !# #   @#"},\
	{"#   #  #   #   # ##  # #### # #   # # # ##       #"},\
	{"#     ### ## #     #   #   #    #  #   #  # ######"},\
	{"### #  #    ###! #  #  #  #  ###  #    #    #   ##"},\
	{"##    # ###    # # #  # # #     ##  # #   # # #  #"},\
	{"#! # #   # ### # #  #   # # # #    # #  # ## # #!#"},\
	{"## #  #      # # ## ###   ##   ####    # #  #   ##"},\
	{"#   #   ### #  #  #  #  #  !# #    # # # # ## #  #"},\
	{"###  ###     ##  #  ###  ###    ##  #    # #  #  #"},\
	{"# !# #   # ##   #!#   #  #   # # !# # # ## # #!# #"},\
	{"# # #  # # #  ##  #  ## #!# # #  #  #    #   #   #"},\
	{"# #   #   ##!# !#   # !#  #   # #  #  ##   ## ## #"},\
	{"#   ## # #! #  # #    #  # #     #  ##   ##  #   #"},\
	{"##    # ###  #    ###   #   # # #   #  ##   #  # #"},\
	{"#  ### #!# # # #  #! ###  #  # ####!##   # # #   #"},\
	{"## #  #  #   ## ####  #  #  ##   # #  ###  #   # #"},\
	{"#  ##    # ##   #   #   # #    # #   #   #   #   #"},\
	{"##    #       #   #   #  !# # #    #   #   #   #!#"},\
	{"##################################################"},



#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <stdio.h>  //输出彩色文本的头

using namespace std;

int n=30;
char mp[100][100]=
{
	{"##################################################"},//$ 30
	{"#   ##   #   # #    #           #   #    $#   # @#"},
	{"#   #  #   #   # ##  # #### # #   # # # ##    |  #"},
	{"###   ### ## #     #   #  $#    #  #   #  #   ####"},
	{"# # #  #    ###$ #  #  #  #  ###$ #    #    #   ##"},
	{"##    # ###    # # #  # # #     ##  # #   # # #  #"},
	{"#$ # #   # ### # #  #   # # # #    # #  # ## #$#$#"},
	{"## #  #      # # ## ###   ##   ####$   # # $#   ##"},
	{"#   #   ### #$ #  #  #  #  $# #    # # # # ## #  #"},
	{"###  ###     ##  #  ###  ###    ##  #    # #  #  #"},
	{"# $#$#   # ##   #$#   #  #   # # $# # # ## # #$# #"},
	{"# # #  # # #  ##  #  ## #$# # #  #  #    #   #   #"},
	{"# #   #   ##$# $#   # $#  #   # #  #  ##   ## ## #"},
	{"#   ## # #$ #  # #    #  # #     #  ##   ## $#   #"},
	{"##    # ###  #     ##   #   # # #   #  ##   #  # #"},
	{"#  ### #$#$# # #  #$ ###  #  #$####$##   # # #   #"},
	{"## #$ #  #   ## ####  #  #  ##   # #  ###  #   # #"},
	{"#  ##    # ##   #   #   #$#    # #   #   #   #   #"},
	{"##    #       #   #   #   # # #    #   #   #   #$#"},
	{"##################################################"},
};
void aa(int x, int y)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD c;
	c.X = y;
	c.Y = x;
	SetConsoleCursorPosition(h,c);
}

void bb(int xx, int yy)
{
	HANDLE hh = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD cc;
	cc.X = yy;
	cc.Y = xx;
	SetConsoleCursorPosition(hh,cc);
}

void aaa(const char* s, int color)										//<输
{																		//<出
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);					//<彩
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);		//<色
	printf(s);															//<文
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);			//<本
}

void abc()
{
	for(int i=0;i<=20;i++)
	{
		for(int j=0;j<=52;j++)
		{
			if(mp[i][j]=='#') aaa("#",12);
			else if(mp[i][j]=='@') aaa("@",9);
			else if(mp[i][j]=='|') aaa("|",8);
			else if(mp[i][j]=='$') aaa("$",13);
			else cout<<" ";
		}
		cout<<"\n";
	}
	cout<<"\n\t       W ";
	cout<<"\n\t用   A S D  移动";
}


void qqq(int x,int y)
{
	if(mp[x][y]=='@') cout<<"\b@";
	else cout<<"\b ";
}

int main()
{
	int o=1;
	abc();
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};                           	//<隐藏
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);	//<光标
	int x = 1, y = 1;
	int xx=23, yy=10;
	bb(xx,yy);
	cout<<"\n\n       还有 "<<n<<" 分打开大门";
	aa(x,y);
	aaa(">",2);
	while (1)
	{
		if (kbhit())
		{
			char k = getch();
			if (k == 'w')
			{
				qqq(x,y);
				if(mp[x-1][y]!='#') x--;
				aa(x,y);
				aaa("^",2);
			}
			else if (k == 's')
			{
				qqq(x,y);
				if(mp[x+1][y]!='#') x++;
				aa(x,y);
				aaa("v",2);
			}
			else if (k == 'a')
			{
				qqq(x,y);
				if(mp[x][y-1]!='#') y--;
				aa(x,y);
				aaa("<",2);
			}
			else if (k == 'd')
			{
				qqq(x,y);
				if(mp[x][y+1]!='#'&&mp[x][y+1]!='|') y++;
				aa(x,y);
				aaa(">",2);
			}
			if(mp[x][y]=='@')
			{
				system("cls");
				aaa("\n\n\n\t\t\t你赢了\n",11);
				system("pause");
				return 0;
			}
		}
		if(mp[x][y]=='$'&&o)
		{
			n--;
			bb(xx,yy);
			cout<<"\n\n       还有 "<<n<<" 分打开大门";
			mp[x][y]=' ';
			aa(x,y+1);
		}
		if(n==0&&o)
		{
			mp[2][46]=' ';
			bb(xx,yy);
			cout<<"\n\n        大门已打开";
			xx=2,yy=47;
			bb(xx,yy);
			cout<<"\b ";
			aa(x,y+1);
			o=0;
		}
	}
}
原文