📄 tetrisview.cpp
字号:
// TetrisView.cpp : implementation of the CTetrisView class
//
#include "stdafx.h"
#include "Tetris.h"
//#include ""
#include <Mmsystem.h>
#include "TetrisDoc.h"
#include "TetrisView.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("速度");
/////////////////////////////////////////////////////////////////////////////
// CTetrisView
IMPLEMENT_DYNCREATE(CTetrisView, CView)
BEGIN_MESSAGE_MAP(CTetrisView, CView)
//{{AFX_MSG_MAP(CTetrisView)
ON_WM_CREATE()
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_GAME_START, OnGameStart)
ON_COMMAND(ID_GAME_PAUSE, OnGamePause)
ON_UPDATE_COMMAND_UI(ID_GAME_PAUSE, OnUpdateGamePause)
ON_COMMAND(IDC_EXIT, OnExit)
ON_WM_MOUSEMOVE()
ON_COMMAND(IDC_SET_MUSIC, OnSetMusic)
ON_UPDATE_COMMAND_UI(IDC_SET_MUSIC, OnUpdateSetMusic)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTetrisView construction/destruction
CTetrisView::CTetrisView()
{
// TODO: add construction code here
m_bPause=false;
m_bStart=false;
// m_bStart=false;
}
CTetrisView::~CTetrisView()
{
}
BOOL CTetrisView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
if (cs.lpszClass == NULL)
cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTetrisView drawing
void CTetrisView::OnDraw(CDC* pDC)
{
CTetrisDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Game.Draw(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CTetrisView diagnostics
#ifdef _DEBUG
void CTetrisView::AssertValid() const
{
CView::AssertValid();
}
void CTetrisView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTetrisDoc* CTetrisView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTetrisDoc)));
return (CTetrisDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTetrisView message handlers
int CTetrisView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CDC* pDC=GetDC();
Game.GameInit(pDC);
Initial();
Game.SetPosition(0,0);
ReleaseDC(pDC);
return 0;
}
void CTetrisView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
int i=nChar;
if(nChar==VK_SPACE)
{
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 CTetrisView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if((m_bPause==TRUE)||(m_bStart==FALSE)) return;
int speed=Game.GetSpeed();
Game.MoveNext();
Game.Draw(GetDC());
if(Game.EndFlag==TRUE)
{
m_bStart=FALSE;
}
if(speed<Game.GetSpeed())
{
OnGamePause();
OnGamePause();
}
CView::OnTimer(nIDEvent);
}
void CTetrisView::OnGameStart()
{
// TODO: Add your command handler code here
CWinApp* pApp = AfxGetApp();
m_bPause=false; //暂停标志为假
m_bStart=true; //开始标志为真
Game.SetPosition(0,0);
SetTimer(9999,50*(11-Game.GetSpeed()),NULL);
Game.Start();
Game.Draw(GetDC());
}
void CTetrisView::OnGamePause()
{
// TODO: Add your command handler code here
m_bPause=!m_bPause;
if(m_bPause)
{
KillTimer(9999);
}
else
{
SetTimer(9999,50*(12-Game.GetSpeed()),NULL);
}
}
void CTetrisView::OnUpdateGamePause(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bPause);
}
void CTetrisView::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 );
Game.SetSpeed(pApp->GetProfileInt(szSection, Speed, 1));
}
void CTetrisView::OnExit()
{
// TODO: Add your command handler code here
exit(1);
}
void CTetrisView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
void CTetrisView::OnSetMusic()
{
// TODO: Add your command handler code here
if(game_music)
{
game_music =false;
HWND hWnd;
hWnd = GetSafeHwnd();
char inBuf[300],outBuf[60],fileName[255];
MCIERROR mciError;
strcpy(fileName,"love.mid");
wsprintf( inBuf,"open %s type sequencer alias myseq",fileName);
mciError = mciSendString( inBuf, outBuf, sizeof(outBuf), NULL);
if (mciError == 0)
{
mciError = mciSendString("play myseq notify",NULL,0, hWnd);
if (mciError != 0)
mciSendString("close myseq",NULL,0,NULL);
}
}
else
{
game_music = true;
mciSendString("close myseq",NULL,0,NULL);
}
}
void CTetrisView::OnUpdateSetMusic(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(!game_music);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -