2021-04-18

推箱子游戏VC2013

##.cpp

#include <iostream>
#include <functional>
#include <algorithm>
#include <ctime>
#include <conio.h>
void color(const unsigned short COLor1);
void gotoXY(int goRow, int goCol);
void HideCursor(bool isVisible);
static const int GAME_ROW = 20;//c++17 constexpr
static const int GAME_COL = 20;
static bool WIN = false;
static int GAME_MAP[GAME_ROW][GAME_COL] = { 0 };
namespace YY{
	//std::string ele = "🏞,🏳‍🌈,🚀,🗻";表示不了
	//std::string ele = "▉,▇,◯,▚,▛,▒";
}
class GameEle{
public:
	enum ELement{
		Road = 0x00,
		Wall = 0x01,
		Box = 0x02,
		Point = 0x03,
		InPoint=0x04
	};
	static void IniGameArr();
	static void IniGameMap();
	static void IniGameRole(const int _rolex, const int _roley);//int 类型值传递比引用快
	static void RoleMove();
	static void SetRolePos(const int _dx, const int _dy);
	static void IniBoxPoint(const int _num);
	static void Collision(const int _row, const int _col);
	static bool IsWin();

public:
	static int m_RoleX;
	static int m_RoleY;
	
};
int GameEle::m_RoleX = 1;
int GameEle::m_RoleY = 1;
bool GameEle::IsWin(){
	for (int i = 0; i < GAME_ROW; ++i){
		for (auto ele : GAME_MAP[i]){//c++11
			if (ele == GameEle::Box || ele == GameEle::Point){
				return false;
			}
		}
	}
	return true;
}
void GameEle::Collision(const int _row, const int _col){
	int newrow = m_RoleX + _row;
	int newcol = m_RoleY + _col;
	if (GAME_MAP[newrow][newcol] == ELement::Wall || GAME_MAP[newrow][newcol] == GameEle::Point){
		//std::cout << "Wall and Point " << std::endl;
	}
	else if (GAME_MAP[newrow][newcol] == GameEle::Road){
		//std::cout << "Road" << std::endl;
		SetRolePos(_row, _col);
		
	}
	else if (GAME_MAP[newrow][newcol] == GameEle::Box){
		if (GAME_MAP[newrow + _row][newcol + _col] == GameEle::Box || GAME_MAP[newrow + _row][newcol + _col] == GameEle::Wall || GAME_MAP[newrow + _row][newcol + _col]==GameEle::InPoint){
			//std::cout << "Wall or Box or InPoint" << std::endl;
		}
		else if (GAME_MAP[newrow + _row][newcol + _col] == GameEle::Road){
			//std::cout << "Road" << std::endl;
			SetRolePos(_row, _col);
			std::swap(GAME_MAP[newrow][newcol], GAME_MAP[newrow + _row][newcol + _col]);
			/*GAME_MAP[newrow][newcol] = GameEle::Road;
			GAME_MAP[newrow + _row][newcol + _col] = GameEle::Box;*/
			gotoXY(newrow+_row, newcol + _col);
			std::cout << "▉";
		}
		else if (GAME_MAP[newrow + _row][newcol + _col] == GameEle::Point){
			SetRolePos(_row, _col);
			GAME_MAP[newrow][newcol] = GameEle::Road;
			GAME_MAP[newrow + _row][newcol + _col] = GameEle::InPoint;
			gotoXY(newrow + _row, newcol + _col);
			std::cout << "Wi";
		}
	}
	else if (GAME_MAP[newrow][newcol] == GameEle::InPoint){
		if (GAME_MAP[newrow + _row][newcol + _col] == GameEle::Road){
			GAME_MAP[newrow + _row][newcol + _col] = GameEle::Box;
			GAME_MAP[newrow][newcol] = GameEle::Point;
			gotoXY(newrow, newcol);
			std::cout << "PP";
			gotoXY(newrow + _row, newcol + _col);
			std::cout << "▉";

		}
		else if (GAME_MAP[newrow + _row][newcol + _col] == GameEle::Point){
			GAME_MAP[newrow][newcol] = GameEle::Point;
			GAME_MAP[newrow + _row][newcol + _col] = GameEle::InPoint;
			gotoXY(newrow, newcol);
			std::cout << "PP";
			gotoXY(newrow + _row, newcol + _col);
			std::cout << "Wi";
		}
	}
	if (IsWin()){
		WIN = true;
	}


}
void GameEle::IniBoxPoint(const int _num){
	int x = 0; int y = 0;
	for (int i = 0; i < _num; ++i){
		x = rand() % (GAME_ROW - 3) + 1;
		y = rand() % (GAME_COL - 3) + 1;
		GAME_MAP[x][y] = GameEle::Box;
	}
	for (int i = 0; i < _num; ++i){
		x = rand() % (GAME_ROW - 3) + 1;
		y = rand() % (GAME_COL - 3) + 1;
		GAME_MAP[x][y] = GameEle::Point;
	}
}
void GameEle::SetRolePos(const int _dx, const int _dy){
	gotoXY(m_RoleX, m_RoleY);
	std::cout << "  ";
	m_RoleX += _dx;
	m_RoleY += _dy;
	gotoXY(m_RoleX, m_RoleY);
	IniGameRole(m_RoleX, m_RoleY);
}
void GameEle::RoleMove(){
	while (true){
		int selection = getch();
		switch (selection)
		{
		case 'W':
		case 'w':Collision(-1, 0); break;
		case 'S':
		case 's':Collision(1, 0); break;
		case 'A':
		case 'a':Collision(0, -1); break;
		case 'D':
		case 'd':Collision(0, 1); break;
		default:
			break;
		}
		if (WIN){
			gotoXY(GAME_ROW, 0);
			std::cout << "WIN" << std::endl;
			return;
		}
	}
}
void GameEle::IniGameRole(const int _rolex, const int _roley){
	gotoXY(_rolex, _roley);
	std::cout << "OO";
}
void GameEle::IniGameMap(){
	for (int i = 0; i < GAME_ROW; ++i){
		for (int j = 0; j < GAME_COL; ++j){
			switch (GAME_MAP[i][j])
			{
			case ELement::Wall:std::cout << "▇"; break;
			case ELement::Box:std::cout << "▉"; break;
			case ELement::Road:std::cout << "  "; break;
			case ELement::Point:std::cout << "PP"; break;
			case ELement::InPoint:std::cout << "Wi"; break;
			}
		}
		std::cout << std::endl;
	}



}
void GameEle::IniGameArr(){
	for (int i = 0; i < GAME_ROW; ++i){
		for (int j = 0; j < GAME_COL; ++j){
			if (i == 0 || j == 0 || i == GAME_ROW - 1 || j == GAME_COL - 1){
				GAME_MAP[i][j] = GameEle::Wall;//设置墙
			}
			else{
				GAME_MAP[i][j] = GameEle::Road;//设置路;
			}
		}
	}
}
int main(){
	/*int yy = '🗻'; 表示不了
	std::cout << (char)yy;*/
	/*std::cout << "🗻";*/
	srand((unsigned)time(0));
	//std::cout << "◯";
	GameEle::IniGameArr();
	GameEle::IniBoxPoint(4);
	GameEle::IniGameMap();
	GameEle::IniGameRole(GameEle::m_RoleX, GameEle::m_RoleY);
	GameEle::RoleMove();
	system("pause");
	return EXIT_SUCCESS;
}

##.cpp //工具函数

#include<windows.h>


/*-------------------------颜色控制函数begin----------------------------------*/
void color(const unsigned short COLor1)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), COLor1);
}

void gotoXY(int goRow, int goCol)
{
	COORD pos = { 2 * goCol, goRow };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

//==================影藏光标函数  //1可见  0不可见
void HideCursor(bool isVisible)
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, isVisible };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

aiyouzichano

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

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

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

打赏作者

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

抵扣说明:

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

余额充值