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

📄 app_frame.cpp

📁 俄罗斯方块 采用MFC和GDI+编写的,自己感觉写得不是很好,不过看上去还行
💻 CPP
字号:
#include "stdafx.h"
#include "app_frame.h"
#include "About.h"

DOCINFO * info;

Game theGame;

BEGIN_MESSAGE_MAP(CTrixApp,CWinApp)

END_MESSAGE_MAP()

CTrixFrame::CTrixFrame() : CFrameWnd()
{
}

BOOL CTrixApp::InitInstance()
{
//#ifdef _AFXDLL
//	Enable3dControls();
//#else
//	Enable3dControlsStatic();
//#endif
	LoadStdProfileSettings(4);
	CSingleDocTemplate * pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAIN,
		RUNTIME_CLASS(CTrixDoc),
		RUNTIME_CLASS(CTrixFrame),
		RUNTIME_CLASS(CTrixView));
	AddDocTemplate(pDocTemplate);
	CCommandLineInfo cmdInfo;

	//Dispath commands specified on the the commands line
	if(!ProcessShellCommand(cmdInfo))
		return FALSE;
	this->m_pMainWnd->ShowWindow(SW_SHOW);
	this->m_pMainWnd->UpdateWindow();
	return TRUE;
};

//Frame map
IMPLEMENT_DYNCREATE(CTrixFrame,CFrameWnd)
BEGIN_MESSAGE_MAP(CTrixFrame,CFrameWnd)
	ON_WM_CREATE()
	ON_WM_CLOSE()
//	ON_WM_KEYDOWN()
	ON_WM_PAINT()
	ON_COMMAND(ID_FILE_EXIT, &CTrixFrame::OnExit)
	ON_COMMAND(ID_GAME_RESTRAT, &CTrixFrame::OnGameRestrat)
	ON_COMMAND(ID_HELP_ABOUT, &CTrixFrame::OnHelpAbout)
END_MESSAGE_MAP()

//Document map
IMPLEMENT_DYNCREATE(CTrixDoc,CDocument)
BEGIN_MESSAGE_MAP(CTrixDoc,CDocument)

END_MESSAGE_MAP()

//view map
IMPLEMENT_DYNCREATE(CTrixView,CView);
BEGIN_MESSAGE_MAP(CTrixView,CView)

	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
END_MESSAGE_MAP()

BOOL CTrixFrame::PreCreateWindow(CREATESTRUCT &cs)
{
	return CFrameWnd::PreCreateWindow(cs);
}
void CTrixDoc::Serialize(CArchive &ar)
{

}
void CTrixView::OnDraw(CDC *pDC)
{
	/*CTrixDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(!pDoc)
		return;*/
	/*CString score;
	score.Format(L"score:%d",theGame.score);
	pDC->TextOutW(WIDTH + 50,HEIGHT - 100,score,score.GetLength());*/
}
int CTrixFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if( CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	//-----------------------GdiPlus init
	GdiplusStartup(&token,&in,NULL);
	ZeroMemory(&docInfo,sizeof(docInfo));
	docInfo.cbSize = sizeof(docInfo);
	docInfo.lpszDocName = L"GdiplusPrint";
	info = & docInfo;
	/*MessageBox(L"in here",L"debug",0);*/
	//-------------------------------------Game
	theGame.Init(& this->GetDC()->m_hDC,info);
	theGame.Start();
	theGame.EnterGame = true;
	//-----------------------------------------
	return 0;
}

CTrixApp theApp;

void CTrixFrame::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

   GdiplusShutdown(token);
	CFrameWnd::OnClose();
}

void CTrixView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CView::OnPaint()
	/*CString temp;
	temp.Format(L"%g",);
	dc.TextOutW(100,100,temp,temp.GetLength())*/
	// double
	CPen Pen;
	Pen.CreatePen(PS_INSIDEFRAME,1,RGB(225,225,0));
	CBrush Brush;
	Brush.CreateSolidBrush(RGB(225,225,0));
	dc.SelectObject(&Pen);
	dc.SelectObject(&Brush);
	CDC MemDC;
	CBitmap MemBitmap;
	MemDC.CreateCompatibleDC(&dc);
	CRect rc;
	GetClientRect(&rc);
	MemBitmap.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
	CBitmap *pOldBit = MemDC.SelectObject(&MemBitmap);
	MemDC.SelectObject(&Pen);
	MemDC.SelectObject(&Brush);
	HDC hdc = MemDC;
	////draw
	//dc.SetViewportOrg(10,400);
	theGame.SetHDC(hdc);
	MemDC.LineTo((GameField::Width)*GameField::SquareSize,0);
	MemDC.LineTo((GameField::Width)*GameField::SquareSize,(GameField::Height)*GameField::SquareSize);
	MemDC.LineTo(0,(GameField::Height)*GameField::SquareSize);
	MemDC.LineTo(0,0);
	theGame.CurrentBlock->Show();
	theGame.nextBlock->Show();
	theGame.ReDraw();
	CString score;
	score.Format(L"score:%d\nLevel:%d",theGame.score,theGame.GetLevel());
	MemDC.DrawTextExW(score,CRect(GameField::Width*GameField::SquareSize+10,GameField::Height/2*GameField::SquareSize,
		GameField::Width*GameField::SquareSize+90,GameField::Height/2*GameField::SquareSize+30),SW_SHOW,NULL);
	//-----------------------------------
	dc.BitBlt(0,0,rc.Width(),rc.Height(),&MemDC,0,0,SRCCOPY);
	MemBitmap.DeleteObject();
	MemDC.DeleteDC();
}

//void CTrixFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
//{
//	// TODO: 在此添加消息处理程序代码和/或调用默认值
//	
//	
//	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
//}
CTrixFrame::~CTrixFrame()
{
}
//void CTrixFrame::OnPaint()
//{
//	CPaintDC dc(this); // device context for painting
//	// TODO: 在此处添加消息处理程序代码
//	// 不为绘图消息调用 CFrameWnd::OnPaint()
//	Square temp(Size(100,100),Color(255,0,0),Color(0,255,255));
//	HDC hdc = dc;
//	GameField engine(hdc,*info);
//	Block test(Point(100,100),BlockTypes::Undefined,engine);
//	temp.Show(hdc,*info);
//	test.Show();
//}

void CTrixView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CRect rc;
	GetClientRect(&rc);
	switch(nChar)
	{
	case VK_UP:
		theGame.CurrentBlock->Rotate();
		//RedrawWindow(CRect(0,0,600,600));
		InvalidateRect(rc,0); //is OK
		//SendMessage(WM_NCPAINT);
		//SendMessage(WM_ERASEBKGND);
		//MessageBox(L"paint?");
		break;
	case VK_DOWN:
		if(theGame.CurrentBlock->Down())
		//RedrawWindow(CRect(0,0,600,600));
		InvalidateRect(rc,0);
		break;
	case VK_RIGHT:
		if(theGame.CurrentBlock->Right())
		InvalidateRect(rc,0);
		//InvalidateRect(CRect(0,0,450,450));
		break;
	case VK_LEFT:
		if(theGame.CurrentBlock->Left())
		InvalidateRect(rc,0);
		//InvalidateRect(CRect(0,0,450,450));
		break;
	case VK_RETURN:
		{
			if(theGame.GetProcessing())
			{
				 ID_TIMER = SetTimer(1,theGame.GetTime(),0);
				 theGame.Switch();
			}
			else
			{
				KillTimer(ID_TIMER);
				theGame.Switch();
				if(!theGame.EnterGame)
				{
					MessageBox(L"再按一次回车键开始游戏!",L"开始游戏",MB_OK);
					theGame.EnterGame = ! theGame.EnterGame;
				}
			}
		}

	}

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void Check()
{
	/*int sum =0;
	for(int i =0;i<GameField::Width;i++)
	{
		for(int j=0;j<GameField::Height;j++)
		{
			if(GameField::arrGameField[i][j].IsNull())
				sum++;
		}
	}
	CString temp;
	temp.Format(L"has %d isn't NULL!",sum);
	MessageBox(NULL,temp.GetString(),L"test",MB_OK);*/
	theGame.NextOne();
	
}
void CTrixFrame::OnExit()
{
	// TODO: 在此添加命令处理程序代码
	exit(0);
}

void CTrixView::OnTimer(UINT_PTR nIDEvent)
{	
	
	if(!theGame.CurrentBlock->Down())
	{
		if(theGame.CurrentBlock->Top() == 0)//test game over
		{
			//Game Over!
			this->KillTimer(ID_TIMER);
			MessageBox(L"Game over!");
			return;
		}
		theGame.NextOne();//create new  a block
	}
	//Level Up process
	{
		theGame.score += (theGame.engine->CheckLines()*100);
		if(theGame.Go())
		{
			this->KillTimer(ID_TIMER);
			MessageBox(L"you win,you are real hero!");
			return ;
		}
		else
		{
			this->SetTimer(1,theGame.GetTime(),0);
		}
		
	}
	// Redraw window
	//RedrawWindow(CRect(0,0,600,600));
	CRect rc;
	GetClientRect(&rc);
	InvalidateRect(rc,0);
	//SendMessage(WM_PAINT);
	CView::OnTimer(nIDEvent);
}

void CTrixFrame::OnGameRestrat()
{
	theGame.ReStrat();
	/*if(theGame.GetProcessing())
		theGame.ReStrat();
	else
		MessageBox(L"请先按回车键暂停再重新开始游戏!",L"ReStrat",MB_OK);*/
	// TODO: 在此添加命令处理程序代码
}

void CTrixFrame::OnHelpAbout()
{
	// TODO: 在此添加命令处理程序代码
	CAbout Dlg;
	Dlg.DoModal();
}

⌨️ 快捷键说明

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