基于Pierre Dellacherie的俄罗斯方块-03背景

  1. 先初始化背景 InitBackGround

  2. 关于小方块的写入 WriteBlock2BackGround

  3. 关于小方块的清除 ClearBlock2BackGround

  4. 消行,以及能消除多少行RemoveRows

  5. 背景数据的获取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();
	//写入Block
	void WriteBlock2BackGround(const CBlock& Block,size_t nRow,size_t nCol,unsigned char chCurType, unsigned char chNextType);
	//清除Block
	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)
{
    // TODO: 在此处插入 return 语句
    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) //BLCOK_MOVE   ==>    1
            {
                m_uchBackGround[y + nRow][x + nCol] = chNextType; //BLCOK_MOVE   ==>    1    BLOCK_FIXED   4
            }

        }
    }

}

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) //BLCOK_MOVE   ==>    1
            {
                m_uchBackGround[y + nRow][x + nCol] = BLOCK_SPACE; //BLCOK_MOVE   ==>    1    BLOCK_FIXED   4
            }

        }
    }
}

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值