⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rusgamedlg.cpp

📁 俄罗斯方块
💻 CPP
字号:
// RusGameDlg.cpp : implementation file
//

#include "stdlib.h"
#include "stdafx.h"
#include "RusGame.h"
#include "RusGameDlg.h"
#include "dlg_help.h"
#include "dlg_about.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDC_TIMER 1
#define TIMER 	700
			
CRusGameDlg::CRusGameDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRusGameDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRusGameDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRusGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRusGameDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRusGameDlg, CDialog)
	//{{AFX_MSG_MAP(CRusGameDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_COMMAND(ID_NewGame, NewGame)
	ON_COMMAND(ID_GameStop, GameStop)
	ON_COMMAND(ID_ReStartGame, ReStartGame)
	ON_COMMAND(ID_QuitGame, QuitGame)
	ON_COMMAND(ID_HELP, GameHelp)
	ON_COMMAND(ID_ABOUT, AboutGame)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRusGameDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CRusGameDlg::OnPaint() 
{
	CWnd*pwnd=GetDlgItem(IDC_STATIC1);   
    CDC *pcontroldc=pwnd->GetDC(); 
    pwnd->Invalidate();   
    pwnd->UpdateWindow();
//	BkColor=GetPixel(*pcontroldc,0,0);
	CPen pen(PS_SOLID,1,RGB(255,255,255));
	pcontroldc->SelectObject(&pen);
    pcontroldc->SelectStockObject(WHITE_BRUSH);   
	pcontroldc->Rectangle(0,0,nGridX*CubSize,nGridY*CubSize);
	pwnd->ReleaseDC(pcontroldc); 

	pwnd=GetDlgItem(IDC_STATIC2);
	pcontroldc=pwnd->GetDC();  
	pwnd->Invalidate();   
    pwnd->UpdateWindow();
	pcontroldc->SelectStockObject(BLACK_BRUSH);   
	pcontroldc->SelectObject(CreatePen(PS_SOLID,1,RGB(255,255,255)));
	for(int i=0;i<4;i++)
		pcontroldc->Rectangle(Cub[rsg.CubNum][i].x*CubSize,Cub[rsg.CubNum][i].y*CubSize,(Cub[rsg.CubNum][i].x+1)*CubSize,(Cub[rsg.CubNum][i].y+1)*CubSize);	
	pwnd->ReleaseDC(pcontroldc);
   
//	CFont font;
	VERIFY(font.CreateFont(
	 13,                        // nHeight
	 0,                         // nWidth
	 0,                         // nEscapement
	 0,                         // nOrientation
     FW_NORMAL,                 // nWeight
     FALSE,                     // bItalic
     FALSE,                     // bUnderline
	 0,                         // cStrikeOut
	 ANSI_CHARSET,              // nCharSet
	 OUT_DEFAULT_PRECIS,        // nOutPrecision
     CLIP_DEFAULT_PRECIS,       // nClipPrecision
     DEFAULT_QUALITY,           // nQuality
     DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
     "Arial"));                 // lpszFacename
  	CClientDC pdc(this);
	pdc.SelectObject(&font);
 	pdc.SetBkColor(RGB(192,192,192));   
	pdc.TextOut(180,140,"L: 0",4);
	pdc.TextOut(180,160,"S: 0",4);
//	pdc.TextOut(180,140,"L: 0",4);
	ReleaseDC(&pdc);

		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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRusGameDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CRusGameDlg::NewGame() 
{
	// TODO: Add your command handler code here
	rsg.GameStart();
	rsg.NewCub();
	CRusGameDlg::DrawGameGrid(nGridX,nGridY,IDC_STATIC1,WHITE_BRUSH,RGB(255,255,255));
	CRusGameDlg::DrawGameGrid(4,4,IDC_STATIC2,LTGRAY_BRUSH,RGB(192,192,192));
	CRusGameDlg::DrawCub(IDC_STATIC2,BLACK_BRUSH,Cub[rsg.CubNum]);
	CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
	CClientDC pdc(this);
	pdc.SelectObject(CreatePen(PS_SOLID,1,RGB(192,192,192)));
	pdc.SelectStockObject(LTGRAY_BRUSH);
	pdc.Rectangle(180,140,300,180);
	pdc.SelectObject(&font);
 	pdc.SetBkColor(RGB(192,192,192));   
	pdc.TextOut(180,140,"L: 0",4);
	pdc.TextOut(180,160,"S: 0",4);
	ReleaseDC(&pdc);
	SetTimer(IDC_TIMER,TIMER,NULL);
}

void CRusGameDlg::GameStop() 
{
	// TODO: Add your command handler code here
	rsg.isStop=true;
//	KillTimer(ID_TIMER);	
}

void CRusGameDlg::ReStartGame() 
{
	// TODO: Add your command handler code here
	rsg.isStop=false;
//	SetTimer(ID_TIMER,TIMER,NULL);
}

void CRusGameDlg::GameOver()
{
	KillTimer(IDC_TIMER);
	/////////
	CDC *pDC = GetDC();
	HBITMAP bitmap;
	CBitmap cbmp;
	BITMAP  bmp;
	cbmp.LoadBitmap(IDB_BITMAP1);
	bitmap=(HBITMAP)cbmp.m_hObject;
	cbmp.GetBitmap(&bmp);
	//MoveWindow(0,0,bmp.bmWidth,bmp.bmHeight+15,TRUE);
	//bitmap=(HBITMAP)LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(129));
	ASSERT(bitmap);
	HBITMAP oldbitmap;
	CDC memdc;
	CRect rect;
	memdc.CreateCompatibleDC(pDC);
	GetClientRect(rect);
	oldbitmap=(HBITMAP)memdc.SelectObject(bitmap);
	///显示它
	pDC->BitBlt(40,120,rect.Width(),rect.Height(),&memdc,0,0,SRCCOPY);
	memdc.SelectObject(oldbitmap);
//	delete memdc;
//	ReleaseDC(pDC);
}

void CRusGameDlg::QuitGame() 
{
	// TODO: Add your command handler code here
	HWND hItem=::GetDlgItem(m_hWnd, IDCANCEL);
	if (hItem==NULL||::IsWindowEnabled(hItem))
		SendMessage(WM_COMMAND, IDCANCEL, 0);
}

void CRusGameDlg::GameHelp() 
{
	// TODO: Add your command handler code here
	dlg_help dlg;
	dlg.DoModal();	
}

void CRusGameDlg::AboutGame() 
{
	// TODO: Add your command handler code here
	dlg_about dlg;
	dlg.DoModal();
}

void CRusGameDlg::DrawCub(int ID,int Brush,const point* c)
{
	CWnd* pwnd=GetDlgItem(ID);
	CDC* pcontroldc=pwnd->GetDC();  
	pcontroldc->SelectStockObject(Brush);   
	pcontroldc->SelectObject(CreatePen(PS_SOLID,1,RGB(255,255,255)));
	for(int i=0;i<4;i++)
		pcontroldc->Rectangle(c[i].x*CubSize,c[i].y*CubSize,(c[i].x+1)*CubSize,(c[i].y+1)*CubSize);	
	pwnd->ReleaseDC(pcontroldc);   
}

void CRusGameDlg::DrawGameGrid(int x,int y,int ID,int Brush,COLORREF color)
{
	CWnd*pwnd=GetDlgItem(ID);   
    CDC *pcontroldc=pwnd->GetDC(); 
//  pwnd->Invalidate();   
//  pwnd->UpdateWindow();
	CPen pen(PS_SOLID,1,color);
	pcontroldc->SelectObject(&pen);
    pcontroldc->SelectStockObject(Brush);   
	pcontroldc->Rectangle(0,0,x*CubSize,y*CubSize);	
	pwnd->ReleaseDC(pcontroldc);   
}

BOOL CRusGameDlg::PreTranslateMessage(MSG *pMsg)
{
	if(pMsg->message==WM_KEYDOWN)
 	{
		if(rsg.isStart&&!rsg.isStop)
 		{
			CRusGameDlg::DrawCub(IDC_STATIC1,WHITE_BRUSH,rsg.CurrentCub);
			switch(pMsg->wParam)
  			{
  			case VK_UP:
  				rsg.Rot();//
  				break;
 			case VK_LEFT:
 				rsg.MoveLt();
 				break;
 			case VK_RIGHT:
 				rsg.MoveRt();
 				break;
			case VK_DOWN:
				if(!rsg.QuickDn())
				{
					if(!rsg.isOver())
					{
						CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
						int y=rsg.CanGainScore();
						if(y)
						{
							RefreshGame(y);
							CClientDC pdc(this);
							pdc.SelectObject(CreatePen(PS_SOLID,1,RGB(192,192,192)));
							pdc.SelectStockObject(LTGRAY_BRUSH);
							pdc.Rectangle(180,140,300,160);
							displayScore(&pdc,rsg.nLine,'L');
							displayScore(&pdc,rsg.Score,'S');
							ReleaseDC(&pdc);
						}
//						rsg.CanGainScore();
						rsg.NewCub();
						CRusGameDlg::DrawGameGrid(4,4,IDC_STATIC2,LTGRAY_BRUSH,RGB(192,192,192));
						CRusGameDlg::DrawCub(IDC_STATIC2,BLACK_BRUSH,Cub[rsg.CubNum]);
						CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
					}
					else
					{
						CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
						CRusGameDlg::GameOver();
					}
					return 1;
				}
			}
			CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
 		}
 	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CRusGameDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(rsg.isStart&&!rsg.isStop)
	{
		if(!rsg.isBottom())
		{
			CRusGameDlg::DrawCub(IDC_STATIC1,WHITE_BRUSH,rsg.CurrentCub);
			if(!rsg.QuickDn())
			{
				if(!rsg.isOver())
				{
					CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
					int y=rsg.CanGainScore();
					if(y)
					{
						RefreshGame(y);
						CClientDC pdc(this);
						pdc.SelectObject(CreatePen(PS_SOLID,1,RGB(192,192,192)));
						pdc.SelectStockObject(LTGRAY_BRUSH);
						pdc.Rectangle(180,140,300,160);
						displayScore(&pdc,rsg.nLine,'L');
						displayScore(&pdc,rsg.Score,'S');
						ReleaseDC(&pdc);
					}
//					rsg.CanGainScore();
					rsg.NewCub();
					CRusGameDlg::DrawGameGrid(4,4,IDC_STATIC2,LTGRAY_BRUSH,RGB(192,192,192));
					CRusGameDlg::DrawCub(IDC_STATIC2,BLACK_BRUSH,Cub[rsg.CubNum]);
					CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
				}
				else
				{
					CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
					CRusGameDlg::GameOver();
				}
				return;
			}
			CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
		}
		else if(!rsg.isOver())
		{
			int y=rsg.CanGainScore();
			if(y)
			{
				RefreshGame(y);
				CClientDC pdc(this);
				pdc.SelectObject(CreatePen(PS_SOLID,1,RGB(192,192,192)));
				pdc.SelectStockObject(LTGRAY_BRUSH);
				pdc.Rectangle(180,140,300,160);
				displayScore(&pdc,rsg.nLine,'L');
				displayScore(&pdc,rsg.Score,'S');
				ReleaseDC(&pdc);
			}			
			rsg.NewCub();
			CRusGameDlg::DrawGameGrid(4,4,IDC_STATIC2,LTGRAY_BRUSH,RGB(192,192,192));
			CRusGameDlg::DrawCub(IDC_STATIC2,BLACK_BRUSH,Cub[rsg.CubNum]);
			CRusGameDlg::DrawCub(IDC_STATIC1,BLACK_BRUSH,rsg.CurrentCub);
		}
		else 
			CRusGameDlg::GameOver();
	}
	CDialog::OnTimer(nIDEvent);
}

void CRusGameDlg::RefreshGame(int y)
{
	CWnd* pwnd=GetDlgItem(IDC_STATIC1);
	CDC* pcontroldc=pwnd->GetDC();   
	pcontroldc->SelectObject(CreatePen(PS_SOLID,1,RGB(255,255,255)));
	for(int i=y;i>=0;i--)
		for(int j=0;j<nGridX;j++)
			if(rsg.GameGrid[i][j])
			{
				pcontroldc->SelectStockObject(BLACK_BRUSH);   
				pcontroldc->Rectangle(j*CubSize,i*CubSize,(j+1)*CubSize,(i+1)*CubSize);	
			}
			else
			{    
				pcontroldc->SelectStockObject(WHITE_BRUSH);   
				pcontroldc->Rectangle(j*CubSize,i*CubSize,(j+1)*CubSize,(i+1)*CubSize);	
			}
	pwnd->ReleaseDC(pcontroldc);   
}

void CRusGameDlg::displayScore(CDC*pdc,int score,char c)
{
	pdc->SelectObject(&font);
	pdc->SetBkColor(RGB(192,192,192));   
	char s1[20]={c,':',' ','\0'};
	char s2[10];
	itoa(score,s2,10);
	strcat(s1,s2);
	switch(c)
	{
	case 'L':pdc->TextOut(180,140,s1,strlen(s1));break;	
	case 'S':pdc->TextOut(180,160,s1,strlen(s1));	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -