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

📄 eluoshiview.cpp

📁 看书后
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// eluoshiView.cpp : implementation of the CEluoshiView class
//

#include "stdafx.h"
#include "eluoshi.h"

#include "eluoshiDoc.h"
#include "eluoshiView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//#define LEFT 0
//#define RIGHT 1
//#define UP 2
//#define DOWN 3
/////////////////////////////////////////////////////////////////////////////
// CEluoshiView

IMPLEMENT_DYNCREATE(CEluoshiView, CView)

BEGIN_MESSAGE_MAP(CEluoshiView, CView)
//{{AFX_MSG_MAP(CEluoshiView)
ON_COMMAND(ID_GAME_START, OnGameStart)
ON_COMMAND(ID_GAME_PAUSH, OnGamePaush)
ON_COMMAND(ID_GAME_END, OnGameEnd)
ON_WM_TIMER()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEluoshiView construction/destruction

CEluoshiView::CEluoshiView()
{
	// TODO: add construction code here
    GameInitnal();

}

CEluoshiView::~CEluoshiView()
{
	DCEnvClear();
}

BOOL CEluoshiView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEluoshiView drawing

void CEluoshiView::OnDraw(CDC* pDC)
{
	CEluoshiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	DcEnvInitial();
	DrawGame(&m_memDC);
	pDC->BitBlt(0,0,m_nWidth,m_nHeight,&m_memDC,0,0,SRCCOPY);
}

/////////////////////////////////////////////////////////////////////////////
// CEluoshiView printing

BOOL CEluoshiView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEluoshiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEluoshiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEluoshiView diagnostics

#ifdef _DEBUG
void CEluoshiView::AssertValid() const
{
	CView::AssertValid();
}

void CEluoshiView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CEluoshiDoc* CEluoshiView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEluoshiDoc)));
	return (CEluoshiDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEluoshiView message handlers

void CEluoshiView::OnGameStart() 
{
	// TODO: Add your command handler code here
	GameStatus[0][8] = 1;
	GameStatus[1][8] = 1;
	GameStatus[2][8] = 1;
	GameStatus[3][8] = 1;
	//CRect rt(m_iStartY+120,m_iStartX,m_iStartY+180,m_iStartX+60);
	//InvalidateRect(&rt);
	//UpdateWindow();
	InvalidateCurrent();
	m_bGamePaush = FALSE;
	SetTimer(1,1500-250*m_iLevel,NULL);
}

void CEluoshiView::OnGamePaush() 
{
	// TODO: Add your command handler code here
	m_bGamePaush = -m_bGamePaush;
	if(m_bGamePaush)
		KillTimer(1);
	else 
	SetTimer(1,1500-250*m_iLevel,NULL);
}

void CEluoshiView::OnGameEnd() 
{
	// TODO: Add your command handler code here
	m_bGamePaush = FALSE;
	m_bGameEnd = TRUE;
		KillTimer(1);
}

void CEluoshiView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int i,j,k;
	if(m_isBottom)
	{
		m_icurrentStatus = m_inextStatus;
		m_inextStatus = Random(7);
		RectStatusToNextStatus(m_inextStatus);
		//CRect rect(m_iStartY+320,m_iStartX,m_iStartY+440,m_iStartX+160);
		//InvalidateRect(&rect);
		//UpdateWindow();
		//InvalidateCurrent();
		m_currentRect = m_icurrentStatus;
		RectStatusToActiveStatus(m_icurrentStatus);
		ActiveStatusToGameStatus();
        //CRect rect1(m_iStartY+120,m_iStartX,m_iStartY+180,m_iStartX+60);
		//InvalidateRect(&rect1);
		//UpdateWindow();
		InvalidateCurrent();
		m_isBottom = FALSE;
		ISBottom();
		if(m_isBottom)
		{
			for(i=0;i<m_iCol;i++)
			{
				if(GameStatus[0][i])
				{
					KillTimer(1);
					AfxMessageBox("游戏结束!");
					for(j=0;j<m_iRow;j++)
					{
						for(k=0;k<m_iCol;k++)
						{
							GameStatus[j][k] = 0;
						}
					}
                    Invalidate(FALSE);
					m_bGameEnd = TRUE;
					break;
				}
			}
		}
	}
	else
		RectDown();
	CView::OnTimer(nIDEvent);
}

int CEluoshiView::Random(int MaxNumber)
{
    srand((unsigned)time(NULL));
	int random = rand() % MaxNumber;
	if(random == 0)
		random++;
	return random;
}

void CEluoshiView::RectStatusToNextStatus(int m_which)
{
	switch(m_which)
	{
	case 1:
		NextStatus[0][0] = 0;NextStatus[0][1] = 1;
		NextStatus[1][0] = 1;NextStatus[1][1] = 1;
		NextStatus[2][0] = 2;NextStatus[2][1] = 1;
		NextStatus[3][0] = 3;NextStatus[3][1] = 1;
		break;
	case 2:
		NextStatus[0][0] = 0;NextStatus[0][1] = 1;
		NextStatus[1][0] = 1;NextStatus[1][1] = 1;
		NextStatus[2][0] = 0;NextStatus[2][1] = 2;
		NextStatus[3][0] = 1;NextStatus[3][1] = 2;
		break;
	case 3:
		NextStatus[0][0] = 1;NextStatus[0][1] = 0;
		NextStatus[1][0] = 0;NextStatus[1][1] = 1;
		NextStatus[2][0] = 1;NextStatus[2][1] = 1;
		NextStatus[3][0] = 1;NextStatus[3][1] = 2;
		break;
	case 4:
		NextStatus[0][0] = 0;NextStatus[0][1] = 1;
		NextStatus[1][0] = 1;NextStatus[1][1] = 1;
		NextStatus[2][0] = 1;NextStatus[2][1] = 2;
		NextStatus[3][0] = 2;NextStatus[3][1] = 2;
		break;
	case 5:
		NextStatus[0][0] = 1;NextStatus[0][1] = 1;
		NextStatus[1][0] = 2;NextStatus[1][1] = 1;
		NextStatus[2][0] = 0;NextStatus[2][1] = 2;
		NextStatus[3][0] = 1;NextStatus[3][1] = 2;
		break;
	case 6:
		NextStatus[0][0] = 0;NextStatus[0][1] = 1;
		NextStatus[1][0] = 0;NextStatus[1][1] = 2;
		NextStatus[2][0] = 1;NextStatus[2][1] = 2;
		NextStatus[3][0] = 2;NextStatus[3][1] = 2;
		break;
	case 7:
		NextStatus[0][0] = 0;NextStatus[0][1] = 1;
		NextStatus[1][0] = 1;NextStatus[1][1] = 1;
		NextStatus[2][0] = 2;NextStatus[2][1] = 1;
		NextStatus[3][0] = 0;NextStatus[3][1] = 2;
		break;
	}
}

void CEluoshiView::RectStatusToActiveStatus(int m_which)
{
	switch(m_which)
	{
	case 1:
		ActiveStatus[0][0] = 0;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 1;ActiveStatus[1][1] = 8;
		ActiveStatus[2][0] = 2;ActiveStatus[2][1] = 8;
		ActiveStatus[3][0] = 3;ActiveStatus[3][1] = 8;
		break;
	case 2:
		ActiveStatus[0][0] = 0;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 1;ActiveStatus[1][1] = 8;
		ActiveStatus[2][0] = 0;ActiveStatus[2][1] = 9;
		ActiveStatus[3][0] = 1;ActiveStatus[3][1] = 9;
		break;
	case 3:
		ActiveStatus[0][0] = 1;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 0;ActiveStatus[1][1] = 9;
		ActiveStatus[2][0] = 1;ActiveStatus[2][1] = 9;
		ActiveStatus[3][0] = 1;ActiveStatus[3][1] = 10;
		break;
	case 4:
		ActiveStatus[0][0] = 0;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 1;ActiveStatus[1][1] = 8;
		ActiveStatus[2][0] = 1;ActiveStatus[2][1] = 9;
		ActiveStatus[3][0] = 2;ActiveStatus[3][1] = 9;
		break;
	case 5:
		ActiveStatus[0][0] = 1;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 2;ActiveStatus[1][1] = 8;
		ActiveStatus[2][0] = 0;ActiveStatus[2][1] = 9;
		ActiveStatus[3][0] = 1;ActiveStatus[3][1] = 9;
		break;
	case 6:
		ActiveStatus[0][0] = 0;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 0;ActiveStatus[1][1] = 9;
		ActiveStatus[2][0] = 1;ActiveStatus[2][1] = 9;
		ActiveStatus[3][0] = 2;ActiveStatus[3][1] = 9;
		break;
	case 7:
		ActiveStatus[0][0] = 0;ActiveStatus[0][1] = 8;
		ActiveStatus[1][0] = 1;ActiveStatus[1][1] = 8;
		ActiveStatus[2][0] = 2;ActiveStatus[2][1] = 8;
		ActiveStatus[3][0] = 0;ActiveStatus[3][1] = 9;
		break;
		
	}
	
}

void CEluoshiView::ActiveStatusToGameStatus()
{
	int x1,x2,x3,x4,y1,y2,y3,y4;
	x1 = ActiveStatus[0][0];
	x2 = ActiveStatus[1][0];
	x3 = ActiveStatus[2][0];
	x4 = ActiveStatus[3][0];
	y1 = ActiveStatus[0][1];
	y2 = ActiveStatus[1][1];
    y3 = ActiveStatus[2][1];
	y4 = ActiveStatus[3][1];
	GameStatus[x1][y1] = MAP_STATE_NOT_EMPTY;
	GameStatus[x2][y2] = MAP_STATE_NOT_EMPTY;
	GameStatus[x3][y3] = MAP_STATE_NOT_EMPTY;
	GameStatus[x4][y4] = MAP_STATE_NOT_EMPTY;
}

void CEluoshiView::ISBottom()
{
	int x1,x2,x3,x4;
	int x,xx,yy,i;
	x1 = ActiveStatus[0][0];
	x2 = ActiveStatus[1][0];
	x3 = ActiveStatus[2][0];
	x4 = ActiveStatus[3][0];
	if(x1>=m_iRow -1 || x2>=m_iRow -1 ||x3>=m_iRow -1 ||x4>=m_iRow -1)
		m_isBottom = TRUE;
	else
	{
		for(i =0;i<4;i++)
		{
			if(InterFace[m_currentRect][i] > -1)
			{
				x = InterFace[m_currentRect][i];
				xx = ActiveStatus[x][0]+1;
				yy = ActiveStatus[x][1];
				if(GameStatus[xx][yy] == MAP_STATE_NOT_EMPTY)
					m_isBottom = TRUE;
			}
		}
	}
	BOOL m_bIsSucced;
	int k,j;
	int m_iMuch = 0;
	if(m_isBottom)
	{
		for(i=0;i<m_iRow;i++)
		{
			m_bIsSucced = TRUE;
			for(j=0;j<m_iCol;j++)
			{
				if(GameStatus[i][j] == MAP_STATE_EMPTY)
					m_bIsSucced = FALSE;
				
			}
			if(m_bIsSucced)
			{
				for(k =i;k>0;k--)
					for(j=0;j<m_iCol;j++)
						GameStatus[k][j] = GameStatus[k-1][j];
					for(j=0;j<m_iCol;j++)
						GameStatus[0][j] = MAP_STATE_EMPTY;
					m_iMuch += 1;
			}
		}
		if(m_iMuch >0)
		{
			m_iPerformance += m_iMuch *100;
			//CRect rect1(m_iStartY,m_iStartX,m_iStartY+300,m_iStartX+360);
			//InvalidateRect(&rect1);
			//UpdateWindow();
			//InvalidateCurrent();
			//CRect rect2(m_iStartY+320,m_iStartX+180,m_iStartY+440,m_iStartX+200);
 		    //InvalidateRect(&rect2);
			//UpdateWindow();
			InvalidateCurrent();
			
		}
	}
}

void CEluoshiView::RectDown()
{
	ISBottom();
	if(!m_isBottom)
	{
		int x1,x2,x3,x4,y1,y2,y3,y4;
		x1 = ActiveStatus[0][0];
		x2 = ActiveStatus[1][0];
		x3 = ActiveStatus[2][0];
		x4 = ActiveStatus[3][0];
		y1 = ActiveStatus[0][1];
		y2 = ActiveStatus[1][1];
		y3 = ActiveStatus[2][1];
		y4 = ActiveStatus[3][1];
		GameStatus[x1][y1] = MAP_STATE_EMPTY;
		GameStatus[x2][y2] = MAP_STATE_EMPTY;
		GameStatus[x3][y3] = MAP_STATE_EMPTY;
		GameStatus[x4][y4] = MAP_STATE_EMPTY;
		InvalidateCurrent();
		ActiveStatus[0][0] += 1;
		ActiveStatus[1][0] += 1;
		ActiveStatus[2][0] += 1;
		ActiveStatus[3][0] += 1;
		GameStatus[x1+1][y1] = MAP_STATE_NOT_EMPTY;
		GameStatus[x2+1][y2] = MAP_STATE_NOT_EMPTY;
		GameStatus[x3+1][y3] = MAP_STATE_NOT_EMPTY;
		GameStatus[x4+1][y4] = MAP_STATE_NOT_EMPTY;
		InvalidateCurrent();
	}
}

void CEluoshiView::InvalidateCurrent()
{
	/*int i;
	for(i =0;i<4;i++)
	{
		CRect rect(m_iStartX+ActiveStatus[i][1]*m_iLarge,
			m_iStartY+ActiveStatus[i][0]*m_iLarge,
			m_iStartX+(ActiveStatus[i][1]+1)*m_iLarge,
			m_iStartY+(ActiveStatus[i][0]+1)*m_iLarge);
		InvalidateRect(&rect);
		UpdateWindow();
	}*/
	CDC* pDC = GetDC();
	DrawGame(pDC);
}

void CEluoshiView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nChar)
	{
	case  VK_LEFT:
		RectArrow(LEFT);
		break;
	case  VK_RIGHT:
		RectArrow(RIGHT);
		break;
	case  VK_UP:
		RectArrow(UP);
		break;
	case  VK_DOWN:
		RectArrow(DOWN);
		break;
		
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CEluoshiView::RectArrow(int m_Type)
{
	int x1,x2,x3,x4,y1,y2,y3,y4;
	   x1 = ActiveStatus[0][0];
	   x2 = ActiveStatus[1][0];
	   x3 = ActiveStatus[2][0];
	   x4 = ActiveStatus[3][0];
	   y1 = ActiveStatus[0][1];
	   y2 = ActiveStatus[1][1];
	   y3 = ActiveStatus[2][1];
	   y4 = ActiveStatus[3][1]; 
	   switch(m_Type)
	   {
	   case LEFT:
		   if((ActiveStatus[0][1] > 0) && IsLeftLimit() && !m_isBottom)
		   {
			   GameStatus[x1][y1] = MAP_STATE_EMPTY;
			   GameStatus[x2][y2] = MAP_STATE_EMPTY;
			   GameStatus[x3][y3] = MAP_STATE_EMPTY;
			   GameStatus[x4][y4] = MAP_STATE_EMPTY;
			   InvalidateCurrent();
			   ActiveStatus[0][1] -= 1;
			   ActiveStatus[1][1] -= 1;
			   ActiveStatus[2][1] -= 1;
			   ActiveStatus[3][1] -= 1;
			   GameStatus[x1][y1-1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x2][y2-1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x3][y3-1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x4][y4-1] = MAP_STATE_NOT_EMPTY;
			   InvalidateCurrent();
			
		   } 
		   break;
	   case RIGHT:
		   if((ActiveStatus[3][1] < m_iCol-1) && IsRightLimit() && !m_isBottom)
		   {
			   GameStatus[x1][y1] = MAP_STATE_EMPTY;
			   GameStatus[x2][y2] = MAP_STATE_EMPTY;
			   GameStatus[x3][y3] = MAP_STATE_EMPTY;
			   GameStatus[x4][y4] = MAP_STATE_EMPTY;
			   InvalidateCurrent();
			   ActiveStatus[0][1] += 1;
			   ActiveStatus[1][1] += 1;
			   ActiveStatus[2][1] += 1;
			   ActiveStatus[3][1] += 1;
			   GameStatus[x1][y1+1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x2][y2+1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x3][y3+1] = MAP_STATE_NOT_EMPTY;
			   GameStatus[x4][y4+1] = MAP_STATE_NOT_EMPTY;
			   InvalidateCurrent();
			  
		   } 
		   break;
	   case DOWN:
		   RectDown();
		   break;
	   case UP:
		   RectTangle();
		   break;

	   }
}

BOOL CEluoshiView::IsLeftLimit()

⌨️ 快捷键说明

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