📄 俄罗斯方块view.cpp
字号:
// 俄罗斯方块View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "俄罗斯方块.h"
#include "俄罗斯方块Doc.h"
#include "俄罗斯方块View.h"
#include "Square.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
#ifndef HONG
#define HONG
#define W_NUMBER 12 //一横排方块的个数
#define H_NUMBER 20 //一竖排方块的个数
#define SIZE 20 //方块的大小
#define BkColor RGB(200,100,200)
#endif
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_COMMAND(ID_BEGIN, OnBegin)
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_COMMAND(ID_PAUSE, OnPause)
ON_UPDATE_COMMAND_UI(ID_PAUSE, OnUpdatePause)
ON_COMMAND(ID_END, OnEnd)
ON_COMMAND(ID_ABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
MySquare.RgRect = CRect(5,5,10+SIZE*(W_NUMBER+1),10+SIZE*H_NUMBER);
for(int i = 0; i <= W_NUMBER; i++)
{
for(int j = 0; j <= H_NUMBER; j++)
{
MySquare.Matrix[i][j] = 0;
}
}
Time_Number = 0;
Down_Able = false;
Runing = true;
Elapse = 500;
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Begin();
MySquare.PaintMatrix(pDC);
MySquare.ShowScore(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnBegin()
{
// TODO: Add your command handler code here
Runing = true;
SetTimer(1,Elapse,NULL);
for(int i = 0; i <= W_NUMBER; i++)
{
for(int j = 0; j <= H_NUMBER; j++)
{
MySquare.Matrix[i][j] = 0;
}
}
MySquare.m_Score = 0;
pDC = GetDC();
MySquare.ShowScore(pDC);
ReleaseDC(pDC);
MySquare.Grade = 1;
MySquare.UpGrade(&Elapse);
CMyView::Begin();
MySquare.Make();
}
void CMyView::Begin()
{
pDC = GetDC();
CRect rect(0,0,28+SIZE*W_NUMBER,10+SIZE*H_NUMBER);
CPen *Pen;
CPen *OldPen;
Pen = new CPen;
Pen->CreatePen(PS_SOLID,5,RGB(100,200,100));
CBrush *MyBrush;
MyBrush = new CBrush;
MyBrush->CreateSolidBrush(RGB(200,100,200));
CBrush *pOldBrush;
pOldBrush = pDC->SelectObject(MyBrush);
OldPen = pDC->SelectObject(Pen);
pDC->Rectangle(rect);
pDC->SelectObject(OldPen);
pDC->SelectObject(pOldBrush);
delete Pen;
delete MyBrush;
ReleaseDC(pDC);
}
void CMyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// if(1== 1)
// {
// MySquare.PaintMatrix(GetDC());
// }
switch(nIDEvent)
{
case 2://break;
case 1:
{
static bool number = true;
if(MySquare.CheckRange(0,SIZE) == true&&
MySquare.CheckMatrix(0,SIZE) == true)//判断往下移动
{
if(number)//如果是刚刚产生的不需擦除上一时刻的方块
{
pDC = GetDC();
MySquare.Move(0,SIZE,pDC,1);
ReleaseDC(pDC);
}
else
{
pDC = GetDC();
MySquare.Move(0,SIZE,pDC);
ReleaseDC(pDC);
}
}//不能往下移动
else
{
if(number == false)//不是第一次
{
KillTimer(1);
KillTimer(2);
Time_Number = 0;
number = true;
pDC = GetDC();
MySquare.ChangeMatrix();
MySquare.Remove(GetDC());
//MySquare.PaintMatrix(pDC,1);
MySquare.PaintMatrix(pDC,0);
MySquare.Make();
SetTimer(1,Elapse,NULL);
MySquare.UpGrade(&Elapse);
MySquare.ShowScore(pDC);
ReleaseDC(pDC);
break;
}
else//第一次就不能往下移
{
KillTimer(1);
KillTimer(2);
Time_Number = 0;
if(AfxMessageBox("再来一局?",MB_OK,0) == IDOK)
{
SetTimer(1,Elapse,NULL);
for(int i = 0; i <= W_NUMBER; i++)
{
for(int j = 0; j <= H_NUMBER; j++)
{
MySquare.Matrix[i][j] = 0;
}
}
MySquare.m_Score = 0;
MySquare.Grade = 1;
MySquare.UpGrade(&Elapse);
pDC = GetDC();
MySquare.ShowScore(pDC);
ReleaseDC(pDC);
MySquare.Make();
Invalidate();
}
}
}
number = false;
Down_Able = 0;
Time_Number ++;
break;
}
}
CView::OnTimer(nIDEvent);
}
void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch (nChar)
{
case VK_LEFT:
{
if(MySquare.CheckRange(-SIZE,0))
{
if(MySquare.CheckMatrix(-SIZE,0))
{
MySquare.Move(-SIZE,0,GetDC());
}
}
break;
}
case VK_RIGHT:
{
if(MySquare.CheckRange(SIZE,0))
{
if(MySquare.CheckMatrix(SIZE,0))
MySquare.Move(SIZE,0,GetDC());
}
break;
}
case VK_DOWN:
{
/* if(Down_Able < 2)
{
bool move = true;
for(int i = 5;i > 0 && move ;i--)
{
if(MySquare.CheckRange(0,i*SIZE))
{
if(MySquare.CheckMatrix(0,i*SIZE))
{
move = false;
MySquare.Move(0,i*SIZE,GetDC());
}
}
}
Down_Able ++;
}*/
KillTimer(1);
SetTimer(2,100,NULL);
break;
}
case VK_UP:
{
if(MySquare.type == 1||MySquare.type == 3)
{
if(Time_Number > 1)
{
MySquare.Turn(GetDC());
break;
}
}
else if(MySquare.type == 2)
{
if(Time_Number > 2)
{
MySquare.Turn(GetDC());
break;
}
}
else
{
MySquare.Turn(GetDC());
break;
}
}
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMyView::OnPause()
{
// TODO: Add your command handler code here
if(Runing == true)
{
KillTimer(1);
KillTimer(2);
MySquare.PaintMatrix(GetDC());
}
else
{
SetTimer(1,Elapse,NULL);
SetTimer(2,Elapse/5,NULL);
MySquare.PaintMatrix(GetDC());
}
Runing = !Runing;
}
void CMyView::OnUpdatePause(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(Runing == true)
{
pCmdUI->SetCheck(0);
}
else
{
pCmdUI->SetCheck(1);
}
}
void CMyView::OnEnd()
{
// TODO: Add your command handler code here
AfxGetMainWnd()->SendMessage(WM_CLOSE,0,0);
}
void CMyView::OnAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -