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

📄 mygameview.cpp

📁 随手写的小游戏
💻 CPP
字号:
// MyGameView.cpp : implementation of the CMyGameView class
//

#include "stdafx.h"
#include "MyGame.h"

// #include "GameStru.h"

#include "MyGameDoc.h"
#include "MyGameView.h"

#include "GameSetup.h"

#include "MainFrm.h"

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

#define COLOR_YELLOW  RGB(255, 255,   0)
#define COLOR_PURPLE  RGB(255,   0, 255)
#define COLOR_BLACK   RGB(  0,   0,   0)
#define COLOR_RED     RGB(255,   0,   0)
#define COLOR_GREEN   RGB(  0, 255,   0)
#define COLOR_DKGREEN RGB(  0, 128,   0)
#define COLOR_BLUE    RGB(  0,   0, 255)
#define COLOR_AQUA    RGB(  0, 255, 255)
#define COLOR_GRAY    RGB(192, 192, 192)


// 分数:

#define	BONUS_BAD_CLICK		-1
#define	BONUS_RIGHT_CLICK	5
#define BONUS_FAST_CLICK    2
#define BONUS_SLOW_CLICK    -1
#define BONUS_FASTER_CLICK  5
#define BONUS_FAST_CLICK_EXT  4
#define BONUS_FASRER_CLICK_EXT  10
#define BONUS_SAVE_SECOND    5

/////////////////////////////////////////////////////////////////////////////
// CMyGameView

IMPLEMENT_DYNCREATE(CMyGameView, CFormView)

BEGIN_MESSAGE_MAP(CMyGameView, CFormView)
	//{{AFX_MSG_MAP(CMyGameView)
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_GAME_NEW, OnGameNew)
	ON_UPDATE_COMMAND_UI(ID_GAME_NEW, OnUpdateGameNew)
	ON_WM_TIMER()
	ON_COMMAND(ID_GAME_SETUP, OnGameSetup)
	ON_UPDATE_COMMAND_UI(ID_GAME_SETUP, OnUpdateGameSetup)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyGameView construction/destruction

CMyGameView::CMyGameView()
	: CFormView(CMyGameView::IDD)
{
	//{{AFX_DATA_INIT(CMyGameView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_iGameType = GAME_NUMBER_STRING;

	m_pItemList = NULL;
	m_pItemFont = NULL;
	m_pClockFont= NULL;
	m_pClickSeq = NULL;
	m_hCursorHand = NULL;
	m_iColumnCount = 5;
	m_iRowCount = 5;


	m_UnclickBrush.CreateSolidBrush(COLOR_GRAY);
	m_ClickBrush.CreateSolidBrush(COLOR_YELLOW);
}

CMyGameView::~CMyGameView()
{
	if(m_pItemFont != NULL)
		delete m_pItemFont;
	if(m_pClockFont != NULL)
		delete m_pClockFont;
	if(m_pItemList != NULL)
		delete m_pItemList;
	if(m_pClickSeq != NULL)
		delete m_pClickSeq;
}

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

BOOL CMyGameView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CMyGameView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	OnGameNew();


}

/////////////////////////////////////////////////////////////////////////////
// CMyGameView printing

BOOL CMyGameView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyGameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyGameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CMyGameView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CMyGameView diagnostics

#ifdef _DEBUG
void CMyGameView::AssertValid() const
{
	CFormView::AssertValid();
}

void CMyGameView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CMyGameDoc* CMyGameView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyGameDoc)));
	return (CMyGameDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyGameView message handlers

void CMyGameView::OnSize(UINT nType, int cx, int cy) 
{
	ResizeAllItems(cx,cy);
//	CFormView::OnSize(nType, cx, cy);

	CMainFrame	*pMainWnd;
	pMainWnd = (CMainFrame	*)AfxGetMainWnd();
	if(pMainWnd != NULL)
	{
		CRect ClockRect;

		pMainWnd->m_wndTopBar.GetWindowRect(&ClockRect); 
		pMainWnd->m_wndTopBar.SetWindowPos(NULL,ClockRect.left,ClockRect.top,
			cx,ClockRect.bottom-ClockRect.top,
			SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);

		CWnd *pWndClock;
		pWndClock = pMainWnd->m_wndTopBar.GetDlgItem(IDC_TIME_USED);
		pWndClock->GetClientRect(&ClockRect);


		if(m_pClockFont != NULL)
			delete m_pClockFont;
		m_pClockFont = new CFont();
		m_pClockFont->CreateFont((ClockRect.bottom-ClockRect.top) , (ClockRect.right-ClockRect.left) / 5, 0, 0,
			 FW_BOLD, FALSE, FALSE,
			 FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
			 DEFAULT_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Courier New");
		pWndClock->SetFont(m_pClockFont);

		pWndClock = pMainWnd->m_wndTopBar.GetDlgItem(IDC_GAME_BONUS);
		pWndClock->SetFont(m_pClockFont);
	}

	// resize all buttons
}

BOOL CMyGameView::BuildAllItems(int iColumn, int iRow)
{
//	if( m_iColumnCount == iColumn && m_iRowCount == iRow )
//		return FALSE;

	if( m_pItemList != NULL)
	{
		// delete original list
	}
	if( m_pClickSeq != NULL)
	{
	}

	m_pItemList = new CLICK_ITEM[iColumn*iRow];
	if(m_pItemList == NULL)
		return FALSE;
	memset(m_pItemList,0,sizeof(CLICK_ITEM)*iColumn*iRow);
	m_pClickSeq = new int[iColumn*iRow];
	if(m_pClickSeq == NULL)
		return FALSE;
	memset(m_pClickSeq,0,sizeof(int)*iColumn*iRow);
	int		iLoop1,iLoop2;
	for(iLoop1=0;iLoop1<iRow;iLoop1++)
	{
		for(iLoop2=0;iLoop2<iColumn;iLoop2++)
		{
			m_pItemList[iLoop1*iColumn+iLoop2].Index = iLoop1*iColumn+iLoop2;
			sprintf(m_pItemList[iLoop1*iColumn+iLoop2].Text,"%02i",iLoop1*iColumn+iLoop2+1);
		}
	}
	m_iColumnCount = iColumn;
	m_iRowCount = iRow;
	BuildClickSequ(1);

	return FALSE;
}

BOOL CMyGameView::ResizeAllItems(int cx,int cy)
{
	int		iLoop1,iLoop2,iIndex;

	int		iWidth,iHeight;

	if( m_iColumnCount == 0 || m_iRowCount == 0 || m_pItemList == NULL)
		return FALSE;

	iWidth = cx / m_iColumnCount;
	iHeight = cy / m_iRowCount;
	for(iLoop1=0;iLoop1<m_iRowCount;iLoop1++)
	{
		for(iLoop2=0;iLoop2<m_iColumnCount;iLoop2++)
		{
			iIndex = iLoop1*m_iColumnCount+iLoop2;
			m_pItemList[iIndex].ItemRect.top = iHeight * iLoop1;
			if( iLoop1 == (m_iRowCount-1))
				m_pItemList[iIndex].ItemRect.bottom = cy-1;
			else
				m_pItemList[iIndex].ItemRect.bottom = (iHeight) * (iLoop1+1) -1;

			m_pItemList[iIndex].ItemRect.left = iWidth * iLoop2;
			if( iLoop2 == (m_iColumnCount-1))
				m_pItemList[iIndex].ItemRect.right = cx-1;
			else
				m_pItemList[iIndex].ItemRect.right = (iWidth) * (iLoop2+1) -1;

//			sprintf(m_pItemList[iLoop1*iColumn+iLoop2].Text,"%02i",iLoop1*iColumn+iLoop2+1);
		}
	}
	if(m_pItemFont != NULL)
		delete m_pItemFont;
//	if(m_pClickSeq != NULL)
//		delete m_pClickSeq;
	m_pItemFont = new CFont();

	m_pItemFont->CreateFont(iHeight / 2, iWidth / 4, 0, 0,
         FW_BOLD, FALSE, FALSE,
         FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
         DEFAULT_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Lucida Sans"/*"Arial"*/);

	return TRUE;
}


void CMyGameView::OnDraw(CDC* pDC) 
{
//	CTestDoc *pDoc = GetDocument();
	CPen     *oldPen;
	CFont    *oldFont;
	char     buf[12];

	CRect rect;

	oldFont = pDC->SelectObject(m_pItemFont);

/*	int color  = 0;
	int width  = m_totalLog.cx / 10;
	int height = m_totalLog.cy / 10;
	int widthPad  = width  / 10;
	int heightPad = height / 10; */
	int textX, textY; 

	CSize size;
	CPen newPen(PS_SOLID, 0, COLOR_DKGREEN);

	int		iLoop1,iLoop2,iIndex;

	for(iLoop1=0;iLoop1<m_iRowCount;iLoop1++)
	{
		for(iLoop2=0;iLoop2<m_iColumnCount;iLoop2++)
		{
			iIndex = iLoop1*m_iColumnCount+iLoop2;
			rect=m_pItemList[iIndex].ItemRect;

			switch(m_pItemList[iIndex].State)
			{
			case 0:
				oldPen = pDC->SelectObject(&newPen);
//				pDC->Draw3dRect(rect,COLOR_GREEN,COLOR_RED);
				pDC->Rectangle(&rect);
				pDC->FillRect(&rect,&m_UnclickBrush);

				textX = rect.left + (rect.Width()/4);
				textY = rect.top  + (rect.Height()/4);
				pDC->SetTextColor(COLOR_BLUE);
				pDC->SetBkMode(TRANSPARENT);
				pDC->TextOut(textX, textY,m_pItemList[iIndex].Text ,
						strlen(m_pItemList[iIndex].Text));
				pDC->SelectObject(oldPen);
				break;
			case 1:
//				break;
				oldPen = pDC->SelectObject(&newPen);
//				pDC->Draw3dRect(rect,COLOR_GREEN,COLOR_RED);
				pDC->Rectangle(&rect);
				pDC->FillRect(&rect,&m_ClickBrush);

				textX = rect.left + (rect.Width()/4);
				textY = rect.top  + (rect.Height()/4);
				pDC->SetTextColor(COLOR_PURPLE);
				pDC->SetBkMode(TRANSPARENT);
				pDC->TextOut(textX, textY,m_pItemList[iIndex].Text ,
						strlen(m_pItemList[iIndex].Text));
				pDC->SelectObject(oldPen);
				break;
			default:
				break;
			}
		}
	}

	pDC->SelectObject(oldFont);
	
}

BOOL CMyGameView::BuildClickSequ(int iMode)
{
	// 构造随机序列
	int		iLoop1,iNextLoc;
	BOOL	bAllocList[100];

	srand((unsigned)time(NULL)); 
//	bAllocList = new BOOL[m_iColumnCount * m_iRowCount];
	memset(bAllocList,0,sizeof(BOOL)*m_iColumnCount * m_iRowCount);

	iNextLoc = rand()% ( m_iColumnCount * m_iRowCount );
	for(iLoop1=0;iLoop1< m_iColumnCount * m_iRowCount; iLoop1++)
	{
		while(bAllocList[iNextLoc])
		{
			iNextLoc = rand()% ( m_iColumnCount * m_iRowCount );
		}

		bAllocList[iNextLoc] = TRUE;
		m_pItemList[iNextLoc].Index = iLoop1;
		sprintf(m_pItemList[iNextLoc].Text,"%02i",iLoop1+1);
		m_pClickSeq[iLoop1] = iNextLoc;

//		iNextLoc = rand()% ( m_iColumnCount * m_iRowCount );

	}

	return TRUE;
}

void CMyGameView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int		iIndex;

	CMainFrame	*pMainWnd;
	pMainWnd = (CMainFrame	*)AfxGetMainWnd();
	if(pMainWnd != NULL)
	{
	}
	DWORD	dwCurTick;
	int		iLength;
	switch(m_iGameType)
	{
	case GAME_NUMBER_STRING:
		if( m_iCurClick < 0)
			return;
		iIndex = GetPointItem(point);
		if(iIndex < 0)
			return;
		if( m_pClickSeq[m_iCurClick] == iIndex )
		{
			m_pItemList[iIndex].State = 1;
			InvalidateRect(&m_pItemList[iIndex].ItemRect);
			if(m_iCurClick == 0)
			{
				// 开始计时
			}
			m_iGameBonus += BONUS_RIGHT_CLICK;

			dwCurTick = GetTickCount();
			m_pItemList[iIndex].ClickTick = dwCurTick - m_dwGameStartTick;
			dwCurTick  -= m_dwGameStartTick;
			if( m_iCurClick >= 1)
			{
				int		iPrevIdx = m_pClickSeq[m_iCurClick-1];

				DWORD		iLastClick = 
					m_pItemList[iIndex].ClickTick - m_pItemList[m_pClickSeq[m_iCurClick-1]].ClickTick;
				iLength = abs( iPrevIdx%m_iColumnCount - iIndex%m_iColumnCount)
					+ abs( iPrevIdx%m_iRowCount - iIndex%m_iRowCount);

				if( (m_iCurClick >= 3) &&  (iLength >= 7)
					&& (dwCurTick - m_pItemList[m_pClickSeq[m_iCurClick-3]].ClickTick < 800) )
				{
					m_iGameBonus += BONUS_FASRER_CLICK_EXT;
				}
				else if( (m_iCurClick >= 3) 
					&& (dwCurTick - m_pItemList[m_pClickSeq[m_iCurClick-3]].ClickTick < 800) )
				{
					m_iGameBonus += BONUS_FASTER_CLICK;
				}
				else if( (m_iCurClick >= 1) &&  (iLength >= 7)
					&& (dwCurTick - m_pItemList[m_pClickSeq[m_iCurClick-1]].ClickTick < 800) )
				{
					m_iGameBonus += BONUS_FAST_CLICK_EXT;
				}
				else if( (m_iCurClick >= 1) 
					&& (dwCurTick - m_pItemList[m_pClickSeq[m_iCurClick-1]].ClickTick < 800) )
				{
					m_iGameBonus += BONUS_FAST_CLICK;
				}
			}

// #define BONUS_SLOW_CLICK    -1


			m_iCurClick ++;
			if( m_iCurClick >= 	m_iColumnCount * m_iRowCount)
			{
				m_iCurClick = -1;
// #define BONUS_SAVE_SECOND    5
				KillTimer(8888);

			}
			DisplayBonus();
		}
		else	// bad click
		{
			if(m_iGameBonus > ( BONUS_BAD_CLICK * -1) )
				m_iGameBonus += BONUS_BAD_CLICK;
			DisplayBonus();
		}
		break;
	default:
		break;
	}
}

int CMyGameView::GetPointItem(CPoint point)
{
	CRect	ItemRect;
	int		iLoop1;
	for(iLoop1=0;iLoop1< m_iColumnCount * m_iRowCount; iLoop1++)
	{

		ItemRect = m_pItemList[iLoop1].ItemRect;
		if( ItemRect.PtInRect(point))
		{
			return iLoop1;
		}
	}
	return -1;
}

void CMyGameView::OnGameNew() 
{
	CRect	RectTemp;
	CMainFrame	*pMainWnd;
	pMainWnd = (CMainFrame	*)AfxGetMainWnd();
	
	switch(m_iGameType)
	{
	case GAME_NUMBER_STRING:
		m_pItemList = NULL;
		m_pClickSeq = NULL;
		m_iCurClick = 0;
		m_iGameBonus = 0;
		BuildAllItems(	m_iColumnCount ,m_iRowCount );

		KillTimer(8888);
		GetClientRect(&RectTemp); 
		ResizeAllItems(RectTemp.right - RectTemp.left,RectTemp.bottom-RectTemp.top);
		InvalidateRect(NULL);


		if(pMainWnd != NULL)
		{
			pMainWnd->m_wndTopBar.SetDlgItemText(IDC_TIME_USED,"");
		}

		SetTimer(8888,100,NULL);
		m_dwGameStartTick = GetTickCount();
		break;
	default:
		break;
	}
}

void CMyGameView::OnUpdateGameNew(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(TRUE);
}


void CMyGameView::OnTimer(UINT nIDEvent) 
{
	CMainFrame	*pMainWnd;
	pMainWnd = (CMainFrame	*)AfxGetMainWnd();


	if( nIDEvent == 7777 )
	{

		return;
	}

	if(pMainWnd != NULL)
	{
		DWORD	dwCurTick = GetTickCount();
		CString	strDisplay;
		if(dwCurTick-m_dwGameStartTick >= 1000 * 100 )
			strDisplay.Format("%4i",
				(int)((dwCurTick-m_dwGameStartTick)/1000));
		else if(dwCurTick-m_dwGameStartTick >= 1000 * 10 )
			strDisplay.Format("%3i.%01i",
				(int)((dwCurTick-m_dwGameStartTick)/1000),
				(int)(((dwCurTick-m_dwGameStartTick)%1000)/100));
		else
			strDisplay.Format("%2i.%02i",
				(int)((dwCurTick-m_dwGameStartTick)/1000),
				(int)(((dwCurTick-m_dwGameStartTick)%1000)/10));

		pMainWnd->m_wndTopBar.SetDlgItemText(IDC_TIME_USED,strDisplay);
	}
	
	CFormView::OnTimer(nIDEvent);
}

void CMyGameView::OnGameSetup() 
{
	CGameSetup		DlgGameSetup(_T("游戏设置"),this, GAME_NUMBER_STRING );

//	DlgGameSetup.Create(this);
	if( DlgGameSetup.DoModal() == IDOK)
	{
//		MessageBox("aaaa");
		OnGameNew();
	}
}

void CMyGameView::OnUpdateGameSetup(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable();
}

void CMyGameView::DisplayBonus()
{
	CMainFrame	*pMainWnd;
	pMainWnd = (CMainFrame	*)AfxGetMainWnd();
	if(pMainWnd != NULL)
	{
		CString		strDisplay;
		strDisplay.Format("%i",m_iGameBonus);
		pMainWnd->m_wndTopBar.SetDlgItemText(IDC_GAME_BONUS,strDisplay);
	}
}

⌨️ 快捷键说明

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