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

📄 aaview.cpp

📁 用MFC做的简单的俄罗斯方块。功能都已经实现。但是没有考虑算法。希望大家指教。
💻 CPP
字号:
// aaView.cpp : implementation of the CAaView class
//

#include "stdafx.h"
#include "aa.h"
#include "aaDoc.h"
#include "aaView.h"

#include "Left_L.h"
#include "Right_L.h"
#include "Z_z1.h"
#include "D_z1.h"
#include "Squre.h"
#include "D_T.h"
#include "Z_T.h"
#include "time.h"
#include "DrawAssist.h"
#include "bar.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAaView

IMPLEMENT_DYNCREATE(CAaView, CView)

BEGIN_MESSAGE_MAP(CAaView, CView)
//{{AFX_MSG_MAP(CAaView)
ON_WM_KEYDOWN()
ON_WM_TIMER()
//}}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()

/////////////////////////////////////////////////////////////////////////////
// CAaView construction/destruction

CAaView::CAaView()
{
	a=true;
	Status=0;
	// TODO: add construction code here
	CreateNewBlock();
}

CAaView::~CAaView()
{
}

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

/////////////////////////////////////////////////////////////////////////////
// CAaView drawing

void CAaView::OnDraw(CDC* pDC)
{	
	CClientDC dc(this); 
	CPen newPen(PS_DASH,2,RGB(20,30,40)); //创建新画笔 
	CPen* oldPen=dc.SelectObject(&newPen); //选入设备环境 
	dc.MoveTo(150,0); 
	dc.LineTo(150,300); 
	dc.MoveTo(0,0);
	dc.LineTo(0,300);
	dc.MoveTo(0,300);
	dc.LineTo(150,300);
	CAaDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	CDC* pDisplayMemDC=new CDC;
	pDisplayMemDC->CreateCompatibleDC(pDC);
	
	assist.SetDC(pDC,pDisplayMemDC);
	
	assist.DrawChessBox();
	assist.DrawBlock(*block);
	//以下循环用来消除一整行方格
		for(int j=0;j<=19;j++)
	{
		int k=0;
		for(int i=0;i<10;i++)
		{
			if(assist.blocks[i][j])
				k++;
		}
		if(k==10)
		{
			for(int i=0;i<10;i++)
				assist.blocks[i][j]=false;
			for(int f=j-1;f>0;f--)
				for(int g=0;g<10;g++)
					if(assist.blocks[g][f])
					{
						assist.blocks[g][f+1]=true;
						assist.blocks[g][f]=false;
					}
			for(int r=j;r>0;r--)
				for(int t=0;t<10;t++)
					if(assist.blocks[t][r])
						assist.DrawButton(t,r);
		}
	}
	
	ReleaseDC(pDisplayMemDC);//记得执行以下的语句
	delete pDisplayMemDC;
	
}

/////////////////////////////////////////////////////////////////////////////
// CAaView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CAaView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CAaView message handlers

void CAaView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	//设置上下左右以及暂停键
	switch(nChar)
	{
	case'A':  
		
		{
			if(assist.CanMoveLeft(*block))
				block->MoveLeft();
		}break;
	case'D':
		
		{
			if(assist.CanMoveRight(*block))
				block->MoveRight();
		}break;  
	case'W':
		{
			if(assist.CanMoveDown(*block))
			{
				Status+=1;
				block->Changestatus(Status);
				block->ChangeBlock();
			}
		}break;
		
	case'S':
		{
			if(assist.CanMoveDown(*block))
				block->MoveDown();
			
		}break;
	case'P':
		{
			static int n=0;
			c=n%2;
			n++;
			if(c==0)
				AfxMessageBox("暂停!");
		}
		
	}
	Invalidate();
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
	
	
	
}

void CAaView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	if(c && CAaView::Zztj())//下落及变形条件
	{
	if(assist.CanMoveDown(*block))
		block->MoveDown();
	else
	{
		assist.AddBlock(*block);
		CreateNewBlock();
	}
	Invalidate();
	UpdateWindow();
	CView::OnTimer(nIDEvent);
	}

}

void CAaView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	SetTimer(0,500,NULL);
	
	// TODO: Add your specialized code here and/or call the base class
	
}
//产生随机图形
void CAaView::CreateNewBlock()
{
	
	srand((unsigned)time(NULL));
	switch(rand()%8)
	{
	case 0:
		{
			block = new Bar;
		}break;
	case 1:
		{
			block = new Left_L;
		}break;
	case 2:
		{
			block =new  Right_L;
		}break;
	case 3:
		{
			block = new Z_z;
		}break;
	case 4:
		{
			block = new D_z;
		}break;
	case 5:
		{
			block =new  Squre;
		}break;
	case 6:
		{
			block =new  D_T;
		}break;
	case 7:
		{
			block = new	Z_T;
		}break;
	}
}
//游戏终止条件
bool CAaView::Zztj()
{
	for(int i=0,k=0;i<=9;i++)

			if(assist.blocks[i][0])
				k++;
	
		if(k!=0)
		{
			a=false;
				AfxMessageBox("Game Over!");
			return a;
		}
	
	return a;
}

⌨️ 快捷键说明

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