📄 elsgamedlg.cpp
字号:
// ElsGameDlg.cpp : implementation file
//*********************************************//
//吴志刚(2003-05) for Microsoft Visual C++6.0
//俄罗斯方块程序
//*********************************************//
#include "stdafx.h"
#include "ElsGame.h"
#include "ElsGameDlg.h"
#include "DlgHelp.h"
#include "DlgSetting.h"
#include "DlgSetFace.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CElsGameDlg dialog
CElsGameDlg::CElsGameDlg(CWnd* pParent /*=NULL*/)
: CDialog(CElsGameDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CElsGameDlg)
m_sumLev = _T("");
m_score = _T("");
m_hiscore = _T("");
m_slevel = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pDlgSetFace=NULL;
}
void CElsGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CElsGameDlg)
DDX_Control(pDX, IDC_BTNDOWN, m_BtnDown);
DDX_Control(pDX, IDC_BTNLEFT, m_BtnLeft);
DDX_Control(pDX, IDC_BTNRIGHT, m_BtnRight);
DDX_Control(pDX, IDC_BTNROTATE, m_BtnRotate);
DDX_Text(pDX, IDC_SUMLEV, m_sumLev);
DDX_Text(pDX, IDC_SCORE, m_score);
DDX_Text(pDX, IDC_HISCORE, m_hiscore);
DDX_Text(pDX, IDC_SPEEDLEVEL, m_slevel);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CElsGameDlg, CDialog)
//{{AFX_MSG_MAP(CElsGameDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_EXIT, OnExit)
ON_COMMAND(IDM_START, OnStart)
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_COMMAND(IDM_ABOUT, OnAbout)
ON_COMMAND(IDM_CONTENTS, OnContents)
ON_COMMAND(IDM_PAUSE, OnPause)
ON_COMMAND(IDM_LEVEL, OnLevel)
ON_COMMAND(IDM_STOP, OnStop)
ON_COMMAND(IDM_SETFACE, OnSetface)
ON_BN_CLICKED(IDC_BTNROTATE, OnBtnrotate)
ON_BN_CLICKED(IDC_BTNLEFT, OnBtnleft)
ON_BN_CLICKED(IDC_BTNDOWN, OnBtndown)
ON_BN_CLICKED(IDC_BTNRIGHT, OnBtnright)
ON_WM_SHOWWINDOW()
ON_WM_CLOSE()
ON_WM_MOUSEMOVE()
ON_COMMAND(IDM_SAVEBMP, OnSavebmp)
ON_COMMAND(IDM_BACKGROUND, OnBackground)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CElsGameDlg message handlers
BOOL CElsGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
//================================================================//
//m_bSupportAlpha=FALSE;
//m_pSetLayerWindow=NULL;
//OSVERSIONINFO os;
//os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
//::GetVersionEx(&os);
//if(os.dwPlatformId == VER_PLATFORM_WIN32_NT)//WindowsNT!
//{
//HMODULE hUser32=GetModuleHandle(_T("USER32.DLL"));
//m_pSetLayerWindow=(lpfn)GetProcAddress(hUser32,_T("SetLayeredWindowAttributes"));
//if(m_pSetLayerWindow)
//{
//::SetWindowLong(*this,GWL_EXSTYLE,::GetWindowLong(*this,GWL_EXSTYLE)|WS_EX_LAYERED);
//m_nCurAlphaDegree=255;
//m_pSetLayerWindow(*this,0,m_nCurAlphaDegree,LWA_ALPHA);
//m_bSupportAlpha=TRUE;
//}
//}
//================================================================//
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
m_hIcon=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDR_MAINFRM));
SetIcon(m_hIcon, TRUE); // Set big icon
m_hIcon=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_MAINFRMSM));
SetIcon(m_hIcon, FALSE); // Set small icon
////////////////////////init status bar//////////////////////////////
m_wndStatusBar.Create(WS_CHILD|WS_VISIBLE|CCS_BOTTOM,CRect(0,0,0,0),this,102);
int iPartDim[3]={100,370,-1};
m_wndStatusBar.SetParts(2,iPartDim);
m_wndStatusBar.SetText("停止游戏",0,0);
m_wndStatusBar.SetText("方向键控制移动,空格键或上箭头控制旋转",1,0);
////////////////////////////////////////////////////////////////////////
//m_ToolBar
m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,
WS_CHILD|WS_VISIBLE|CBRS_GRIPPER|CBRS_ALIGN_TOP|CBRS_TOOLTIPS,CRect(4,4,0,0));
m_wndToolBar.LoadToolBar(IDR_TOOLBAR1);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
////////////////////////按钮初始化////////////////////////
m_BtnDown.SetDirection(CTriangleButton::POINT_DOWN);
m_BtnRotate.SetDirection(CTriangleButton::POINT_UP);
m_BtnLeft.SetDirection(CTriangleButton::POINT_LEFT);
m_BtnRight.SetDirection(CTriangleButton::POINT_RIGHT);
//////////////////Clear Array Load bitmap/////////////////
CWinApp * pApp=AfxGetApp();
m_nBmpType=pApp->GetProfileInt("GAMEFACE","BLOCKTYPE",0);
m_bSameGraph=pApp->GetProfileInt("GAMEFACE","SAMEGRAPH",1);
m_bBackGround=pApp->GetProfileInt("GAMEFACE","BACKGROUND",0);
//////////////////////////////////////////////////////////
if(m_bBackGround)
{
m_strMapPath=pApp->GetProfileString("GAMEFACE","FILEPATH",NULL);
HBITMAP bitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),m_strMapPath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if(bitmap)
{
CMenu* hMenu=GetMenu();
hMenu=hMenu->GetSubMenu(1);
CBitmap hBitmap;
hBitmap.Attach(bitmap);
m_brBack.CreatePatternBrush(&hBitmap);
hMenu->CheckMenuItem(IDM_BACKGROUND,MF_CHECKED);
}
else
m_bBackGround=FALSE;
}
///////////////////////////////////////////////////////////
ReLoadBitmap();
m_nCurBmpIndex=0;
InitTheArray();
m_bPlaying=FALSE;
srand(GetTickCount());
DemoStatic(rand()%7);
/////////////////////////////////////////
m_nSpeedTime=1000;
m_nSpeedLevel=0;
m_nUserSpeed=0;
/////////////////////////////////////////
m_nInitX=20;
m_nInitY=40;
m_rcGame.SetRect(m_nInitX,m_nInitY,m_nInitX+10*20,m_nInitY+19*20);
m_rcNext.SetRect(m_rcGame.right+20,m_rcGame.top,m_rcGame.right+20+80,m_rcGame.top+80);
return TRUE; // return TRUE unless you set the focus to a control
}
void CElsGameDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CElsGameDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
DrawGameFace();
DrawTheMap();
if(m_bPlaying) DrawNextBlock(FALSE);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CElsGameDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//退出程序
void CElsGameDlg::OnExit()
{
CDialog::EndDialog(0);
}
//开始游戏
void CElsGameDlg::OnStart()
{
ClearArray();
Invalidate(FALSE);
//当前块信息
m_nCurBlock=rand()%7+1;
m_nCurDir=rand()%4+1;
if((m_nCurBlock==7)&&
((m_nCurDir==1)||(m_nCurDir==2)||(m_nCurDir==4)))
{
m_nCurIndexY=1;
m_nCurIndexX=4;
}
else
{
m_nCurIndexY=0;
m_nCurIndexX=4;
}
m_nCurBmpIndex=rand()%7;
//下一块信息
m_nNextBlock=rand()%7+1;
m_nNextDir=rand()%4+1;
m_nNextBmpIndex=rand()%7;
////////////////////////
m_bPlaying=TRUE;
m_bPause=FALSE;
m_lscore =0;
m_lsumLev=0;
m_sumLev=_T("0");
m_score=_T("0");
m_slevel.Format("%d",m_nUserSpeed);
///////////////////////
DrawCurBlock(FALSE);
DrawNextBlock(FALSE);
//////////////////////////////////////////////
MySetTimer();
///////////////////////////////////////////////
UpdateData(FALSE);
ReadHiScore();
m_wndStatusBar.SetText("正在游戏",0,0);
}
//timer
void CElsGameDlg::OnTimer(UINT nIDEvent)
{
if(nIDEvent==99)
ShowSpecialAward(FALSE);
else
{
if(CheckCanMove(1))
{
DrawCurBlock(TRUE);
m_nCurIndexY++;
DrawCurBlock(FALSE);
}
else
{
if(CheckOver())
{
DoGameOver();
}
else
{
SetArrayBlockData();
if(CheckFullLine())
InvalidateRect(&m_rcGame,FALSE);
DrawNextBlock(TRUE);
CreateNewBlock();
DrawNextBlock(FALSE);
}
}
}
CDialog::OnTimer(nIDEvent);
}
//当前块
void CElsGameDlg::DrawCurBlock(BOOL bErase)
{
DrawSingleBlock(m_nCurIndexX,m_nCurIndexY,bErase,m_nCurBmpIndex);
for(int i=0;i<3;i++)
DrawSingleBlock(m_nCurIndexX+m_nBlockInfoArray[m_nCurBlock-1][m_nCurDir-1][i].x,
m_nCurIndexY+m_nBlockInfoArray[m_nCurBlock-1][m_nCurDir-1][i].y,
bErase,m_nCurBmpIndex);
}
void CElsGameDlg::DrawSingleBlock(int initX, int initY,
BOOL bErase,
int indexBmp)
{//画单各方快
CDC* pDC=GetDC();
CDC memDC;
CBrush brush;
CRect rcBlock;
if(bErase)//擦除
{
rcBlock.SetRect(initX*20+m_nInitX,initY*20+m_nInitY,initX*20+m_nInitX+20,initY*20+m_nInitY+20);
if(!m_bBackGround)
{
brush.CreateSolidBrush(RGB(0,0,0));
pDC->FillRect(&rcBlock,&brush);
brush.DeleteObject();
}
else
{
pDC->FillRect(&rcBlock,&m_brBack);
}
}
else
{
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(m_BmpBlock[indexBmp]);
pDC->BitBlt(initX*20+m_nInitX,initY*20+m_nInitY,20,20,&memDC,0,0,SRCCOPY);
memDC.DeleteDC();
}
pDC->DeleteDC();
}
//键盘接口
void CElsGameDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(m_bPlaying)//||m_bPause
{
switch(nChar)
{
case VK_LEFT:
if(!m_bPause)
{
if(CheckCanMove(2))
{
DrawCurBlock(TRUE);
m_nCurIndexX--;
DrawCurBlock(FALSE);
}
}
break;
case VK_RIGHT:
if(!m_bPause)
{
if(CheckCanMove(3))
{
DrawCurBlock(TRUE);
m_nCurIndexX++;
DrawCurBlock(FALSE);
}
}
break;
case VK_DOWN:
if(!m_bPause)
{
if(CheckCanMove(1))
{
DrawCurBlock(TRUE);
m_nCurIndexY++;
DrawCurBlock(FALSE);
}
else
{
if(CheckOver())
{
DoGameOver();
}
else
{
SetArrayBlockData();
if(CheckFullLine())
InvalidateRect(&m_rcGame,FALSE);
DrawNextBlock(TRUE);
CreateNewBlock();
DrawNextBlock(FALSE);
}
}
}
break;
case VK_SPACE:
case VK_UP://ratate
if(!m_bPause)
{
if(CanRotate(m_nCurIndexX,m_nCurIndexY))
RotateBlock();
else
{
int mDX=AdaptRotatePos();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -