📄 handtetrisview.cpp
字号:
// HandTetrisView.cpp : implementation of the CHandTetrisView class
//
#include "stdafx.h"
#include "HandTetris.h"
#include "HandTetrisDoc.h"
#include "HandTetrisView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static const TCHAR szSection[] = _T("Settings");
static const TCHAR clrCube[] = _T("方块颜色");
static const TCHAR clrDeclareCube[] = _T("声明区域的方块颜色");
static const TCHAR clrMaxRect[] = _T("最大区域颜色");
static const TCHAR clrDeclareRect[] = _T("声明区域颜色");
static const TCHAR clrDiamondsRect[] = _T("运行区域颜色");
static const TCHAR clrLine[] = _T("方格线颜色");
static const TCHAR clrDeclareFont[] = _T("声明区域的字体颜色");
static const TCHAR clrGameOverFont[] = _T("游戏结束字体的颜色");
static const TCHAR bRandomCubeColor[] = _T("随机产生运动方块颜色");
static const TCHAR Speed[] = _T("速度");
static const TCHAR Difficult[] = _T("难度");
static const TCHAR Music[] = _T("音乐开关");
/////////////////////////////////////////////////////////////////////////////
// CHandTetrisView
IMPLEMENT_DYNCREATE(CHandTetrisView, CView)
BEGIN_MESSAGE_MAP(CHandTetrisView, CView)
//{{AFX_MSG_MAP(CHandTetrisView)
ON_WM_CREATE()
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_GAME_START, OnGameStart)
ON_COMMAND(ID_GAME_PAUSE, OnGamePause)
ON_COMMAND(ID_GAME_SETUP, OnGameSetup)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHandTetrisView construction/destruction
CHandTetrisView::CHandTetrisView()
{
// TODO: add construction code here
m_bPause=false;
m_bStart=false;
m_bMusic=TRUE;
}
CHandTetrisView::~CHandTetrisView()
{
}
BOOL CHandTetrisView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHandTetrisView drawing
void CHandTetrisView::OnDraw(CDC* pDC)
{
CHandTetrisDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Game.Draw(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CHandTetrisView diagnostics
#ifdef _DEBUG
void CHandTetrisView::AssertValid() const
{
CView::AssertValid();
}
void CHandTetrisView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHandTetrisDoc* CHandTetrisView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHandTetrisDoc)));
return (CHandTetrisDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHandTetrisView message handlers
int CHandTetrisView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
CDC* pDC=GetDC();
Game.GameInit(pDC);
Initial();
Game.SetPosition(20,0);
ReleaseDC(pDC);
return 0;
}
void CHandTetrisView::Initial()
{
CWinApp* pApp = AfxGetApp();
Game.m_clrCube=pApp->GetProfileInt(szSection, clrCube, Game.m_clrCube);
Game.m_clrDeclareCube=pApp->GetProfileInt(szSection, clrDeclareCube, Game.m_clrDeclareCube);
Game.m_clrMaxRect =pApp->GetProfileInt(szSection,clrMaxRect ,Game.m_clrMaxRect );
Game.m_clrDeclareRect=pApp->GetProfileInt(szSection,clrDeclareRect , Game.m_clrDeclareRect);
Game.m_clrDiamondsRect=pApp->GetProfileInt(szSection,clrDiamondsRect , Game.m_clrDiamondsRect);
Game.m_clrLine=pApp->GetProfileInt(szSection, clrLine, Game.m_clrLine);
Game.m_clrDeclareFont =pApp->GetProfileInt(szSection, clrDeclareFont,Game.m_clrDeclareFont );
Game.m_clrGameOverFont=pApp->GetProfileInt(szSection,clrGameOverFont , Game.m_clrGameOverFont);
Game.m_bRandomCubeColor=pApp->GetProfileInt(szSection, bRandomCubeColor,Game.m_bRandomCubeColor );
m_bMusic=pApp->GetProfileInt(szSection, Music, FALSE);
Game.SetSpeed(pApp->GetProfileInt(szSection, Speed, 5));
Game.SetLevel(pApp->GetProfileInt(szSection,Difficult ,2));
}
void CHandTetrisView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int i=nChar;
if(nChar==VK_RETURN)
{
OnGamePause();
return;
}
if(m_bPause==TRUE) return;
switch(nChar)
{
case VK_LEFT:
Game.MoveLeft();
break;
case VK_RIGHT:
Game.MoveRight();
break;
case VK_UP:
Game.MoveRotate();
break;
case VK_DOWN:
Game.MoveNext();
break;
}
CDC* pDC=GetDC();
Game.Draw(pDC);
ReleaseDC(pDC);
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CHandTetrisView::OnTimer(UINT nIDEvent)
{
if((m_bPause==TRUE)||(m_bStart==FALSE)) return;
int speed=Game.GetSpeed();
switch(1)
{
case 1:
Game.MoveNext();
Game.Draw(GetDC());
if(Game.EndFlag==TRUE)
{
m_bStart=FALSE;
}
break;
case 2:
Game.MoveNext();
Game.Draw(GetDC());
break;
case 3:
break;
}
if(speed<Game.GetSpeed())
{
//OnMenuPause();
}
CView::OnTimer(nIDEvent);
}
void CHandTetrisView::OnGameStart()
{
CWinApp* pApp = AfxGetApp();
int dx=10;//pApp->GetProfileInt("Settings","方块大小",20);
int m1=111+dx*10;
int m2=85+dx*24;
m_bPause=false; //暂停标志为假
m_bStart=true; //开始标志为真
Game.SetPosition(20,0);
SetTimer(9999,50*(11-Game.GetSpeed()),NULL);
Game.Start();
Game.Draw(GetDC());
//播放声音
Game.PlayMusic(m_bMusic);
}
void CHandTetrisView::OnGamePause()
{
m_bPause=!m_bPause;
if(m_bPause)
{
KillTimer(9999);
sndPlaySound(NULL,SND_ASYNC);
Game.PlayMusic(FALSE);
}
else
{
SetTimer(9999,50*(12-Game.GetSpeed()),NULL);
Game.PlayMusic(m_bMusic);
}
}
void CHandTetrisView::OnGameSetup()
{
m_bPause=TRUE;
Game.PlayMusic(FALSE);
CSetupDlg dlg;
dlg.m_nDifficulty=Game.GetLevel();
dlg.m_nSpeed=Game.GetSpeed();
dlg.m_bMusic=m_bMusic;
if(dlg.DoModal()==IDOK)
{
Game.SetLevel(dlg.m_nDifficulty);
Game.SetSpeed(dlg.m_nSpeed);
m_bMusic=dlg.m_bMusic;
}
Invalidate(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -