Part3:CTetrad_Z 、CTetradFactory及CComboControl

目录:

1、CTetrad_Z类具体实现

2、CTetradFactory类的实现

3、CComboControl类的实现

1、CTetrad_Z类具体实现

      CTetrad_Z类是方块中的一种,继承自CTetrad类。其它的实现类似 

class CTetrad_Z : public CTetrad
{
public:
	CTetrad_Z(CBlocksMatrix* pParent);
	~CTetrad_Z();

	bool Rotate();
	bool MoveLeft();
	bool MoveRight();
	bool MoveDown();
	void FillMatrix();
	bool IsValid();

	void Draw();
	void DrawOnScreen(const TRectanglei& rect);
};

CTetrad_Z::CTetrad_Z(CBlocksMatrix* pParent) : CTetrad(pParent,bcRed)
{
}

CTetrad_Z::~CTetrad_Z()
{
}

bool CTetrad_Z::Rotate()
{
	bool bSuccess = false;
	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		if (m_pParentMatrix->IsCellFree(m_iXPos,m_iYPos-1) &&
			m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos+1) )
		{
			m_Orientation = Rotation90;
			bSuccess = true;
		}
		break;

	case Rotation90:
	case Rotation270:
		if (m_pParentMatrix->IsCellFree(m_iXPos,m_iYPos+1) &&
			m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos+1))
		{
			m_Orientation = Rotation0;
			bSuccess = true;
		}
		break;
	}
	return bSuccess;
}

bool CTetrad_Z::MoveLeft()
{
	bool bSuccess = false;
	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		if (m_pParentMatrix->IsCellFree(m_iXPos-2,m_iYPos) && 
			m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos+1))
		{
			m_iXPos--;
			bSuccess = true;
		}
		break;

	case Rotation90:
	case Rotation270:
		if (m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos-1) &&
			m_pParentMatrix->IsCellFree(m_iXPos-2,m_iYPos  ) &&
			m_pParentMatrix->IsCellFree(m_iXPos-2,m_iYPos+1) )
		{
			m_iXPos--;
			bSuccess = true;
		}
		break;
	}
	return bSuccess;
}

bool CTetrad_Z::MoveRight()
{
	bool bSuccess = false;
	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		if (m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos) && 
			m_pParentMatrix->IsCellFree(m_iXPos+2,m_iYPos+1))
		{
			m_iXPos++;
			bSuccess = true;
		}
		break;

	case Rotation90:
	case Rotation270:
		if (m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos-1) &&
			m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos  ) &&
			m_pParentMatrix->IsCellFree(m_iXPos,m_iYPos+1) )
		{
			m_iXPos++;
			bSuccess = true;
		}
		break;
	}
	return bSuccess;
}

bool CTetrad_Z::MoveDown()
{
	bool bSuccess = false;
	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		if (m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos+1) &&
			m_pParentMatrix->IsCellFree(m_iXPos  ,m_iYPos+2) &&
			m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos+2) )
		{
			m_iYPos++;
			bSuccess = true;
		}
		break;

	case Rotation90:
	case Rotation270:
		if (m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos+2) &&
			m_pParentMatrix->IsCellFree(m_iXPos,m_iYPos+1))
		{
			m_iYPos++;
			bSuccess = true;
		}
		break;
	}
	return bSuccess;
}

void CTetrad_Z::Draw()
{
	int screenX=0, screenY=0; 

	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos-1,m_iYPos,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos  ,m_iYPos,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos  ,m_iYPos+1,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos+1,m_iYPos+1,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		break;

	case Rotation90:
	case Rotation270:
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos  ,m_iYPos-1,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos  ,m_iYPos  ,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos-1,m_iYPos  ,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		m_pParentMatrix->GetScreenPosFromCell(m_iXPos-1,m_iYPos+1,screenX,screenY);
		m_pBlockImg->BlitImage(screenX,screenY);
		break;
	}
}

void CTetrad_Z::DrawOnScreen(const TRectanglei& rect)
{
	int XPos = (rect.GetWidth() - 3*BLOCK_WIDTH)/2 + rect.m_Left;
	int YPos = (rect.GetHeight() - 2*BLOCK_HEIGHT)/2 + rect.m_Top;
	m_pBlockImg->BlitImage(XPos,YPos);
	m_pBlockImg->BlitImage(XPos+BLOCK_WIDTH,YPos);
	m_pBlockImg->BlitImage(XPos+BLOCK_WIDTH,YPos+BLOCK_HEIGHT);
	m_pBlockImg->BlitImage(XPos+2*BLOCK_WIDTH,YPos+BLOCK_HEIGHT);
}

void CTetrad_Z::FillMatrix()
{
	switch (m_Orientation)
	{
	case Rotation0:
	case Rotation180:
		m_pParentMatrix->FillCell(m_iXPos-1,m_iYPos,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos  ,m_iYPos,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos  ,m_iYPos+1,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos+1,m_iYPos+1,m_BlockColor);
		break;

	case Rotation90:
	case Rotation270:
		m_pParentMatrix->FillCell(m_iXPos  ,m_iYPos-1,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos  ,m_iYPos  ,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos-1,m_iYPos  ,m_BlockColor);
		m_pParentMatrix->FillCell(m_iXPos-1,m_iYPos+1,m_BlockColor);
		break;
	}
}

bool CTetrad_Z::IsValid()
{
	bool bValid = false;
	if (m_pParentMatrix->IsCellFree(m_iXPos-1,m_iYPos) &&
		m_pParentMatrix->IsCellFree(m_iXPos  ,m_iYPos) &&
		m_pParentMatrix->IsCellFree(m_iXPos  ,m_iYPos+1) &&
		m_pParentMatrix->IsCellFree(m_iXPos+1,m_iYPos+1) )
	{
		bValid = true;
	}

	return bValid;
}

2、CTetradFactory类的实现

CTetradFactory类用来生成方块

// This class simply creates random tetrads.
class CTetradFactory
{
public:
	CTetradFactory();
	~CTetradFactory();

	CTetrad* CreateTetrad(CBlocksMatrix* pMatrix);
};

CTetradFactory::CTetradFactory()
{
	//time(NULL):1970年1月1日00:00:00以来的秒数
	//设置随机数种子
	srand ( (unsigned int)time(NULL) );
}

CTetradFactory::~CTetradFactory()
{
}

CTetrad* CTetradFactory::CreateTetrad(CBlocksMatrix* pMatrix)
{
	CTetrad* pNewTetrad = NULL;

	int value = rand()%7;
	switch (value)
	{
	case 0:
		pNewTetrad = new CTetrad_I(pMatrix);
		break;
	case 1:
		pNewTetrad = new CTetrad_O(pMatrix);
		break;
	case 2:
		pNewTetrad = new CTetrad_S(pMatrix);
		break;
	case 3:
		pNewTetrad = new CTetrad_Z(pMatrix);
		break;
	case 4:
		pNewTetrad = new CTetrad_T(pMatrix);
		break;
	case 5:
		pNewTetrad = new CTetrad_J(pMatrix);
		break;
	case 6:
		pNewTetrad = new CTetrad_L(pMatrix);
		break;
	}

	return pNewTetrad;
}

简单应用:
	// The tetrad factory
	CTetradFactory m_TetradFactory;

	m_pTetrad = m_TetradFactory.CreateTetrad(this);
	m_pNextShape = m_TetradFactory.CreateTetrad(this);

3、CComboControl类的实现

CComboControl类是一个辅助,增加游戏的趣味性。在相应的时间消掉一行,m_iMultiplier就会增加,相应的分数也会成倍增加。

// Class which handles the combo control: this
// control shows a decreasing time bar when a line
// has been completed. If a new line is completed 
// before time is over, a multiplier is applied to the 
// points gained for the line(s) completed. The higher
// the multiplier, the fastest time will decrease.
class CComboControl
{
public:
	CComboControl(const TRectanglei& rectControl, CGameFont* pFont);
	~CComboControl();

	// Updates and draw the control
	void Update(unsigned long dwCurrentTime);
	void Draw();

	// Increases the current multiplier and starts
	// a new timer which depends of the multiplier
	void IncreaseMultiplier();
	int GetMultiplier() const  { return m_iMultiplier; } 

	void Reset();
	void Pause();
	void Unpause();

private:
	// Returns the total time available for 
	// the current multiplier.
	int GetMultiplierTime() const;

	// The current combo multiplier
	int m_iMultiplier;
	// Time left for the current combo
	int m_iTimeLeft;
	// Total time for the current combo
	int m_iTotalTime;

	// The last time at which the control was updated
	unsigned long m_iLastUpdate;
	// true if the control is paused (time doesn't
	// decrease anymore)
	bool m_bPaused;

	// The font used by the control
	CGameFont* m_pFont;
	// The rectangle of the control
	TRectanglei m_rectControl;

	TImagePtr m_pProgressBorder;
	TImagePtr m_pProgressFill;
};


CComboControl::CComboControl(const TRectanglei& rectControl, CGameFont* pFont) 
  : m_iMultiplier(1), m_iTimeLeft(0), m_iTotalTime(0), 
    m_iLastUpdate(0), m_bPaused(false), m_pFont(pFont),
	m_rectControl(rectControl)
{
	m_pProgressBorder = CImage::CreateImage("ProgressBar.png",TRectanglei(0,24,0,126));
	m_pProgressFill = CImage::CreateImage("ProgressBar.png",TRectanglei(27,51,0,126));
}

CComboControl::~CComboControl()
{
	if (m_pFont)
	{
		delete m_pFont;
		m_pFont = NULL;
	}
}

void CComboControl::Pause()
{
	m_bPaused = true;
}

void CComboControl::Unpause()
{
	m_iLastUpdate = GetCurrentTime();
	m_bPaused = false;
}

void CComboControl::Update(unsigned long dwCurrentTime)
{
	if (m_iMultiplier == 1)
		return;
	if (m_bPaused)
		return;

	int dwTimeDiff = dwCurrentTime - m_iLastUpdate;
	if (dwTimeDiff >= m_iTimeLeft)
	{
		m_iMultiplier--;
		if (m_iMultiplier != 1)
			m_iTimeLeft = m_iTotalTime = GetMultiplierTime();
		else
			m_iTimeLeft = m_iTotalTime = 0;
	}
	else
		m_iTimeLeft -= dwTimeDiff;
	m_iLastUpdate = dwCurrentTime;
}

void CComboControl::Draw()
{
	stringstream ssText;
	ssText << "Multiplier: X" << m_iMultiplier;
	m_pFont->DrawText(ssText.str(),m_rectControl.m_Left+25,
					  m_rectControl.m_Top+60,1.0f,0.588f,0.039f);

	float barPercent = 	0;
	if (m_iTotalTime!=0)
		barPercent = (float)m_iTimeLeft/m_iTotalTime;
	else
		barPercent = 0;

	TRectanglei fill(0,24,0,(int)(126*barPercent));
	m_pProgressFill->BlitImagePart(85,405,fill);
	m_pProgressBorder->BlitImage(85,405);
}

void CComboControl::IncreaseMultiplier()
{
	if (m_iMultiplier<10)
		m_iMultiplier++;
	m_iTotalTime = m_iTimeLeft = GetMultiplierTime();
	m_iLastUpdate = GetCurrentTime();
}

int CComboControl::GetMultiplierTime() const
{
	return 20000 - (m_iMultiplier-2)*2000;
}

void CComboControl::Reset()
{
	m_iMultiplier = 1;
	m_iTotalTime = m_iTimeLeft = 0;
	m_iLastUpdate = 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<ctime> #include<chrono> #include<string> #include<filesystem> #include<fstream> #include<sstream> #include<thread> #include<boost/filesystem.hpp> const uintmax_t MAX_LOGS_SIZE = 10ull * 1024ull * 1024ull * 1024ull; //const uintmax_t MAX_LOGS_SIZE = 10ull; void create_folder(std::string folder_name) { boost::filesystem::create_directory(folder_name); std::string sub_foldername=folder_name+"/logs_ros"; boost::filesystem::create_directory(sub_foldername); } std::string get_current_time() { auto now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); char buffer[20]; std::strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M", &parts); return buffer; } void check_logs_size() { std::string logs_path = "/home/sage/logs/"; boost::filesystem::path logs_dir(logs_path); std::uintmax_t total_size = 0; for (const auto& file : boost::filesystem::recursive_directory_iterator(logs_dir)) { if (boost::filesystem::is_regular_file(file)) { total_size += boost::filesystem::file_size(file); } } if (total_size > MAX_LOGS_SIZE) { boost::filesystem::path earliest_dir; std::time_t earliest_time = std::time(nullptr); for (const auto& dir : boost::filesystem::directory_iterator(logs_dir)) { if (boost::filesystem::is_directory(dir)) { std::string dir_name = dir.path().filename().string(); std::tm time_parts = {}; std::istringstream ss(dir_name); std::string part; std::getline(ss, part, '-'); time_parts.tm_year = std::stoi(part) - 1900; std::getline(ss, part, '-'); time_parts.tm_mon = std::stoi(part) - 1; std::getline(ss, part, '-'); time_parts.tm_mday = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_hour = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_min = std::stoi(part); std::time_t dir_time = std::mktime(&time_parts); if (dir_time < earliest_time) { earliest_time = dir_time; earliest_dir = dir.path(); } } } if (!earliest_dir.empty()) { boost::filesystem::remove_all(earliest_dir); } } } int main() { std::string logs_path = "/home/sage/logs/"; while (true) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); if (parts.tm_min % 10 == 0) { std::string folder_name = logs_path + get_current_time(); create_folder(folder_name); } check_logs_size(); std::this_thread::sleep_for(std::chrono::minutes(1)); } return 0; }修改为ros节点
06-09

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值