⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 russian2.cpp

📁 一个可以在嵌入式平台上面开发的俄罗斯方块程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "resource.h"
#include "russian2.h"

extern int array[xGrid][yGrid];//save two dimension array's status.	
extern  struct Calcul_Score_Level{//计算分数和等级
	int TotalScore;//TotalScore:总分
	int UnitScore;//UnitScore:基分,每消掉一行时为50分,每落一次但不消行为10分
	int nRemoveRow;//统计一次消去的行数(当同时填满多行时,可成倍加分).消行结束时,将其复位为0
	int nTotalRemoveRow;//统计总共消去的行数(用于进行升等级计算的依据:当该值达到20行时升
						//一级,同时将其复位为0)
	int Level;//记录当前的游戏等级:最高设为10级
} Score_Level;


CSquareBase::CSquareBase()
{
	//every block's left-top coordinate.
	cx1=0;cy1=0;
	cx2=0;cy2=0;
	cx3=0;cy3=0;
	cx4=0;cy4=0;
	
	//set two dimension array's original status.
//	for(int i=0;i<xGrid;i++)
//	for(int j=0;j<yGrid;j++)
//		array[i][j]=0;

	//set array's subscript to zero.
	i1=0;
	j1=0;
	i2=0;
	j2=0;
	i3=0;
	j3=0;
	i4=0;
	j4=0;
}

CSquareBase::~CSquareBase()
{
}

void CSquareBase::EraseDesign(CDC *pDC)
{
	CBrush whiteBrush(RGB(255,255,255));

	CRect rect1(cx1,cy1,cx1+mWidth,cy1+mHeight);
	CRect rect2(cx2,cy2,cx2+mWidth,cy2+mHeight);
	CRect rect3(cx3,cy3,cx3+mWidth,cy3+mHeight);
	CRect rect4(cx4,cy4,cx4+mWidth,cy4+mHeight);

	pDC->FillRect(&rect1,&whiteBrush);
	pDC->FillRect(&rect2,&whiteBrush);
	pDC->FillRect(&rect3,&whiteBrush);
	pDC->FillRect(&rect4,&whiteBrush);
	
}

void CSquareBase::PaintDesign(CDC *pDC)
{
	CDC MemDC;
	CBitmap bmp;
	if(!bmp.LoadBitmap(IDB_BLOCK))
	{
		AfxMessageBox(_T("Cann't load bitmap!"));
		return;
	}
	MemDC.CreateCompatibleDC(pDC);
	CBitmap* pOrgBmp=MemDC.SelectObject(&bmp);

	pDC->BitBlt(cx1,cy1,mWidth,mHeight,&MemDC,0,0,SRCCOPY);
	pDC->BitBlt(cx2,cy2,mWidth,mHeight,&MemDC,0,0,SRCCOPY);
	pDC->BitBlt(cx3,cy3,mWidth,mHeight,&MemDC,0,0,SRCCOPY);
	pDC->BitBlt(cx4,cy4,mWidth,mHeight,&MemDC,0,0,SRCCOPY);

	MemDC.SelectObject(pOrgBmp);
	bmp.DeleteObject();
}

void CSquareBase::CalcStatus()
{
	i1=(cx1-ulPosx-frmwidth)/mWidth;
	j1=(cy1-ulPosy-frmwidth)/mHeight;
	
	i2=(cx2-ulPosx-frmwidth)/mWidth;
	j2=(cy2-ulPosy-frmwidth)/mHeight;

	i3=(cx3-ulPosx-frmwidth)/mWidth;
	j3=(cy3-ulPosy-frmwidth)/mHeight;

	i4=(cx4-ulPosx-frmwidth)/mWidth;
	j4=(cy4-ulPosy-frmwidth)/mHeight;

	
	if(i1>(xGrid-1)||i2>(xGrid-1)||i3>(xGrid-1)||i4>(xGrid-1))
		{i1-=1;i2-=1;i3-=1;i4-=1;}

	if(j1>(yGrid-1)||j2>(yGrid-1)||j3>(yGrid-1)||j4>(yGrid-1))
		{j1-=1;j2-=1;j3-=1;j4-=1;}
	int t=0;
}

void CSquareBase::DetectFill(CDC *pDC)
{
	Score_Level.nRemoveRow=0;
	CBrush whiteBrush(RGB(255,255,255));
	CDC MemDC;
	CBitmap bmp;
	if(!bmp.LoadBitmap(IDB_BLOCK))
	{
		AfxMessageBox(_T("Cann't load bitmap!"));
		return;
	}
	MemDC.CreateCompatibleDC(pDC);
	CBitmap* pOrgBmp=MemDC.SelectObject(&bmp);

	for(int i=0;i<yGrid;i++)
	{
		bool IsFilled=TRUE;
		for(int j=0;j<=xGrid-1;j++)
		{
			if(array[j][i]==0)
			{
				IsFilled=FALSE;
				break;
			}
		}
		
		if(IsFilled)
		{
			Score_Level.nRemoveRow++;//消行计数器:每消一行加1
			Score_Level.UnitScore=Score_Level.nRemoveRow*50;//每消掉一行加50分,每落一次但不消行加10分
			CRect rc1(ulPosx+frmwidth,i*mHeight+ulPosy+frmwidth,
					  ulPosx+xGrid*mWidth+frmwidth,i*mHeight+ulPosy+frmwidth+mHeight);
			pDC->FillRect(&rc1,&whiteBrush);
			
			for(int k=i;k>=1;k--)
			{
				for(int l=0;l<=xGrid-1;l++)
				{
					array[l][k]=array[l][k-1];
				}
			}
			for(int n=0;n<=i;n++)
			{
				for(int m=0;m<=xGrid-1;m++)
				{
					if(array[m][n]==1)
						pDC->BitBlt(m*mWidth+ulPosx+frmwidth,n*mHeight+ulPosy+frmwidth,mWidth,mHeight,&MemDC,0,0,SRCCOPY);
					else
					{
						CRect rect(m*mWidth+ulPosx+frmwidth,n*mHeight+ulPosy+frmwidth,
                                      m*mWidth+ulPosx+frmwidth+mWidth,n*mHeight+ulPosy+frmwidth+mHeight);
						pDC->FillRect(&rect,&whiteBrush);
					}
				}
			}
		}
	}
	Score_Level.nTotalRemoveRow+=Score_Level.nRemoveRow;//统计当前消去的总行数
	MemDC.SelectObject(pOrgBmp);
	bmp.DeleteObject();
}

void CSquareBase::Left(CDC *pDC)
{
	EraseDesign(pDC);
	
	cx1-=mWidth;
	cx2-=mWidth;
	cx3-=mWidth;
	cx4-=mWidth;
	
	CalcStatus();
	//If collide with other squareness or exceed extent:
	if(cx1<(ulPosx+frmwidth)||
		cx2<(ulPosx+frmwidth)||
		cx3<(ulPosx+frmwidth)||
		cx4<(ulPosx+frmwidth)||
		(array[i1][j1]==1)||
		(array[i2][j2]==1)||
		(array[i3][j3]==1)||
		(array[i4][j4]==1))
	{
		cx1+=mWidth;
		cx2+=mWidth;
		cx3+=mWidth;
		cx4+=mWidth;

		PaintDesign(pDC);
	}
	else
	PaintDesign(pDC);
}

void CSquareBase::Right(CDC *pDC)
{
	EraseDesign(pDC);
	
	cx1+=mWidth;
	cx2+=mWidth;
	cx3+=mWidth;
	cx4+=mWidth;
	
	CalcStatus();
	//If collide with other squareness or exceed extent:
	if (cx1>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx2>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx3>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx4>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		(array[i1][j1]==1)||
		(array[i2][j2]==1)||
		(array[i3][j3]==1)||
		(array[i4][j4]==1))
	{
		cx1-=mWidth;
		cx2-=mWidth;
		cx3-=mWidth;
		cx4-=mWidth;

		PaintDesign(pDC);
	}
	else
	PaintDesign(pDC);
}

//not integrity:
void CSquareBase::Rotate(CDC *pDC)
{
	EraseDesign(pDC);
	return;
}

void CSquareBase::Init()
{
		cx1=0;cy1=0;
		cx2=0;cy2=0;
		cx3=0;cy3=0;
		cx4=0;cy4=0;
		i1=0;
		j1=0;
		i2=0;
		j2=0;
		i3=0;
		j3=0;
		i4=0;
		j4=0;
}

//返回值:0:不能继续下落(遇到障碍物或到底,但方格未顶到头)
//		 1:可以继续下落(未遇到障碍物也未到底)
//		 2:方格已顶到头,游戏结束
int CSquareBase::Down(CDC *pDC)
{
	Score_Level.UnitScore=10;

	EraseDesign(pDC);
	cy1+=mHeight;
	cy2+=mHeight;
	cy3+=mHeight;
	cy4+=mHeight;
	
	CalcStatus();
	//If collide with other squareness or exceed extent:	
	if (cy1>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy2>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy3>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy4>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		(array[i1][j1]==1)||
		(array[i2][j2]==1)||
		(array[i3][j3]==1)||
		(array[i4][j4]==1))
		{
		cy1-=mHeight;
		cy2-=mHeight;
		cy3-=mHeight;
		cy4-=mHeight;

		PaintDesign(pDC);

		array[(cx1-ulPosx-frmwidth)/mWidth][(cy1-ulPosy-frmwidth)/mHeight]=1;
		array[(cx2-ulPosx-frmwidth)/mWidth][(cy2-ulPosy-frmwidth)/mHeight]=1;
		array[(cx3-ulPosx-frmwidth)/mWidth][(cy3-ulPosy-frmwidth)/mHeight]=1;
		array[(cx4-ulPosx-frmwidth)/mWidth][(cy4-ulPosy-frmwidth)/mHeight]=1;

		DetectFill(pDC);
		for(int i=0;i<xGrid;i++)//扫描第一行,如有一个为1,则方格已到头,游戏结束
		{
			if(array[i][0]==1)
				return 2;
		}
		Score_Level.TotalScore+=Score_Level.UnitScore;//加分
		
		
		Init();
		return 0;
		}
	else
		{
		PaintDesign(pDC);
		}
return 1;
}

////////////////////////////////////////////////////////////////////
//derived class overload member function:
//CSquare1:
CSquare1::CSquare1()
{
	//1	2
	//3	4
	status=1;
	cx1=ulPosx+frmwidth+(xGrid/2)*mWidth;
	cy1=ulPosy+frmwidth;
	cx2=cx1+mWidth;
	cy2=cy1;
	cx3=cx1;
	cy3=cy2+mHeight;
	cx4=cx2;
	cy4=cy3;
}
		
CSquare1::~CSquare1()
{
	;
}

void CSquare1::Init()
{
		status=1;
		cx1=ulPosx+frmwidth+(xGrid/2)*mWidth;
			cy1=ulPosy+frmwidth;
			cx2=cx1+mWidth;
			cy2=cy1;
			cx3=cx1;
			cy3=cy2+mHeight;
			cx4=cx2;
			cy4=cy3;
	
}

void CSquare1::Rotate(CDC *pDC)
{
	CSquareBase::Rotate(pDC);

}

/////////////////////////////////////////////////////////////////////
//CSquare2:
CSquare2::CSquare2()
{
	//1	 2	3  4
	status=1;
	cx1=ulPosx+frmwidth+(xGrid/2)*mWidth;
	cy1=ulPosy+frmwidth;
	cx2=cx1+mWidth;
	cy2=cy1;
	cx3=cx1+mWidth*2;
	cy3=cy1;
	cx4=cx1+mWidth*3;
	cy4=cy1;
}
		
CSquare2::~CSquare2()
{
	;
}
void CSquare2::Init()
{
		status=1;
			cx1=ulPosx+frmwidth+(xGrid/2)*mWidth;
			cy1=ulPosy+frmwidth;
			cx2=cx1+mWidth;
			cy2=cy1;
			cx3=cx1+mWidth*2;
			cy3=cy1;
			cx4=cx1+mWidth*3;
			cy4=cy1;
	
}

BOOL CSquare2::CanRotate()
{
		CalcStatus();

	BOOL bRtn=1;
	int x1=cx1;
	int x2=cx2;
	int x3=cx3;
	int x4=cx4;
	int y1=cy1;
	int y2=cy2;
	int y3=cy3;
	int y4=cy4;
switch(status)
	{
	case 1:
		cx1=cx1+mWidth;
		cy1=cy1-mHeight;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3+mHeight;
		cx4=cx4-mWidth*2;
		cy4=cy4+mHeight*2;
		break;
	case 2:
		cx1=cx1-mWidth;
		cy1=cy1+mHeight;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3-mHeight;
		cx4=cx4+mWidth*2;
		cy4=cy4-mHeight*2;
		break;
	}

	CalcStatus();

	if (
		(array[i1][j1]==1)||
		(array[i2][j2]==1)||
		(array[i3][j3]==1)||
		(array[i4][j4]==1)||
		
		cy1>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy2>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy3>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy4>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		
		cy1<(ulPosy+frmwidth)||
		cy2<(ulPosy+frmwidth)||
		cy3<(ulPosy+frmwidth)||
		cy4<(ulPosy+frmwidth)||
		
		cx1>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx2>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx3>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx4>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		
		cx1<(ulPosx+frmwidth)||
		cx2<(ulPosx+frmwidth)||
		cx3<(ulPosx+frmwidth)||
		cx4<(ulPosx+frmwidth)
		)
		{
		bRtn=0;
		}
	cx1=x1;
	cx2=x2;
	cx3=x3;
	cx4=x4;
	cy1=y1;
	cy2=y2;
	cy3=y3;
	cy4=y4;

	CalcStatus();

	return bRtn;
}

void CSquare2::Rotate(CDC *pDC)
{
if(!CanRotate())return;
	CSquareBase::Rotate(pDC);
	switch(status)
	{
	case 1:
		status=2;
		cx1=cx1+mWidth;
		cy1=cy1-mHeight;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3+mHeight;
		cx4=cx4-mWidth*2;
		cy4=cy4+mHeight*2;
		break;
	case 2:
		status=1;
		cx1=cx1-mWidth;
		cy1=cy1+mHeight;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3-mHeight;
		cx4=cx4+mWidth*2;
		cy4=cy4-mHeight*2;
		break;
	}
	
	PaintDesign(pDC);
	
}

/////////////////////////////////////////////////////////////////////
//CSquare3:
CSquare3::CSquare3()
{
	//	 1
	//3	 2	4
	status=1;
	cx1=ulPosx+frmwidth+(xGrid/2+1)*mWidth; 
	cy1=ulPosy+frmwidth;
	cx2=cx1;
	cy2=cy1+mHeight;
	cx3=cx1-mWidth;
	cy3=cy2;
	cx4=cx1+mWidth;
	cy4=cy2;
		
}
		
CSquare3::~CSquare3()
{
	;
}
void CSquare3::Init()
{
	status=1;
	cx1=ulPosx+frmwidth+(xGrid/2+1)*mWidth; 
	cy1=ulPosy+frmwidth;
	cx2=cx1;
	cy2=cy1+mHeight;
	cx3=cx1-mWidth;
	cy3=cy2;
	cx4=cx1+mWidth;
	cy4=cy2;
		
		
}

BOOL CSquare3::CanRotate()
{
		CalcStatus();
	BOOL bRtn=1;
	int x1=cx1;
	int x2=cx2;
	int x3=cx3;
	int x4=cx4;
	int y1=cy1;
	int y2=cy2;
	int y3=cy3;
	int y4=cy4;
	switch(status)
	{
	case 1:
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3-mHeight;
		cx4=cx4-mWidth;
		cy4=cy4+mHeight;

		break;
	case 2:
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3+mHeight;
		cx4=cx4-mWidth;
		cy4=cy4-mHeight;
		break;
	case 3:
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3+mHeight;
		cx4=cx4+mWidth;
		cy4=cy4-mHeight;
		break;
	case 4:
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3-mHeight;
		cx4=cx4+mWidth;
		cy4=cy4+mHeight;
		break;
	}
	CalcStatus();

	if (
		(array[i1][j1]==1)||
		(array[i2][j2]==1)||
		(array[i3][j3]==1)||
		(array[i4][j4]==1)||
		
		cy1>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy2>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy3>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		cy4>(ulPosy+frmwidth+(yGrid-1)*mHeight)||
		
		cy1<(ulPosy+frmwidth)||
		cy2<(ulPosy+frmwidth)||
		cy3<(ulPosy+frmwidth)||
		cy4<(ulPosy+frmwidth)||
		
		cx1>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx2>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx3>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		cx4>(ulPosx+frmwidth+(xGrid-1)*mWidth)||
		
		cx1<(ulPosx+frmwidth)||
		cx2<(ulPosx+frmwidth)||
		cx3<(ulPosx+frmwidth)||
		cx4<(ulPosx+frmwidth)
		)
		{
		bRtn=0;
		}
	cx1=x1;
	cx2=x2;
	cx3=x3;
	cx4=x4;
	cy1=y1;
	cy2=y2;
	cy3=y3;
	cy4=y4;

	CalcStatus();

	return bRtn;
}

void CSquare3::Rotate(CDC *pDC)
{
	if(!CanRotate())return;
	CSquareBase::Rotate(pDC);
	switch(status)
	{
	case 1:
		status=2;
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3-mHeight;
		cx4=cx4-mWidth;
		cy4=cy4+mHeight;

		break;
	case 2:
		status=3;
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3+mWidth;
		cy3=cy3+mHeight;
		cx4=cx4-mWidth;
		cy4=cy4-mHeight;
		break;
	case 3:
		status=4;
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3+mHeight;
		cx4=cx4+mWidth;
		cy4=cy4-mHeight;
		break;
	case 4:
		status=1;
		cx1=cx4;
		cy1=cy4;
		cx2=cx2;
		cy2=cy2;
		cx3=cx3-mWidth;
		cy3=cy3-mHeight;
		cx4=cx4+mWidth;
		cy4=cy4+mHeight;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -