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

📄 diamonddlg.cpp

📁 This file can be directly edited in Microsoft Visual C++.新的俄罗师方块游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// DiamondDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Diamond.h"
#include "DiamondDlg.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "configdlg.h"
#include "RecordDlg.h"
#include "NewRecordDlg.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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiamondDlg dialog

CDiamondDlg::CDiamondDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDiamondDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDiamondDlg)
	m_bStart = FALSE;
	m_bPause=FALSE;
	m_bShowRaster=FALSE;
	m_AccelerateSpeed=1;
	m_pRecordDlg1=NULL;
	m_SettingLevel=1;
	m_Level = 1;
	m_Score = 0;
	m_maxScore = 0;
	m_recordCount = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	for(int i=0;i<22;i++)
		for(int j=0;j<10;j++)
		{
			m_Matrix[i][j][0]=0;
			m_Matrix[i][j][1]=0;
		}
	m_bkColor = RGB(0,0,0);
	m_RasterColor = RGB(0,255,0);
//	LoadAccelerators(NULL,IDR_ACCELERATOR1);
//	LoadAccelerators(
}

void CDiamondDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDiamondDlg)
	DDX_Control(pDX, IDC_WRITER, m_Writer);
	DDX_Control(pDX, IDC_SCORE, m_ScoreCtrl);
	DDX_Control(pDX, IDC_LEVEL, m_LevelCtrl);
	DDX_Control(pDX, IDC_STATIC_SCORE, m_StaticScore);
	DDX_Control(pDX, IDC_STATIC_LEVEL, m_StaticLevel);
	DDX_Control(pDX, IDC_PREFRAME, m_PreFrame);
	DDX_Control(pDX, IDC_BLOCK, m_Block);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDiamondDlg, CDialog)
	//{{AFX_MSG_MAP(CDiamondDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_FILE_PAUSE, OnFilePause)
	ON_COMMAND(ID_FILE_NEWGAME, OnFileNewgame)
	ON_WM_TIMER()
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	ON_COMMAND(ID_FILE_RESTART, OnFileRestart)
	ON_COMMAND(ID_FILE_EXIT, OnFileExit)
	ON_COMMAND(ID_FILE_RESUME, OnFileResume)
	ON_COMMAND(ID_OPTION_CONFIG, OnOptionConfig)
	ON_COMMAND(ID_QUERY_RECORD, OnQueryRecord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiamondDlg message handlers

BOOL CDiamondDlg::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);
		}
	}

	// 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
	//////////////////////////////
	RECT rect;
	//设置主对话框大小
	GetClientRect(&rect);
	rect.right = rect.left + 300;
	rect.bottom = rect.top + 510;
	MoveWindow(&rect);
	//设置绘图区大小
	m_Block.GetClientRect(&rect);
	rect.left += 10;
	rect.right = rect.left + 204;
	rect.bottom = rect.top + 444;
	m_Block.MoveWindow(&rect);
	//设置预览窗大小
	rect.left = rect.right + 10;
	rect.right = rect.left + 65;
	rect.bottom = rect.top + 65;
	m_PreFrame.MoveWindow(&rect);
	//移动状态栏
	rect.right = rect.left + 60;	//SCORE静态文本
	rect.top = rect.bottom + 10;
	rect.bottom =rect.top + 30;
	m_StaticScore.MoveWindow(&rect);

	rect.top = rect.bottom + 10;	//得分
	rect.bottom = rect.top + 30;
	m_ScoreCtrl.MoveWindow(&rect);
	m_ScoreCtrl.SetWindowText("0000");
	
	rect.top = rect.bottom + 10;	//LEVEL静态文本
	rect.bottom = rect.top + 30;
	m_StaticLevel.MoveWindow(&rect);

	rect.top = rect.bottom + 10;	//级别
	rect.bottom = rect.top + 30;
	m_LevelCtrl.MoveWindow(&rect);
	m_LevelCtrl.SetWindowText("1");

	rect.top = rect.bottom + 50;	//Writer
	rect.bottom = rect.top + 40;
	m_Writer.MoveWindow(&rect);

	m_Bitmap[0].LoadBitmap(IDB_BITMAP1);
	m_Bitmap[1].LoadBitmap(IDB_BITMAP2);
	m_Bitmap[2].LoadBitmap(IDB_BITMAP3);
	m_Bitmap[3].LoadBitmap(IDB_BITMAP4);
	m_Bitmap[4].LoadBitmap(IDB_BITMAP5);
	m_Bitmap[5].LoadBitmap(IDB_BITMAP6);
	m_Bitmap[6].LoadBitmap(IDB_BITMAP7);
	m_Bitmap[7].LoadBitmap(IDB_BITMAP8);
	m_Bitmap[8].LoadBitmap(IDB_BITMAP9);
	m_Bitmap[9].LoadBitmap(IDB_BITMAP10);
	m_Bitmap[10].LoadBitmap(IDB_BITMAP11);
	m_Bitmap[11].LoadBitmap(IDB_BITMAP12);

	FILE *fp;
	fp=fopen("diamond.dat","r");
	if(fp==NULL) return TRUE;
	fscanf(fp,"%d",&m_recordCount);
	for(int i=0;i<m_recordCount;i++)
	{
		fscanf(fp,"%d%s%ld%d%s",&(m_recordNo[i]),m_recordName[i],&(m_recordScore[i]),&(m_recordLevel[i]),m_recordDate[i]);
		if(m_maxScore<m_recordScore[i]) m_maxScore=m_recordScore[i];
	}
	fclose(fp);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDiamondDlg::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 CDiamondDlg::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
	{
		CDialog::OnPaint();
	}
	//画格网
	m_pBlockDC = m_Block.GetDC();
	m_Block.GetClientRect(&m_Rect);
	CPen Pen(PS_SOLID,1,m_RasterColor);
	CPen *pOldPen=m_pBlockDC->SelectObject(&Pen);
	CBrush Brush;
	Brush.CreateSolidBrush(m_bkColor);
	m_pBlockDC->FillRect(&m_Rect,&Brush);

	if(m_bShowRaster==TRUE)
	{
		POINT pt;
		int dx=(m_Rect.right-m_Rect.left)/10;
		int dy=(m_Rect.bottom-m_Rect.top)/22;
		for(int i=1;i<22;i++)
		{
			pt.x = m_Rect.left;
			pt.y = dy*i;
			m_pBlockDC->MoveTo(pt);
			pt.x=m_Rect.right;
			m_pBlockDC->LineTo(pt);
		}
		for(int j=1;j<10;j++)
		{
			pt.x=dx*j;
			pt.y=m_Rect.top;
			m_pBlockDC->MoveTo(pt);
			pt.y=m_Rect.bottom;
			m_pBlockDC->LineTo(pt);
		}
	}
	DrawAll();
	if(m_bPause) DrawCurBlock();
	m_pBlockDC->SelectObject(pOldPen);
}

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

BOOL CDiamondDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	/*ARAM lParam = pMsg->lParam & 0xffff;
	switch(pMsg->message)
	{
	 case WM_CHAR:
		 //Onchar(pMsg->wParam,lParam,pMsg->lParam);
	     break;
	 case WM_KEYUP:
		 break;
	 default:
		 break;
	}*/
   if(pMsg->message!=WM_KEYUP)
	switch(pMsg->wParam)
	{
		case 38:	//UP-Rotation
			if(m_bPause)
			{
				SetTimer(1,600-50*m_Level,NULL);
				m_bPause=FALSE;
			}
			Rotation();
			 break;
		case 37:	//Left--Move left
			if(m_bPause)
			{
				SetTimer(1,600-50*m_Level,NULL);
				m_bPause=FALSE;
			}
			MoveLeft();
			 break;
		case 39:	//Right	--Move right
			if(m_bPause)
			{
				SetTimer(1,600-50*m_Level,NULL);
				m_bPause=FALSE;
			}
			MoveRight();
			 break;
		case 40:	//Down--加速
			if(m_bPause)
			{
				SetTimer(1,600-50*m_Level,NULL);
				m_bPause=FALSE;
			}
			Acceleration();
		default:
			 break;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CDiamondDlg::DrawNextBlock(int index)
{
	CDC *pDC = m_PreFrame.GetDC();
	POINT pt;
	int size=16;
	pt.x=270;
	pt.y=40;

    RECT rect;
	m_PreFrame.GetClientRect(&rect);
	CBrush Brush;
	Brush.CreateSolidBrush(RGB(128,128,128));
	CBrush* pOldBrush=m_PreFrame.GetDC()->SelectObject(&Brush);
	pDC->FillRect(&rect,&Brush);
	
	pt.x = (rect.right + rect.left)/2;
	pt.y = (rect.top + rect.bottom)/2;
	switch(index)
	{
	 case 1:
		pDC->Draw3dRect(pt.x-size,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x-2*size,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x+size,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		break;
	 case 2:
		pDC->Draw3dRect(pt.x-3*size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x-size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x-size/2,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		pDC->Draw3dRect(pt.x+size/2,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		break;
	 case 3:
		 pDC->Draw3dRect(pt.x-3*size/2,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x+size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 break;
	 case 4:
		 pDC->Draw3dRect(pt.x-3*size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x+size/2,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 break;
	 case 5:
		 pDC->Draw3dRect(pt.x-size,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x,pt.y-size,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x,pt.y,size,size,RGB(255,0,0),RGB(255,0,0));
		 break;
	 case 6:
		 pDC->Draw3dRect(pt.x-3*size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x+size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x+size/2,pt.y+size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 break;
	 case 7:
		 pDC->Draw3dRect(pt.x-3*size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x+size/2,pt.y-size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 pDC->Draw3dRect(pt.x-3*size/2,pt.y+size/2,size,size,RGB(255,0,0),RGB(255,0,0));
		 break;
	 default: break;
	}
	pDC->SelectObject(pOldBrush);
}

void CDiamondDlg::OnFilePause() 
{
	// TODO: Add your command handler code here
	if(m_bStart)
	{
		KillTimer(1);
		m_bPause=TRUE;
	}
}


void CDiamondDlg::OnFileNewgame() 
{
	// TODO: Add your command handler code here
	m_bStart=TRUE;	//游戏已经开始
	m_bPause=FALSE;
	for(int i=0;i<22;i++)

⌨️ 快捷键说明

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