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

📄 bitmapbrowserdlg.cpp

📁 自己用MFC做的一个小的游戏程序MFC当文档框架
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// BitmapBrowserDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BitmapBrowser.h"
#include "BitmapBrowserDlg.h"

#include "SettingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBitmapBrowserDlg dialog

CBitmapBrowserDlg::CBitmapBrowserDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBitmapBrowserDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBitmapBrowserDlg)
		// 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);

	m_hbmp=NULL;
	m_strFileName="";
	m_IsOpenbmp=FALSE;
	m_IsBeginGame=FALSE;
	m_IsPauseGame=FALSE;
	m_Scores=0;
	m_Velocity=1;
	m_ptFlag=0;

	ROW=3;
	COL=5;
	m_IsCheat=1;
}

void CBitmapBrowserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBitmapBrowserDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBitmapBrowserDlg, CDialog)
	//{{AFX_MSG_MAP(CBitmapBrowserDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDM_MENU_OPEN, OnMenuOpen)
	ON_COMMAND(IDM_MENU_EXIT, OnMenuExit)
	ON_COMMAND(IDM_MENU_CLOSEBITMAP, OnMenuClosebitmap)
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_COMMAND(IDM_MEUN_START, OnMeunStart)
	ON_COMMAND(IDM_MEUN_PAUSE, OnMeunPause)
	ON_COMMAND(IDM_MENU_CHOOSEPIC, OnMenuChoosepic)
	ON_COMMAND(IDM_MENU_SETTING, OnMenuSetting)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBitmapBrowserDlg message handlers

BOOL CBitmapBrowserDlg::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
	
	GetClientRect(&m_rectClient);

	//初始化矩阵
	int i,j,k;
	for (i=0;i<30;i++)
	{
		for (j=0;j<20;j++)
		{
			m_Matrix[i][j].pt=CPoint(i,j);
			m_Matrix[i][j].state=FALSE;
			m_Matrix[i][i].clr=0;
		}
	}

	srand(time(0));

	//初始化飞机
	int pos=(rand()%18)+1;
	m_sOB[0]=&m_Matrix[28][pos];
	m_sOB[1]=&m_Matrix[29][pos-1];
	m_sOB[2]=&m_Matrix[29][pos];
	m_sOB[3]=&m_Matrix[29][pos+1];
	for (k=0;k<4;k++)
	{
		m_sOB[k]->state=TRUE;
		m_sOB[k]->clr=RGB(200,200,0);
	}

	//作弊用
	if(m_IsCheat==0)
		for (k=0;k<19;k++)
		{
			m_Matrix[29][k].state=TRUE;
			m_Matrix[29][k].clr=RGB(rand()%256,rand()%256,rand()%256);
		}

	//画被撞矩形
	COLORREF clr=RGB(rand()%256,rand()%256,rand()%256);
	int rowBegin=(28-ROW)/2-5;
	int colBegin=(20-COL)/2;
	if (rowBegin<0)
	{
		rowBegin=0;
	}
	if (colBegin<0)
	{
		colBegin=0;
	}
	for (i=0;i<ROW;i++)
	{
		for (j=0;j<COL;j++)
		{
			m_Matrix[rowBegin+i][colBegin+j].state=TRUE;
			m_Matrix[rowBegin+i][colBegin+j].clr=clr;
				
		}
	}
	
	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 CBitmapBrowserDlg::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();
	}

		
//////////////////////////////////////////////////////////////////////////
	
	fnRenderScene();

}

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

void CBitmapBrowserDlg::OnMenuOpen() 
{
	// TODO: Add your command handler code here

	if (m_hbmp!=NULL)
	{
		m_IsOpenbmp=TRUE;
		Invalidate();
		return;
	}
	else
	{
		MessageBox("您还未选择图片!","提示",MB_OK | MB_ICONASTERISK );
		return;
	}

}

void CBitmapBrowserDlg::OnMenuExit() 
{
	// TODO: Add your command handler code here
	CDialog::OnCancel();
}

void CBitmapBrowserDlg::OnMenuClosebitmap() 
{
	// TODO: Add your command handler code here
	m_IsOpenbmp=FALSE;
	Invalidate();
}

void CBitmapBrowserDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (1==nIDEvent && m_IsBeginGame && !m_IsPauseGame)
	{
		if(m_ptFlag==LEFTUP)
		{
			RandHeadMoving(LEFTUP);
		}
		else if(m_ptFlag==RIGHTUP)
		{
			RandHeadMoving(RIGHTUP);
		}
		else if(m_ptFlag==LEFTDOWN)
		{
			RandHeadMoving(LEFTDOWN);
		}
		else if(m_ptFlag==RIGHTDOWN)
		{
			RandHeadMoving(RIGHTDOWN);
		}
	}
	if (2==nIDEvent)
	{
		fnRenderScene();
	}
	CDialog::OnTimer(nIDEvent);
}

void CBitmapBrowserDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	switch (nChar)
	{
	case 'A':
		MoveOB(LEFT);
		break;
	case 'D':
		MoveOB(RIGHT);
		break;
	case 'W':
		MoveOB(UP);
		break;
	case 'S':
		MoveOB(DOWN);
		break;
	case VK_SPACE:
// 		OnMeunStart();
		OnMeunPause();
		break;
	default:
		break;
	}
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CBitmapBrowserDlg::MoveOB(CPoint point)
{
	if (!m_IsBeginGame || m_IsPauseGame)
	{
		return;
	}
	
	CPoint ptTemp;
	COLORREF clr;
	if (LEFT==point)
	{	
		if (m_sOB[1]->pt.y==0)
		{
			return;
		}
		for (int i=1;i<4;i++)
		{
			clr=m_sOB[i]->clr;
			ptTemp=m_sOB[i]->pt+point;
			m_sOB[i]->state=FALSE;
			m_sOB[i]=&m_Matrix[ptTemp.x][ptTemp.y];
			m_sOB[i]->state=TRUE;
			m_sOB[i]->clr=clr;
		}
	}
	else if (RIGHT==point)
	{
		if (m_sOB[3]->pt.y==19)
		{
			return;
		}
		for (int i=3;i>0;i--)
		{
			clr=m_sOB[i]->clr;
			ptTemp=m_sOB[i]->pt+point;
			m_sOB[i]->state=FALSE;
			m_sOB[i]=&m_Matrix[ptTemp.x][ptTemp.y];
			m_sOB[i]->state=TRUE;
			m_sOB[i]->clr=clr;
		}
	}
// 	else if (UP==point)
// 	{
// 		for (int i=1;i<4;i++)
// 		{
// 			clr=m_sOB[i]->clr;
// 			ptTemp=m_sOB[i]->pt+point;
// 			m_sOB[i]->state=FALSE;
// 			m_sOB[i]=&m_Matrix[ptTemp.x][ptTemp.y];
// 			m_sOB[i]->state=TRUE;
// 			m_sOB[i]->clr=clr;
// 		}
// 
// 	}
// 	else if (DOWN==point)
// 	{
// 		if (m_sOB[1]->pt.x==29)
// 		{
// 			return;
// 		}
// 		for (int i=1;i<4;i++)
// 		{
// 			clr=m_sOB[i]->clr;
// 			ptTemp=m_sOB[i]->pt+point;
// 			m_sOB[i]->state=FALSE;
// 			m_sOB[i]=&m_Matrix[ptTemp.x][ptTemp.y];
// 			m_sOB[i]->state=TRUE;
// 			m_sOB[i]->clr=clr;
// 		}
// 	}
}


void CBitmapBrowserDlg::OnMeunStart() 
{
	// TODO: Add your command handler code here
	OnInitDialog();
	m_Scores=0;
	m_IsBeginGame=TRUE;
	m_IsPauseGame=FALSE;

	int timespan=(10-m_Velocity+1)*30;
	SetTimer(1,timespan,NULL);
	SetTimer(2,TIMESPAN,NULL);
	if (rand()%2)
	{
		m_ptFlag=LEFTUP;
	}
	else
	{
		m_ptFlag=RIGHTUP;
	}
}

void CBitmapBrowserDlg::OnMeunPause() 
{
	// TODO: Add your command handler code here
	if (m_IsPauseGame==TRUE)
	{
		m_IsPauseGame=FALSE;
		int timespan=(10-m_Velocity+1)*30;
		SetTimer(1,TIMESPAN,NULL);
	}
	else if (m_IsPauseGame==FALSE)
	{
		m_IsPauseGame=TRUE;
		KillTimer(1);
	}
// 	Invalidate();
}


void CBitmapBrowserDlg::HeadMoving(CPoint point)
{
	if (!m_IsBeginGame || m_IsPauseGame)
	{
		return;
	}
	m_ptFlag=point;
	CPoint ptTemp;
	COLORREF clr;
	ptTemp=m_sOB[0]->pt+point;
	if (LEFTUP==point)
	{
		if (m_sOB[0]->pt.y==0 || m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state==TRUE || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state==TRUE)
		{
			m_ptFlag=RIGHTUP;
			if (m_sOB[0]->pt.y-1<0 || m_sOB[0]->pt.x-1<0)
			{
				return;
			}
			if(m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].clr=0;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state=FALSE;
			}
			if (m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state=FALSE;
			}
			return;
		}
		if (m_sOB[0]->pt.x==0 || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state==TRUE || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state==TRUE)
		{
			m_ptFlag=LEFTDOWN;
			if (m_sOB[0]->pt.x-1<0 || m_sOB[0]->pt.y-1<0)
			{
				return;
			}
			if(m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state=FALSE;
			}
			if (m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y-1].state=FALSE;
			}
			return;
		}
	}
	else if (RIGHTUP==point)
	{
		if (m_sOB[0]->pt.x==0 || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state==TRUE || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state==TRUE)
		{
			m_ptFlag=RIGHTDOWN;
			if (m_sOB[0]->pt.x-1<0 || m_sOB[0]->pt.y+1>19)
			{
				return;
			}
			if(m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state==TRUE)
			{
 				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y].state=FALSE;
			}
			if (m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state=FALSE;
			}
			return;
		}
		if (m_sOB[0]->pt.y==19 || m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y+1].state==TRUE || m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state==TRUE)
		{
			m_ptFlag=LEFTUP;
			if (m_sOB[0]->pt.x-1<0 || m_sOB[0]->pt.y+1>19)
			{
				return;
			}
			if (m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y+1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y+1].clr=0;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y+1].state=FALSE;
			}
			if (m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].clr=0;
				m_Matrix[m_sOB[0]->pt.x-1][m_sOB[0]->pt.y+1].state=FALSE;
			}
			return;
		}
	}
	else if (LEFTDOWN==point)
	{
		if (m_sOB[0]->pt.y==0 || m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state==TRUE /*|| m_Matrix[m_sOB[0]->pt.x+1][m_sOB[0]->pt.y-1].state==TRUE*/)
		{
			m_ptFlag=RIGHTDOWN;
			if (m_sOB[0]->pt.y-1<0 || m_sOB[0]->pt.x+1>28)
			{
				return;
			}
			if (m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state==TRUE)
			{
				m_Scores++;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].clr=0;
				m_Matrix[m_sOB[0]->pt.x][m_sOB[0]->pt.y-1].state=FALSE;
			}
// 			if (m_Matrix[m_sOB[0]->pt.x+1][m_sOB[0]->pt.y-1].state==TRUE)
// 			{
// 				m_Scores++;
// 				m_Matrix[m_sOB[0]->pt.x+1][m_sOB[0]->pt.y-1].clr=0;
// 				m_Matrix[m_sOB[0]->pt.x+1][m_sOB[0]->pt.y-1].state=FALSE;

⌨️ 快捷键说明

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