2 u011368478 u011368478 于 2013.07.21 13:35 提问

求大神给该段代码详细注解(MFC 俄罗斯方块)

// TetrisDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "myself.h"
#include "Tetris.h"
#include "TetrisDlg.h"
#include
#include
// #include
// #include //导入声音头文件
// #pragma comment(lib,"winmm.lib")//导入声音头文件库
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// 对话框数据
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// CTetrisDlg 对话框

CTetrisDlg::CTetrisDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTetrisDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTetrisDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTetrisDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_COMMAND(ID_32771, &CTetrisDlg::On32771)
ON_COMMAND(ID_32772, &CTetrisDlg::On32772)
ON_COMMAND(ID_32773, &CTetrisDlg::On32773)
ON_COMMAND(ID_32775, &CTetrisDlg::On32775)
ON_COMMAND(ID_32776, &CTetrisDlg::On32776)
ON_COMMAND(ID_32777, &CTetrisDlg::On32777)
ON_COMMAND(ID_32779, &CTetrisDlg::On32779)
ON_COMMAND(ID_32778, &CTetrisDlg::On32778)
END_MESSAGE_MAP()

// CTetrisDlg 消息处理程序

BOOL CTetrisDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// 将“关于...”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
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);
    }
}

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE);         // 设置大图标
SetIcon(m_hIcon, FALSE);        // 设置小图标

// TODO: 在此添加额外的初始化代码
srand((unsigned)time(NULL));
my_brush.CreateSolidBrush(RGB(0,0,0)); 
speed=1;
score=0;
gamestate=STOP;
return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE

}

void CTetrisDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void CTetrisDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

    SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

    // 使图标在工作区矩形中居中
    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;

    // 绘制图标
    dc.DrawIcon(x, y, m_hIcon);
}
else
{
    CRect rect;
    CPaintDC dc(this);
    GetClientRect(rect);
    dc.FillSolidRect(rect,RGB(0,0,0));

    CPen pen(PS_SOLID,1,RGB(255,255,255));
    pDC=GetDC();
    pDC->SelectObject(&pen);
    //绘制方框区
    pDC->MoveTo(MAINFRAME_X-1,MAINFRAME_Y-1);
    pDC->LineTo(MAINFRAME_X+MAX_COL*SIZE+1,MAINFRAME_Y-1);
    pDC->LineTo(MAINFRAME_X+MAX_COL*SIZE+1,MAINFRAME_Y+MAX_ROW*SIZE+1);
    pDC->LineTo(MAINFRAME_X-1,MAINFRAME_Y+MAX_ROW*SIZE+1);
    pDC->LineTo(MAINFRAME_X-1,MAINFRAME_Y-1);
    //绘制预览区
    pDC->MoveTo(LOOK_X,LOOK_Y);
    pDC->LineTo(LOOK_X+6*SIZE,LOOK_Y);
    pDC->LineTo(LOOK_X+6*SIZE,LOOK_Y+6*SIZE);
    pDC->LineTo(LOOK_X,LOOK_Y+6*SIZE);
    pDC->LineTo(LOOK_X,LOOK_Y);
    //绘制文字
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(255,0,0));
    pDC->TextOut(LOOK_X,LOOK_Y+SIZE*6+20,_T("Score:"),6);
    pDC->SetTextColor(RGB(0,255,0));
    pDC->TextOut(LOOK_X,LOOK_Y+SIZE*6+55,_T("Speed:"),6);
    //绘制图标
    BITMAP bm1;
    CDC MemDC1;
    pDC=GetDC();
    my_bitmap.LoadBitmap(IDB_PAUSE);
    my_bitmap.GetObject(sizeof(BITMAP),&bm1);
    MemDC1.CreateCompatibleDC(pDC);
    MemDC1.SelectObject(&my_bitmap);
    pDC->StretchBlt(LOOK_X+45,LOOK_Y+SIZE*6+175,bm1.bmWidth,bm1.bmHeight,&MemDC1,0,0,40,40,SRCCOPY);
    my_bitmap.DeleteObject();

    BITMAP bm2;
    CDC MemDC2;
    my_bitmap.LoadBitmap(IDB_START);
    my_bitmap.GetObject(sizeof(BITMAP),&bm2);
    MemDC2.CreateCompatibleDC(pDC);
    MemDC2.SelectObject(&my_bitmap);
    pDC->StretchBlt(LOOK_X+45,LOOK_Y+SIZE*6+100,bm2.bmWidth,bm2.bmHeight,&MemDC2,0,0,40,40,SRCCOPY);
    my_bitmap.DeleteObject();

    BITMAP bm3;
    CDC MemDC3;
    my_bitmap.LoadBitmap(IDB_STOP);
    my_bitmap.GetObject(sizeof(BITMAP),&bm3);
    MemDC3.CreateCompatibleDC(pDC);
    MemDC3.SelectObject(&my_bitmap);
    pDC->StretchBlt(LOOK_X+45,LOOK_Y+SIZE*6+250,bm3.bmWidth,bm3.bmHeight,&MemDC3,0,0,40,40,SRCCOPY);
    my_bitmap.DeleteObject();

    DrawNextState(LOOK_X,LOOK_Y);
    for (int i=0;i<MAX_ROW;i++)
    {
        for (int j=0;j<MAX_COL;j++)
        {
             if (stateMap[i][j]==MAP_STATE_NOT_EMPTY)
             {
                 BITMAP bm;
                 CDC MemDC;
                 pDC=GetDC();
                 my_bitmap.LoadBitmap(IDB_SQUARE);
                 my_bitmap.GetObject(sizeof(BITMAP),&bm);
                 MemDC.CreateCompatibleDC(pDC);
                 MemDC.SelectObject(&my_bitmap);
                 pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,SIZE,SIZE,&MemDC,0,0,SIZE,SIZE,SRCCOPY);
                 my_bitmap.DeleteObject();
             }
             else if (stateMap[i][j]==MAP_FALLING)
             {
                 BITMAP bm;
                 CDC MemDC;
                 pDC=GetDC();
                 my_bitmap.LoadBitmap(IDB_SQUARE);
                 my_bitmap.GetObject(sizeof(BITMAP),&bm);
                 MemDC.CreateCompatibleDC(pDC);
                 MemDC.SelectObject(&my_bitmap);
                 pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
                 my_bitmap.DeleteObject();
             } 
             else 
             {
                 CRect my(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,MAINFRAME_X+j*SIZE+SIZE,MAINFRAME_Y+i*SIZE+SIZE);
                 pDC=GetDC();
                 pDC->FillRect(my,&my_brush);
             }
        }
    }
    pDC=GetDC();
    CRect my(LOOK_X+50,LOOK_Y+SIZE*6+20,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
    pDC->FillRect(my,&my_brush);
    pDC=GetDC();
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(255,0,0));
    CString Cscore;
    Cscore.Format(_T("%d"),score);
    pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+20,Cscore,Cscore.GetLength());
    CString Cspeed;
    switch(speed)
    {
    case 1:
        Cspeed.Format(_T("简单"));
        break;
    case 2:
        Cspeed.Format(_T("一般"));
        break;
    case 3:
        Cspeed.Format(_T("困难"));
        break;
    }
    pDC->SetTextColor(RGB(0,255,0));
    pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+55,Cspeed,Cspeed.GetLength());
    CDialog::OnPaint();
}

}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTetrisDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}

void CTetrisDlg::start()
{
score=0;
InitMap();
Produce_NextState();
NexttoActive();
Produce_NextState();
ActivetoMap();

 pDC=GetDC();
 pDC->SetBkMode(TRANSPARENT);
 pDC->SetTextColor(RGB(255,0,0));
 CString Cscore;
 Cscore.Format(_T("%d"),score);
 pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+20,Cscore,Cscore.GetLength());
 CString Cspeed;
 switch(speed)
 {
 case 1:
     Cspeed.Format(_T("简单"));
     break;
 case 2:
     Cspeed.Format(_T("一般"));
     break;
 case 3:
     Cspeed.Format(_T("困难"));
     break;
 }
 pDC->SetTextColor(RGB(0,255,0));
 pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+55,Cspeed,Cspeed.GetLength());
 if (gamestate==OVER)
     gameover();

}

void CTetrisDlg::Produce_NextState()
{
int Shape=rand()%7;
NextColor=rand()%4+1;
switch(Shape) //调试中
{
case 0:
{
switch(rand()%4)
{
case 0: //山条,完成
{
NextState[0][0]=2;
NextState[0][1]=0;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
case 1:
{
NextState[0][0]=2;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=3;
NextState[3][1]=2;
break;
}
case 2:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=1;
NextState[3][1]=3;
break;
}
case 3:
{
NextState[0][0]=0;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=1;
NextState[3][1]=2;
break;
}
default:break;
}
break;
}
case 1: //长条,完成
{
switch(rand()%4)
{
case 0:
{
NextState[0][0]=0;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=3;
NextState[3][1]=1;
break;
}
case 1:
{
NextState[0][0]=2;
NextState[0][1]=0;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=3;
break;
}
case 2:
{
NextState[0][0]=0;
NextState[0][1]=2;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=3;
NextState[3][1]=2;
break;
}
case 3:
{
NextState[0][0]=1;
NextState[0][1]=0;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=1;
NextState[3][1]=3;
break;
}
default:break;
}
break;
}
case 2: //L,完成
{
switch(rand()%4)
{
case 0:
{
NextState[0][0]=2;
NextState[0][1]=0;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
case 1:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=3;
NextState[3][1]=2;
break;
}
case 2:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=1;
NextState[3][1]=3;
break;
}
case 3:
{
NextState[0][0]=0;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
default:break;
}
break;
}
case 3: //倒L,完成
{
switch(rand()%4)
{
case 0:
{
NextState[0][0]=1;
NextState[0][1]=0;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
case 1:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=3;
NextState[2][1]=1;
NextState[3][0]=1;
NextState[3][1]=2;
break;
}
case 2:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=3;
break;
}
case 3:
{
NextState[0][0]=2;
NextState[0][1]=1;
NextState[1][0]=0;
NextState[1][1]=2;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
default:break;
}
break;
}
case 4: //正Z
{
switch(rand()%4)
{
case 0:
{
NextState[0][0]=1;
NextState[0][1]=0;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
case 1:
{
NextState[0][0]=2;
NextState[0][1]=1;
NextState[1][0]=3;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
case 2:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=3;
break;
}
case 3:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=0;
NextState[2][1]=2;
NextState[3][0]=1;
NextState[3][1]=2;
break;
}
default:break;
}
break;
}
case 5: //倒Z
{
switch(rand()%4)
{
case 0:
{
NextState[0][0]=2;
NextState[0][1]=0;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=1;
NextState[3][0]=1;
NextState[3][1]=2;
break;
}
case 1:
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=3;
NextState[3][1]=2;
break;
}
case 2:
{
NextState[0][0]=2;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=2;
NextState[2][0]=2;
NextState[2][1]=2;
NextState[3][0]=1;
NextState[3][1]=3;
break;
}
case 3:
{
NextState[0][0]=0;
NextState[0][1]=1;
NextState[1][0]=1;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
default:break;
}
break;
}
case 6: //方格,完成
{
NextState[0][0]=1;
NextState[0][1]=1;
NextState[1][0]=2;
NextState[1][1]=1;
NextState[2][0]=1;
NextState[2][1]=2;
NextState[3][0]=2;
NextState[3][1]=2;
break;
}
default:break;
}
}

void CTetrisDlg::NexttoActive()
{
for (int i=0;i for (int j=0;j ActiveState[i][j]=NextState[i][j];
ActiveColor=NextColor;
}
void CTetrisDlg::DrawNextState(int x,int y)
{
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);
for (int i=0;i {
if (NextState[i][1]>=0&&NextState[i][0]>=0)
pDC->StretchBlt(x+SIZE+NextState[i][1]*SIZE,y+SIZE+NextState[i][0]*SIZE,bm.bmWidth,SIZE,&MemDC,0,SIZE*NextColor,SIZE,SIZE,SRCCOPY);
}
my_bitmap.DeleteObject();
}
void CTetrisDlg::ActivetoMap()
{
int min_row;
int min_col;
min_row=ActiveState[0][0];
if (ActiveState[1][0] min_row=ActiveState[1][0];
if (ActiveState[2][0] min_row=ActiveState[2][0];
if (ActiveState[3][0] min_row=ActiveState[3][0];
min_col=ActiveState[0][1];
if (ActiveState[1][1] min_col=ActiveState[1][1];
if (ActiveState[2][1] min_col=ActiveState[2][1];
if (ActiveState[3][1] min_col=ActiveState[3][1];
for (int i=0;i {
if (stateMap[ActiveState[i][0]-min_row][MAX_COL/2-1+ActiveState[i][1]-min_col]==MAP_STATE_NOT_EMPTY)
{ gamestate=OVER;
break;
}
else
stateMap[ActiveState[i][0]-min_row][MAX_COL/2-1+ActiveState[i][1]-min_col]=MAP_FALLING;
}
}
void CTetrisDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if ((point.x>=LOOK_X+45)&&(point.x<=LOOK_X+85)&&(point.y>=(LOOK_Y+SIZE*6+100))&&(point.y<=(LOOK_Y+SIZE*6+140))&&gamestate>=STOP)
{
SetTimer(1,600/speed,NULL);
start();
gamestate=PLAY;
OnPaint();
}
if ((point.x>=LOOK_X+45)&&(point.x<=LOOK_X+85)&&(point.y>=(LOOK_Y+SIZE*6+100))&&(point.y<=(LOOK_Y+SIZE*6+140))&&gamestate==PAUSE)
{
SetTimer(1,600/speed,NULL);
gamestate=PLAY;
}
if ((point.x>=LOOK_X+45)&&(point.x<=LOOK_X+85)&&(point.y>=(LOOK_Y+SIZE*6+175))&&(point.y<=(LOOK_Y+SIZE*6+215))&&gamestate==PLAY)
{
KillTimer(1);
gamestate=PAUSE;
}
if ((point.x>=LOOK_X+45)&&(point.x<=LOOK_X+85)&&(point.y>=(LOOK_Y+SIZE*6+250))&&(point.y<=(LOOK_Y+SIZE*6+290))&&gamestate<=PAUSE)
{
KillTimer(1);
pDC=GetDC();
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+20,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
InitMap();
InitNextAndActive();
gamestate=STOP;
DrawMapBlack();
DrawLookBlack();
}
CDialog::OnLButtonDown(nFlags, point);
}

void CTetrisDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent==1)
{
if (IsBottom()==1)
{
for (int i=0;i {
for (int j=0;j {
if (stateMap[i][j]==MAP_FALLING)
{
stateMap[i][j]=MAP_STATE_NOT_EMPTY;
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);
pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,SIZE,SIZE,&MemDC,0,0,SIZE,SIZE,SRCCOPY);
my_bitmap.DeleteObject();
}
}
}
Sweep();
NexttoActive();
Produce_NextState();
DrawLookBlack();
DrawNextState(LOOK_X,LOOK_Y);
ActivetoMap();
if (gamestate==OVER)
gameover();
else {

             BITMAP bm;
             CDC MemDC;
             pDC=GetDC();
             my_bitmap.LoadBitmap(IDB_SQUARE);
             my_bitmap.GetObject(sizeof(BITMAP),&bm);
             MemDC.CreateCompatibleDC(pDC);
             MemDC.SelectObject(&my_bitmap);
         for (int i=0;i<4;i++)
         {
              for (int j=0;j<MAX_COL;j++)
              {
                  if (stateMap[i][j]==MAP_FALLING)
                  {
                      pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
                  } 
              }
         }
         my_bitmap.DeleteObject();
         }
     }
     else 
     {
         BITMAP bm;
         CDC MemDC;
         pDC=GetDC();
         my_bitmap.LoadBitmap(IDB_SQUARE);
         my_bitmap.GetObject(sizeof(BITMAP),&bm);
         MemDC.CreateCompatibleDC(pDC);
         MemDC.SelectObject(&my_bitmap);
         for (int i=MAX_ROW-1;i>=0;i--)
         {
              for (int j=MAX_COL-1;j>=0;j--)
              {
                   if (stateMap[i][j]==MAP_FALLING)
                   {
                         stateMap[i][j]=MAP_STATE_EMPTY;
                         stateMap[i+1][j]=MAP_FALLING;

                         pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+(i+1)*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);

                         CRect my(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,MAINFRAME_X+j*SIZE+SIZE,MAINFRAME_Y+i*SIZE+SIZE);
                         pDC=GetDC();
                         pDC->FillRect(my,&my_brush);
                   }
              }
         }
         my_bitmap.DeleteObject();
     }
}
KillTimer(1);
SetTimer(1,600/speed,NULL);
CDialog::OnTimer(nIDEvent);

}
int CTetrisDlg::IsLeftLimit() //1代表不能左移
{
for (int i=0;i<MAX_ROW;i++)
{
for (int j=0;j<MAX_COL;j++)
{
if (stateMap[i][j]==MAP_FALLING)
{
if ((j==0)||stateMap[i][j-1]==MAP_STATE_NOT_EMPTY)
return 1;
}
}
}
return 0;
}
int CTetrisDlg::IsRightLimit()
{
for (int i=0;i<MAX_ROW;i++)
{
for (int j=0;j<MAX_COL;j++)
{
if (stateMap[i][j]==MAP_FALLING)
{
if ((j==MAX_COL-1)||stateMap[i][j+1]==MAP_STATE_NOT_EMPTY)
return 1;
}
}
}
return 0;
}
int CTetrisDlg::IsBottom()
{
for (int i=0;i<MAX_ROW;i++)
{
for (int j=0;j<MAX_COL;j++)
{
if (stateMap[i][j]==MAP_FALLING)
{
if ((i==MAX_ROW-1)||stateMap[i+1][j]==MAP_STATE_NOT_EMPTY)
{

return 1;
}
}
}
}
return 0;
}

void CTetrisDlg::InitMap()
{
for (int i=0;i {
for (int j=0;j {
stateMap[i][j]=MAP_STATE_EMPTY;
}
}
}
void CTetrisDlg::InitNextAndActive()
{
for (int i=0;i {
for (int j=0;j {
NextState[i][j]=-1;
ActiveState[i][j]=-1;
}
}
}
void CTetrisDlg::DrawMapBlack()
{
for (int i=0;i {
for (int j=0;j {
CRect my(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,MAINFRAME_X+j*SIZE+SIZE,MAINFRAME_Y+i*SIZE+SIZE);
pDC=GetDC();
pDC->FillRect(my,&my_brush);
}
}
}
void CTetrisDlg::DrawLookBlack()
{
CRect my(LOOK_X+1,LOOK_Y+1,LOOK_X+6*SIZE-1,LOOK_Y+6*SIZE-1);
pDC=GetDC();
pDC->FillRect(my,&my_brush);
}
void CTetrisDlg::gameover()
{
KillTimer(1);
CString notice;
notice.Format(_T("GAME IS OVER!\nYOUR SCORE IS %d"),score);
AfxMessageBox(notice);
/////////////////////////////////////////清除记录的数字
pDC=GetDC();
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+20,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
InitMap();
InitNextAndActive();
gamestate=STOP;
DrawMapBlack();
DrawLookBlack();
}
void CTetrisDlg::RectLeft()
{
//PlaySound(_T("sounds\rotateANDmove.WAV"),NULL,SND_FILENAME|SND_ASYNC);
if (IsLeftLimit()==0)
{
int temp[4][2];
int count=0;
for (int i=0;i {
for (int j=0;j {
if (stateMap[i][j]==MAP_FALLING)
{
stateMap[i][j-1]=MAP_FALLING;
stateMap[i][j]=MAP_STATE_EMPTY;
temp [count][0]=i;
temp [count][1]=j-1;
count++;
}
}
}
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);
count--;
for (;count>=0;count--)
pDC->StretchBlt(MAINFRAME_X+temp[count][1]*SIZE,MAINFRAME_Y+temp[count][0]*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
my_bitmap.DeleteObject();
for (int i=0;i {
if (stateMap[temp[i][0]][temp[i][1]+1]==MAP_STATE_EMPTY)
{
CRect my(MAINFRAME_X+(temp[i][1]+1)*SIZE,MAINFRAME_Y+temp[i][0]*SIZE,MAINFRAME_X+SIZE+(temp[i][1]+1)*SIZE,MAINFRAME_Y+SIZE+temp[i][0]*SIZE);
pDC->FillRect(my,&my_brush);
}
}
}
}
void CTetrisDlg::RectRight()
{
if (IsRightLimit()==0)
{
int temp[4][2];
int count=0;
for (int i=0;i {
for (int j=MAX_COL-1;j>=0;j--)
{
if (stateMap[i][j]==MAP_FALLING)
{
stateMap[i][j+1]=MAP_FALLING;
stateMap[i][j]=MAP_STATE_EMPTY;
temp [count][0]=i;
temp [count][1]=j+1;
count++;
}
}
}
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);
count--;
for (;count>=0;count--)
pDC->StretchBlt(MAINFRAME_X+temp[count][1]*SIZE,MAINFRAME_Y+temp[count][0]*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
my_bitmap.DeleteObject();
for (int i=0;i {
if (stateMap[temp[i][0]][temp[i][1]-1]==MAP_STATE_EMPTY)
{
CRect my(MAINFRAME_X+(temp[i][1]-1)*SIZE,MAINFRAME_Y+temp[i][0]*SIZE,MAINFRAME_X+SIZE+(temp[i][1]-1)*SIZE,MAINFRAME_Y+SIZE+temp[i][0]*SIZE);
pDC->FillRect(my,&my_brush);
}
}
}
}
void CTetrisDlg::RectUp() //逆时针旋转
{

bool choice;
int temp[4][2];
for (int i=0;i<4;i++)
{
    temp[i][1]=ActiveState[i][0];
    temp[i][0]=3-ActiveState[i][1];
}
int locate[4][2];    //将旋转前falling状态的方块记住
int count=0;
for (int j=0;j<MAX_COL;j++)
{
    for (int i=0;i<MAX_ROW;i++)
    {
        if (stateMap[i][j]==MAP_FALLING)
        {
            locate[count][0]=i;
            locate[count][1]=j;
            ++count;
        }
    }
}
--count;
int travel[4][2];   //将旋转后的falling状态的方块记住
for (int i=0;i<4;i++)
    for (int j=0;j<2;j++)
        travel[i][j]=locate[i][j]+(temp[i][j]-ActiveState[i][j]);
choice=TRUE;
for (int i=0;i<4;i++)
{
    if (!(travel[i][0]>=0&&travel[i][0]<MAX_ROW&&travel[i][1]>=0&&travel[i][1]<MAX_COL&&stateMap[travel[i][0]][travel[i][1]]!=MAP_STATE_NOT_EMPTY))
        choice=FALSE;
}

if (choice==TRUE)
{
    pDC=GetDC();
    for (int i=0;i<4;i++)
    {
        stateMap[locate[i][0]][locate[i][1]]=MAP_STATE_EMPTY;
        //zuotu
        CRect my(MAINFRAME_X+locate[i][1]*SIZE,MAINFRAME_Y+locate[i][0]*SIZE,MAINFRAME_X+SIZE+locate[i][1]*SIZE,MAINFRAME_Y+SIZE+locate[i][0]*SIZE);
        pDC->FillRect(my,&my_brush);

    }
    BITMAP bm;
    CDC MemDC;
    pDC=GetDC();
    my_bitmap.LoadBitmap(IDB_SQUARE);
    my_bitmap.GetObject(sizeof(BITMAP),&bm);
    MemDC.CreateCompatibleDC(pDC);
    MemDC.SelectObject(&my_bitmap);
    for (int i=0;i<4;i++)
    {
        stateMap[travel[i][0]][travel[i][1]]=MAP_FALLING;
        //zuotu
        pDC->StretchBlt(MAINFRAME_X+travel[i][1]*SIZE,MAINFRAME_Y+travel[i][0]*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
    }
    my_bitmap.DeleteObject();
    for (int i=0;i<4;i++)
        for (int j=0;j<2;j++)
            ActiveState[i][j]=temp[i][j];
    for (int j=0;j<3;j++)
    {
        for (int i=2;i>=j;i--)
        if (ActiveState[i][1]>ActiveState[i+1][1]||(ActiveState[i][1]==ActiveState[i+1][1]&&ActiveState[i][0]>ActiveState[i+1][0]))
        {
             int x=ActiveState[i+1][0];
             int y=ActiveState[i+1][1];
             ActiveState[i+1][0]=ActiveState[i][0];
             ActiveState[i+1][1]=ActiveState[i][1];
             ActiveState[i][0]=x;
             ActiveState[i][1]=y;
        }
    }
}

}
void CTetrisDlg::RectDown()
{
int temp[4][2];
int origin[4][2];
int count=0;
for (int i=0;i {
for (int j=0;j {
if (stateMap[i][j]==MAP_FALLING)
{
origin[count][0]=temp [count][0]=i;
origin[count][1]=temp [count][1]=j;
count++;
}
}
}
if (!IsBottom())
{
for (int i=3;i>=0;i--)
{
stateMap [temp[i][0]+1][temp[i][1]]=MAP_FALLING;
stateMap [temp[i][0]][temp[i][1]]=MAP_STATE_EMPTY;
temp[i][0]++;
}
}
for (int i=0;i {
if (stateMap[origin[i][0]][origin[i][1]]==MAP_STATE_EMPTY)
{
CRect my(MAINFRAME_X+(origin[i][1])*SIZE,MAINFRAME_Y+origin[i][0]*SIZE,MAINFRAME_X+(origin[i][1])*SIZE+SIZE,MAINFRAME_Y+origin[i][0]*SIZE+SIZE);
pDC->FillRect(my,&my_brush);
}
}
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);
for (int i=0;i pDC->StretchBlt(MAINFRAME_X+temp[i][1]*SIZE,MAINFRAME_Y+temp[i][0]*SIZE,SIZE,SIZE,&MemDC,0,SIZE*ActiveColor,SIZE,SIZE,SRCCOPY);
my_bitmap.DeleteObject();
}
BOOL CTetrisDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN&&gamestate==PLAY)
{
switch(pMsg->wParam)
{
case VK_LEFT:
RectLeft();
break;
case VK_RIGHT:
RectRight();
break;
case VK_UP:
RectUp();
break;
case VK_DOWN:
RectDown();
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}

void CTetrisDlg::Sweep()
{
bool m_sweep;
int bottom;
int judge=0;
int ii=0;
for (int i=MAX_ROW-1;i>=0;i--)
{

m_sweep=TRUE;
for (int j=0;j {
if(stateMap[i][j]==MAP_STATE_EMPTY)
{
m_sweep=FALSE;
break;
}
}
if (m_sweep)
{
if (judge!=1)
{
bottom=i;
judge=1;
}
for (int k=i;k>0;k--)
{
for (int l=0;l<MAX_COL;l++)
{
stateMap[k][l]=stateMap[k-1][l];
}
}
for (int l=0;l<MAX_COL;l++)
stateMap[0][l]=MAP_STATE_EMPTY;
i++;
ii++;
}
}
if (judge==1)
{
BITMAP bm;
CDC MemDC;
pDC=GetDC();
my_bitmap.LoadBitmap(IDB_SQUARE);
my_bitmap.GetObject(sizeof(BITMAP),&bm);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&my_bitmap);

       for (int i=bottom;i>=0;i--)
       for (int j=0;j<MAX_COL;j++)
       {
             if (stateMap[i][j]==MAP_STATE_NOT_EMPTY)
             pDC->StretchBlt(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,SIZE,SIZE,&MemDC,0,0,SIZE,SIZE,SRCCOPY);
             else
             {
                 CRect my(MAINFRAME_X+j*SIZE,MAINFRAME_Y+i*SIZE,MAINFRAME_X+j*SIZE+SIZE,MAINFRAME_Y+i*SIZE+SIZE);
                 pDC->FillRect(my,&my_brush);
             }
       }
       my_bitmap.DeleteObject();
       score+=ii*ii*100;
       CRect my(LOOK_X+50,LOOK_Y+SIZE*6+20,LOOK_X+100,LOOK_Y+SIZE*6+55);
       pDC=GetDC();
       pDC->FillRect(my,&my_brush);
       pDC->SetBkMode(TRANSPARENT);
       pDC->SetTextColor(RGB(255,0,0));
       CString Cscore;
       Cscore.Format(_T("%d"),score);
       pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+20,Cscore,Cscore.GetLength());
     //  PlaySound(_T("sounds\\line.WAV"),NULL,SND_FILENAME|SND_ASYNC);
 }

}

void CTetrisDlg::On32771()
{
if (gamestate>=STOP)
{
SetTimer(1,600/speed,NULL);
start();
gamestate=PLAY;
OnPaint();
}
if (gamestate==PAUSE)
{
SetTimer(1,600/speed,NULL);
gamestate=PLAY;
}
}

void CTetrisDlg::On32772()
{
if (gamestate==PLAY)
{
KillTimer(1);
gamestate=PAUSE;
}
}

void CTetrisDlg::On32773()
{
if (gamestate<=PAUSE)
{
KillTimer(1);
pDC=GetDC();
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+20,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
InitMap();
InitNextAndActive();
gamestate=STOP;
DrawMapBlack();
DrawLookBlack();
}
}

void CTetrisDlg::On32775()
{
speed=1;
pDC=GetDC();
pDC->SetBkMode(TRANSPARENT);
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+55,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
CString Cspeed;
Cspeed.Format(_T("简单"));
pDC->SetTextColor(RGB(0,255,0));
pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+55,Cspeed,Cspeed.GetLength());
}

void CTetrisDlg::On32776()
{
speed=2;
pDC=GetDC();
pDC->SetBkMode(TRANSPARENT);
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+55,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
CString Cspeed;
Cspeed.Format(_T("一般"));
pDC->SetTextColor(RGB(0,255,0));
pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+55,Cspeed,Cspeed.GetLength());
}

void CTetrisDlg::On32777()
{
speed=3;
pDC=GetDC();
pDC->SetBkMode(TRANSPARENT);
CRect my(LOOK_X+50,LOOK_Y+SIZE*6+55,LOOK_X+6*SIZE,LOOK_Y+SIZE*6+100);
pDC->FillRect(my,&my_brush);
CString Cspeed;
Cspeed.Format(_T("困难"));
pDC->SetTextColor(RGB(0,255,0));
pDC->TextOut(LOOK_X+50,LOOK_Y+SIZE*6+55,Cspeed,Cspeed.GetLength());
}

void CTetrisDlg::On32779()
{
OnCancel();
}

void CTetrisDlg::On32778()
{
AfxMessageBox(_T("通过键盘控制方块移动和旋转\n上:旋转物块\n下:加速下滑\n左、右:左右移动\n当一行全部填满可消去"));
}
void CTetrisDlg::OnOK()
{}

// PlaySound(_T("sounds\\question.wav"),NULL,SND_FILENAME|SND_ASYNC);
Csdn user default icon
上传中...
上传图片
插入图片