-
先初始化背景 InitBackGround
-
关于小方块的写入 WriteBlock2BackGround
-
关于小方块的清除 ClearBlock2BackGround
-
消行,以及能消除多少行RemoveRows
-
背景数据的获取GetBackGround
#pragma once
#include "CBlock.h"
typedef unsigned char(*BACKGROUND)[COL];
class CBackGround
{
public:
CBackGround();
CBackGround(const CBackGround& BackGround);
~CBackGround();
CBackGround& operator= (const CBackGround& BackGround);
public:
void InitBackGround();
void WriteBlock2BackGround(const CBlock& Block,size_t nRow,size_t nCol,unsigned char chCurType, unsigned char chNextType);
void ClearBlock2BackGround(const CBlock& Block, size_t nRow, size_t nCol, unsigned char chCurType);
const BACKGROUND GetBackGround()const;
size_t RemoveRows();
bool IsRemoveRow(int nRow);
void RemoveRow(int nRow);
size_t GetScore();
private:
unsigned char m_uchBackGround[ROW][COL] = {};
size_t m_uScore=0;
};
#include "CBackGround.h"
#include <memory.h>
CBackGround::CBackGround()
{
}
CBackGround::CBackGround(const CBackGround& BackGround)
{
*this = BackGround;
}
CBackGround::~CBackGround()
{
}
CBackGround& CBackGround::operator=(const CBackGround& BackGround)
{
memcpy(m_uchBackGround, BackGround.m_uchBackGround, sizeof(m_uchBackGround));
m_uScore = BackGround.m_uScore;
return *this;
}
void CBackGround::InitBackGround()
{
for (size_t y = 0; y < ROW; y++)
{
for (size_t x = 0; x < COL; x++)
{
if (y == ROW - 1 || x == 0 || x == COL - 1)
{
m_uchBackGround[y][x] = BLOCK_WAll;
}
else
{
m_uchBackGround[y][x] = BLOCK_SPACE;
}
}
}
}
void CBackGround::WriteBlock2BackGround(const CBlock& Block, size_t nRow, size_t nCol, unsigned char chCurType, unsigned char chNextType)
{
BLOCKDATE BlockDate = Block.GetCurrentBlock();
for (size_t y = 0; y < BLOCK_ROW; y++)
{
for (size_t x = 0; x < BLOCK_COL; x++)
{
if (BlockDate[y][x] == chCurType)
{
m_uchBackGround[y + nRow][x + nCol] = chNextType;
}
}
}
}
void CBackGround::ClearBlock2BackGround(const CBlock& Block, size_t nRow, size_t nCol, unsigned char chCurType)
{
BLOCKDATE BlockDate = Block.GetCurrentBlock();
for (size_t y = 0; y < BLOCK_ROW; y++)
{
for (size_t x = 0; x < BLOCK_COL; x++)
{
if (BlockDate[y][x] == chCurType)
{
m_uchBackGround[y + nRow][x + nCol] = BLOCK_SPACE;
}
}
}
}
const BACKGROUND CBackGround::GetBackGround() const
{
return BACKGROUND(m_uchBackGround);
}
size_t CBackGround::RemoveRows()
{
size_t nCount = 0;
for (size_t y = ROW - 2; y > 3; y--)
{
if (IsRemoveRow(y))
{
RemoveRow(y);
nCount++;
y++;
}
}
m_uScore = m_uScore + nCount;
return nCount;
}
bool CBackGround::IsRemoveRow(int nRow)
{
for (size_t x = 1; x < COL -1; x++)
{
if (m_uchBackGround[nRow][x] == BLOCK_SPACE)
{
return false;
}
}
return true;
}
void CBackGround::RemoveRow(int nRow)
{
for (size_t y = nRow; y > 3; y--)
{
memcpy(&m_uchBackGround[y][0], &m_uchBackGround[y - 1][0], COL);
}
}
size_t CBackGround::GetScore()
{
return m_uScore;
}