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

📄 hwrectview.cpp

📁 一个很好的俄罗斯方块游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// HWRECTView.cpp : implementation of the CHWRECTView class
//

#include "stdafx.h"
#include "HWRECT.h"

#include "HWRECTDoc.h"
#include "HWRECTView.h"
#include "OptionDlg.h"

#include "mmsystem.h"
#include "Midi.h"

#include "HWRECTDoc.h"
#include "HWRECTView.h"


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

/////////////////////////////////////////////////////////////////////////////
// CHWRECTView

IMPLEMENT_DYNCREATE(CHWRECTView, CView)

BEGIN_MESSAGE_MAP(CHWRECTView, CView)
	//{{AFX_MSG_MAP(CHWRECTView)
	ON_COMMAND(ID_GAME_START, OnGameStart)
	ON_COMMAND(ID_GAME_PAUSH, OnGamePaush)
	ON_COMMAND(ID_GAME_END, OnGameEnd)
	ON_COMMAND(ID_GAME_OPTION, OnGameOption)
	ON_COMMAND(ID_GAME_EXIT, OnGameExit)
	ON_COMMAND(ID_OPTION_AREA1, OnOptionArea1)
	ON_COMMAND(ID_OPTION_AREA2, OnOptionArea2)
	ON_COMMAND(ID_OPTION_AREA3, OnOptionArea3)
	ON_COMMAND(ID_OPTION_AREA4, OnOptionArea4)
	ON_COMMAND(ID_OPTION_LEVEL1, OnOptionLevel1)
	ON_COMMAND(ID_OPTION_LEVEL2, OnOptionLevel2)
	ON_COMMAND(ID_OPTION_LEVEL3, OnOptionLevel3)
	ON_COMMAND(ID_OPTION_LEVEL4, OnOptionLevel4)
	ON_COMMAND(ID_OPTION_LEVEL5, OnOptionLevel5)
	ON_COMMAND(ID_OPTION_LEVEL6, OnOptionLevel6)
	ON_COMMAND(ID_OPTION_GRID, OnOptionGrid)
	ON_COMMAND(ID_OPTION_MUSIC, OnOptionMusic)
	ON_COMMAND(ID_HELP_HELP, OnHelpHelp)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_UPDATE_COMMAND_UI(ID_GAME_START, OnUpdateGameStart)
	ON_UPDATE_COMMAND_UI(ID_GAME_END, OnUpdateGameEnd)
	ON_UPDATE_COMMAND_UI(ID_GAME_OPTION, OnUpdateGameOption)
	ON_UPDATE_COMMAND_UI(ID_GAME_EXIT, OnUpdateGameExit)
	ON_UPDATE_COMMAND_UI(ID_GAME_PAUSH, OnUpdateGamePaush)
	ON_UPDATE_COMMAND_UI(ID_OPTION_AREA1, OnUpdateOptionArea1)
	ON_UPDATE_COMMAND_UI(ID_OPTION_AREA2, OnUpdateOptionArea2)
	ON_UPDATE_COMMAND_UI(ID_OPTION_AREA3, OnUpdateOptionArea3)
	ON_UPDATE_COMMAND_UI(ID_OPTION_AREA4, OnUpdateOptionArea4)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL1, OnUpdateOptionLevel1)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL2, OnUpdateOptionLevel2)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL3, OnUpdateOptionLevel3)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL4, OnUpdateOptionLevel4)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL5, OnUpdateOptionLevel5)
	ON_UPDATE_COMMAND_UI(ID_OPTION_LEVEL6, OnUpdateOptionLevel6)
	ON_UPDATE_COMMAND_UI(ID_OPTION_GRID, OnUpdateOptionGrid)
	ON_UPDATE_COMMAND_UI(ID_OPTION_MUSIC, OnUpdateOptionMusic)
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_HELP_HELP, OnUpdateHelpHelp)
	ON_UPDATE_COMMAND_UI(ID_HELP_ABOUT, OnUpdateHelpAbout)
	ON_COMMAND(ID_HELP_TEACH, OnHelpTeach)
	ON_UPDATE_COMMAND_UI(ID_HELP_TEACH, OnUpdateHelpTeach)
	ON_COMMAND(IDC_BACK_MUSIC, OnBackMusic)
	ON_COMMAND(IDC_CLOSE_MUSIC, OnCloseMusic)
	ON_COMMAND(IDC_MUSIC_CYCLE, OnMusicCycle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHWRECTView construction/destruction

CHWRECTView::CHWRECTView()
{
	// TODO: add construction code here
	//缺省为不是游戏暂停状态
	m_bGamePaush = FALSE;

	//缺省为不插放背景音乐
	m_bMusic = FALSE;
	
	//缺省为画网格线
	m_bDrawGrid = TRUE;
	//总分值清零
	m_iPerformance = 0;

	//测试值:为12行,10列
	m_iRow = 12;
	m_iCol = 10;

	//左上角X,Y坐标
	m_iStartX = 10;
	m_iStartY = 10;

	//缺省级别为3级
	m_iLevel = 2;

	//缺省方块大小为m_iLarge个象素
	m_iLarge = 30;

	//缺省游戏是结束的
	m_bGameEnd = TRUE;

	int i,j;

	//赋初值
	for (i=0;i<100;i++)
		for (j=0;j<100;j++)
			GameStatus[i][j]=0;

	//各种形状方块的接触面数据,参见设计书的接触面表格, 如果某种形状的方块没有4个接触面,则后面的数据填-1
	for (i=0;i<74;i++)
		for (j=0;j<4;j++)
			InterFace[i][j] = -1;

	InterFace[1][0] = 3;
	
	InterFace[11][0] = 0;	InterFace[11][1] = 1;	InterFace[11][2] = 2;	InterFace[11][3] = 3;
  
	InterFace[2][0] = 1;	InterFace[2][1] = 3;
	
	InterFace[3][0] = 0;	InterFace[3][1] = 2;	InterFace[3][2] = 3;
	
	InterFace[31][0] = 2;	InterFace[31][1] = 3;
	
	InterFace[32][0] = 0;	InterFace[32][1] = 2;	InterFace[32][2] = 3;
	
	InterFace[33][0] = 0;	InterFace[33][1] = 3;
	
	InterFace[4][0] = 1;	InterFace[4][1] = 3;
	
	InterFace[41][0] = 0;	InterFace[41][1] = 2;	InterFace[41][2] = 3;
	
	InterFace[5][0] = 1;	InterFace[5][1] = 3;

	InterFace[51][0] = 0;	InterFace[51][1] = 2;	InterFace[51][2] = 3;
	
	InterFace[6][0] = 0;	InterFace[6][1] = 3;
	
	InterFace[61][0] = 1;	InterFace[61][1] = 2;	InterFace[61][2] = 3;

	InterFace[62][0] = 2;	InterFace[62][1] = 3;

	InterFace[63][0] = 0;	InterFace[63][1] = 1;	InterFace[63][2] = 3;

	InterFace[7][0] = 2;	InterFace[7][1] = 3;
	
	InterFace[71][0] = 1;	InterFace[71][1] = 2;	InterFace[71][2] = 3;

	InterFace[72][0] = 0;	InterFace[72][1] = 3;

	InterFace[73][0] = 0;	InterFace[73][1] = 1;	InterFace[73][2] = 3;

}

CHWRECTView::~CHWRECTView()
{
	delete (this);
}

BOOL CHWRECTView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHWRECTView drawing

void CHWRECTView::OnDraw(CDC* pDC)
{
	CHWRECTDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	int i,j;
	//CBrush *oldBrush = 
	pDC -> SelectObject(blackBrush);
	CRect rect;
	GetClientRect(&rect);
	pDC -> Rectangle(rect);

	pDC -> SelectObject(grayBrush);
	pDC -> Rectangle(m_iStartY ,m_iStartX, m_iStartY + 301, m_iStartX + 360);
	
	pDC -> SelectObject(blueBrush);

	//CPen *oldPen = 
	pDC->SelectObject(blackPen);
	
	//画网格线
	if (m_bDrawGrid)
	{
		//画横线
		for (i=0;i<m_iRow;i++)
		{
			pDC->MoveTo(m_iStartY, m_iStartX + i*m_iLarge);
			pDC->LineTo(m_iStartY+300, m_iStartX +i*m_iLarge);
		}	
		
		//画竖线
		for (i=0;i<m_iCol;i++)
		{
			pDC->MoveTo(m_iStartY+i*m_iLarge, m_iStartX);
			pDC->LineTo(m_iStartY+i*m_iLarge, m_iStartX+360);
		}
	}

	//画方块
	for (i=0;i<m_iRow;i++)
		for (j=0;j<m_iCol;j++)
		{
			if (GameStatus[i][j]==1)
			{
				//划的方块比规定的大小略微小一点
				//画小方块边框黑线
				pDC -> SelectObject(blackPen);
				pDC -> MoveTo(m_iStartY+j*m_iLarge +1, m_iStartX+i*m_iLarge +1);
				pDC -> LineTo(m_iStartY+j*m_iLarge +1, m_iStartX+(i+1)*m_iLarge -1);
				pDC -> MoveTo(m_iStartY+j*m_iLarge +1,	m_iStartX+(i+1)*m_iLarge -1);
				pDC -> LineTo(m_iStartY+(j+1)*m_iLarge -1, m_iStartX+(i+1)*m_iLarge -1);

				//画小方块边框白线
				pDC -> SelectObject(whitePen);
				pDC -> MoveTo(m_iStartY+j*m_iLarge +1, m_iStartX+i*m_iLarge +1);
				pDC -> LineTo(m_iStartY+(j+1)*m_iLarge -1,m_iStartX+i*m_iLarge +1);
				pDC -> MoveTo(m_iStartY+(j+1)*m_iLarge -1,m_iStartX+i*m_iLarge +1);
				pDC -> LineTo(m_iStartY+(j+1)*m_iLarge -1,m_iStartX+(i+1)*m_iLarge -1);

				//每个方块填充绿色的背景 
				pDC -> Rectangle(m_iStartY+j*m_iLarge +2,m_iStartX+i*m_iLarge +2,m_iStartY+(j+1)*m_iLarge -2,m_iStartX+(i+1)*m_iLarge -2);

			}
		}

	//显示游戏区域及游戏级别的汉字描述
	if (!m_bGameEnd)
	{
		pDC -> SetBkColor(BLACK);
		pDC -> SetTextColor(WHITE);
		pDC -> TextOut(m_iStartY+320, m_iStartX+220, "游戏区域大小:");
		pDC -> TextOut(m_iStartY+320, m_iStartX+240,m_strArea);

		pDC -> TextOut(m_iStartY+320, m_iStartX+280, "游戏级别:");
		pDC -> TextOut(m_iStartY+320, m_iStartX+300,m_strLevel);
	}
		
		
	//显示总分
	if (!m_bGameEnd)
	{
		CString lsStr;	
		lsStr.Format("总分为:%d 分",m_iPerformance);
		pDC -> SetBkColor(BLACK);
		pDC -> SetTextColor(WHITE);
		pDC -> TextOut(m_iStartY+320, m_iStartX+180,lsStr);
	}

	//画下一次将要出现的方块,用于提示用户
	if (!m_bGameEnd)
	{
		pDC -> SetBkColor(BLACK);
		pDC -> SetTextColor(WHITE);
		pDC -> TextOut(m_iStartY+320, m_iStartX,"下一个方块:");
		
		for (UINT k=0;k<4;k++)
		{
			i = NextStatus[k][0];
		    j = NextStatus[k][1];

			//划的方块比规定的大小略微小一点
			//画小方块边框黑线
			pDC -> SelectObject(blackPen);
			pDC -> MoveTo(m_iStartY+j*30 +1+320, m_iStartX+i*30 +1+30);
			pDC -> LineTo(m_iStartY+j*30 +1+320, m_iStartX+(i+1)*30 -1+30);
			pDC -> MoveTo(m_iStartY+j*30 +1+320,	m_iStartX+(i+1)*30 -1+30);
			pDC -> LineTo(m_iStartY+(j+1)*30 -1+320, m_iStartX+(i+1)*30 -1+30);

			//画小方块边框白线
			pDC -> SelectObject(whitePen);
			pDC -> MoveTo(m_iStartY+j*30 +1+320,      m_iStartX+i*30 +1+30);
			pDC -> LineTo(m_iStartY+(j+1)*30 -1+320,  m_iStartX+i*30 +1+30);
			pDC -> MoveTo(m_iStartY+(j+1)*30 -1+320,  m_iStartX+i*30 +1+30);
			pDC -> LineTo(m_iStartY+(j+1)*30 -1+320,  m_iStartX+(i+1)*30 -1+30);

			//每个方块填充蓝色的背景 
			pDC -> Rectangle(m_iStartY+j*30 +2+320,m_iStartX+i*30 +2+30,
							 m_iStartY+(j+1)*30 -2+320,m_iStartX+(i+1)*30 -2+30);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CHWRECTView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHWRECTView message handlers

void CHWRECTView::RectChange()
{
	//先预先变形,然后判断变形后的方块是否有空间,如有足够空间,则进行实际变形,否则不变
	int xx1,xx2,xx3,xx4,yy1,yy2,yy3,yy4;
	int m_lscurrentRect;
	
	CString lsStr;
	
	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];

	//变形后位置在数组中的存放顺序仍需遵循先左后右,在同一列中先上后下
	xx1=x1; xx2=x2; xx3=x3; xx4=x4; yy1=y1; yy2=y2; yy3=y3; yy4=y4;
	switch(m_currentRect)
	{
	case 1:
		xx1=x1+1; yy1=y1-1; xx3=x3-1; yy3=y3+1; xx4=x4-2; yy4=y4+2;
		m_lscurrentRect = 11;
		break;
	case 11:
		xx1=x1-1; yy1=y1+1; xx3=x3+1; yy3=y3-1; xx4=x4+2; yy4=y4-2;
		m_lscurrentRect = 1;
		break;
	case 2:
		m_lscurrentRect=2; 
		break;
	case 3:
		xx1=x1-2; yy1=y1+1; xx4=x4-1; yy4=y4;
		m_lscurrentRect = 31;
		break;
	case 31:
		xx1=x1+1; yy1=y1-1; 
		m_lscurrentRect = 32;
		break;
	case 32:
		xx1=x1+1; yy1=y1; xx4=x4+2; yy4=y4-1;
		m_lscurrentRect=33;
		break;
	case 33:
		xx4=x4-1; yy4=y4+1;
		m_lscurrentRect=3;
		break;
	case 4:
		xx1=x1+2; yy1=y1-1; xx3=x3+1; yy3=y3-1; xx4=x4-1;
		m_lscurrentRect = 41;
		break;
	case 41:
		xx1=x1-2; yy1=y1+1; xx3=x3-1; yy3=y3+1; xx4=x4+1;
		m_lscurrentRect = 4;
		break;
	case 5:
		xx1=x1-1; xx2=x2-2; yy2=y2+1; xx3=x3+1; yy4=y4+1;
		m_lscurrentRect = 51;
		break;
	case 51:
		xx1=x1+1; xx2=x2+2; yy2=y2-1; xx3=x3-1; yy4=y4-1;
		m_lscurrentRect = 5;
		break;
	case 6:
		xx2=x1+1; yy2=y2-1; xx3=x3-1; xx4=x4-2; yy4 = yy4+1;
		m_lscurrentRect = 61;
		break;
	case 61:
		xx3=x3+2; yy3=y3-1; xx4=x4+2; yy4=y4-1;
		m_lscurrentRect = 62;
		break;
	case 62:
		xx1=x1+1; yy1=y1-1; xx3=x3-2; yy3=y3+1; xx4=x4-1;
		m_lscurrentRect = 63;
		break;
	case 63:
		xx1=x1-2; yy1=y1+1; xx2=x2-2; yy2=y2+1;
		m_lscurrentRect = 6;
		break;
	case 7:
		xx3=x3-1; yy3=y3+1; xx4=x4+1; yy4=y4+1;
		m_lscurrentRect = 71;
		break;
	case 71:
		xx1=x1+2; xx2=x2-1; yy2=y2+1; xx4=x4+1; yy4=y4-1;
		m_lscurrentRect = 72;
		break;
	case 72:
		xx1=x1-2; xx3=x3-1; yy3=y3+1; xx4=x4-1; yy4=y4+1;
		m_lscurrentRect = 73;
		break;
	case 73:
		xx2=x2+1; yy2=y2-1; xx3=x3+2; yy3=y3-2; xx4=x4-1; yy4=y4-1;
		m_lscurrentRect = 7;
		break;
	}

	//如果变形后所在的区域内无其他方块,则表示有足够空间,可以变形
	//且不能超越边界
	GameStatus[x1][y1] = 0;
	GameStatus[x2][y2] = 0;
	GameStatus[x3][y3] = 0;
	GameStatus[x4][y4] = 0;
	if (GameStatus[xx1][yy1]==0 && GameStatus[xx2][yy2]==0 && GameStatus[xx3][yy3]==0 && GameStatus[xx4][yy4]==0 
		&& yy1>=0 && yy4<=m_iCol-1 
		&& !(xx1<0 || xx2<0 || xx3<0 || xx4<0) 
		&& !(xx1>m_iRow-1 || xx2>m_iRow-1 || xx3>m_iRow-1 || xx4>m_iRow-1) )
	{
		InvalidateCurrent();

		ActiveStatus[0][0]=xx1;
		ActiveStatus[1][0]=xx2;
		ActiveStatus[2][0]=xx3;
		ActiveStatus[3][0]=xx4;
		ActiveStatus[0][1]=yy1;
		ActiveStatus[1][1]=yy2;
		ActiveStatus[2][1]=yy3;
		ActiveStatus[3][1]=yy4;

		GameStatus[xx1][yy1] = 1;
		GameStatus[xx2][yy2] = 1;
		GameStatus[xx3][yy3] = 1;
		GameStatus[xx4][yy4] = 1;

		InvalidateCurrent();

		//改变形状代码
		m_currentRect = m_lscurrentRect;
	}
	else
	{
		GameStatus[x1][y1] = 1;
		GameStatus[x2][y2] = 1;
		GameStatus[x3][y3] = 1;
		GameStatus[x4][y4] = 1;
	}

	//判断是否已到底
	ActiveIsBottom();

}

BOOL CHWRECTView::RightIsLimit()
{
	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_currentRect)
	{
	case 1:
		if (GameStatus[x1][y1+1] || GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 11:
		if (GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 2:
		if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 3:
		if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 31:
		if (GameStatus[x1][y1+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 32:
		if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 33:
		if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 4:
		if (GameStatus[x1][y1+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 41:
		if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 5:
		if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 51:
		if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 6:
		if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 61:
		if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 62:
		if (GameStatus[x1][y1+1] || GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 63:
		if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 7:
		if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 71:
		if (GameStatus[x1][y1+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 72:
		if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	case 73:
		if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
			return FALSE;
		break;
	}

	return TRUE;

}

⌨️ 快捷键说明

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